javaeclipsegradlebuildship

Eclipse Gradle adding a classpath entry


I'm adding a classpath entry to the .classpath file in Eclipse to avoid having to add it manually each time I run the .eclipse task while I add a number of dependencies. I need some resources on the path to run locally.

this works,

eclipse.classpath.file {
   withXml { 
     def node = it.asNode()
     node.appendNode('classpathentry', 
                [kind: 'lib', path: '/some/path'])
           }
}

this doesn't,

eclipse.classpath.file {
   whenMerged { classpath ->
      classpath.entries.add { entry -> kind: 'lib', path: '/some/path' }
              }
}

The error I get is,

startup failed: build.gradle': 75: unexpected token: lib @ line 75, column 48. .entries.add { entry -> kind: 'lib', pat ^

For future reference, what is wrong with the second example?


Solution

  • The equivalent should be something like:

    eclipse.classpath.file {
      whenMerged { classpath ->
        def lib = new org.gradle.plugins.ide.eclipse.model.Library(fileReference(file('path/to/my/jar')))
        lib.exported = true
        classpath.entries << lib
      }
    }
    

    See Gradle docs for Library and its interface ClasspathEntry.