How to read the dependencies from an eclipse .classpath?

  • I would like to read dependencies from an eclipse .classpath file but never write back to it. Essentially, the .classpath become the master dependency definition file. - In the future, I would probably want to define dependencies in build.gradle to be able to override those defined in .classpath.

I can apply the eclipse plugin and see the eclipseClasspath task but can’t quite work out how to do it.

I know this sounds horrible, please don’t ask me why!

You will have to open the ‘.classpath’ in your build.gradle, parse it and translate it to Gradle dependency notations.

I was hoping that the Eclipse plugin would do the heavy lifting!

The EclipsePlugin seems to understand the structure of the .classpath file, eg. org.gradle.plugins.ide.eclipse.model.Classpath.groovy. I was wondering if I could reuse the logic in the plugin to read the file in?

A quick look at that class leaves me with the feeling it would be easier to write your own parser.

This is the opposite direction that the Eclipse plugin goes in, so it’s not something the plugin provides.

Actually, looking again this is not too difficult:

import org.gradle.plugins.ide.eclipse.model.*
  def cp = new Classpath()
cp.load(file(".classpath").newInputStream())
def libs = cp.entries.findAll { it instanceof Library }
  dependencies {
  compile files(libs*.library*.file)
}

I haven’t tested that, but hopefully it gets you on the right path. You’ll likely have to do some digging through the source code if the above is not enough.

Thanks very much, I had to set a FileReferenceFactory when creating the Classpath

The code has set me off down the right track - very useful.

Tried the snippet but does’n seem to be working. > Cannot invoke method fromJarURI() on null object Not sure why i get the above error…