Use jar file as source set for building and static compiling

We'd like to add a second content root for a module that refers to .java source files stored in a .jar file.

We can add a content root to a module through the IDE as follows:

  1. Click File > Project Structure
  2. Click Modules
  3. Expand and click module name (e.g., main)
  4. Click Sources tab
  5. Click Add Content Root
  6. Browse to and select sources.jar
  7. Click OK
  8. Click Mark as: Sources
  9. Click OK to accept the additional sources

This works as expected: IDEA can find the class files, compile them, and open them into the editor as read-only.

The file sources.jar resembles the following:

$ jar -tvf sources.jar 
     0 Thu Feb 03 08:38:56 PST 2022 META-INF/
    52 Thu Feb 03 08:38:56 PST 2022 META-INF/MANIFEST.MF
     0 Thu Feb 03 08:38:30 PST 2022 com/
     0 Thu Feb 03 08:38:32 PST 2022 com/domain/
     0 Thu Feb 03 08:38:30 PST 2022 com/domain/package/
   938 Thu Feb 03 08:38:32 PST 2022 com/domain/package/SourceCode.java

How do we accomplish the same effect of adding to the content root using Gradle? For example:

plugins {
  id: 'idea'
}

idea.module.contentRoot = file( "jar:${projectDir}/sources.jar!/" )

When building, this produces the following error:

Cannot convert URL 'jar:/home/user/dev/project/sources.jar!/' to a file.

For details, see StackOverflow Post.

How would you use the idea plugin to add another content root to the module without extracting the jar file contents?

Please sign in to leave a comment.