4

I am trying to use Eclipse, Buildship, Gradle to develop java 9 applications.

Eclipse: Oxygen Buildship: 2.2.0 Gradle: 4.3.1

I am looking for a way to tell Buildship/Gradle to add Project and External Dependencies to the Modulepath rather than the Classpath.

Here's a representation of what I see when I configure my Eclipse project Java Build Path.

Properties for TestMain

Java Build path

Source   |   Projects   |   Libraries   |   Order and Export
                        ---------
Modulepath
   - JRE System Library [JavaSe-9]

Classpath                            
   - Project and External Dependencies
      - Access rules: No rules defined
      - External annotations: (None)
      - Native library location: (None)
         - coreutil-9.4.1.jar
         - slf4j-api-1.7.2.1.jar
         - ...

When I try to reference the automatic module coreutil in module-info.java I get the error coreutil cannot be resolved to a module.

If I manually add coreutil-9.4.1.jar to the Modulepath then the coreutil module becomes visible. This is a problem, however, since it is impractical to manually add over 60 libraries in some cases. Moreover, each time I Refresh Gradle Project they are all removed from the Modulepath.

Thanks for help.

Gaëtan

2 Answers2

3

After talking to Donát Csikós at gradle (thanks Donát) adding the following to the build.gradle file solves the problem:

apply plugin: 'eclipse'

eclipse.classpath.file {
    whenMerged {
        entries.findAll { isModule(it) }.each { it.entryAttributes['module'] = 'true' }
    }
}

boolean isModule(entry) {
    // filter java 9 modules
    entry.kind == 'lib'  // Only libraries can be modules
}
  • Works great! Searched for sooooo long till I found this helpful answer. :) – judos Jun 24 '20 at 20:34
  • This didn't work for me when the dependency module is a project that I wrote myself in eclipse (it's not a compiled jar dependency). For this case, instead of `isModule(it)`, you will need `it.kind == 'src' || it.kind == 'lib'` because not only libs can be modules, but also source projects. – user1803551 Sep 02 '23 at 22:03
0

Here is a more general solution where all libraries and projects dependencies are added to the module path of Eclipse:

apply plugin: 'eclipse'

eclipse.classpath.file {
    whenMerged {
        entries.findAll { isModule(it) }.each { it.entryAttributes['module'] = 'true' }
    }
}

boolean isModule(entry) {
    return (entry instanceof org.gradle.plugins.ide.eclipse.model.ProjectDependency) ||
           (entry instanceof org.gradle.plugins.ide.eclipse.model.Library)  
}
javasuns
  • 1,061
  • 2
  • 11
  • 22