How does Buildship create the .classpath file for Eclipse?

I’m using Buildship and Gradle with Eclipse. I have an issue I described on Stackoverflow, but I haven’t found a solution so far, so I’m asking here.

I’m wondering how Buildship creates the .classpath file for Eclipse, because apparently the Gradle task eclipseClasspath is never executed, but the .classpath file gets modified. In the end, I need to modify the order Buildship writes the classpath entries into .classpath, so I’m wondering how to do that. Is there a way to convince Buildship to use the Gradle task, or is there some other way to modify the behaviour?

Thank you and best regards,

Robert

1 Like

Your observation is correct, Buildship doesn’t call eclipseClasspath directly. It reads the configuration from the java and the eclipse Gradle plugins via the Gradle Tooling API and sets the values by using Eclipse JDT’s API. You can customize the content of the .classpath file via the eclipse.classpath.file.whenMerged block. You’ll find examples how to that in the Buildship 1.0.18 release announcement.

Donát,
thank you for your response!

So I tried to use the eclipse.classpath.file.whenMerged block to reorder some classpath entries:

eclipse {
	classpath { 
		defaultOutputDir = file('war/WEB-INF/classes')
		file {
			whenMerged {
					def gwtClasspath = entries.find { it.path == 'com.gwtplugins.gwt.eclipse.core.GWT_CONTAINER' }
					entries.remove gwtClasspath
					entries += gwtClasspath
			}
		}
	}
}

However, that doesn’t change the generated .classpath file.

But then, when leaving out the line entries += gwtClasspath line, the classpath entry gets removed. But how can I reorder classpath entries?

Thanks,

Robert

I tried reordering the classpath entries:

		whenMerged {
				entries.sort { it.path }
				entries.reverse(true)
		}

This worked, this is the result:

<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
<classpathentry kind="con" path="com.gwtplugins.gwt.eclipse.core.GWT_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer">
	<attributes>
		<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
	</attributes>
</classpathentry>
<classpathentry kind="output" path="war/WEB-INF/classes"/>

So the reordering seems to work, but <classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"> will still end up last. Is there any way to get an entry behind this one?

Buildship appends the Gradle classpath container to the end of the classpath if it’s not defined in the eclipse plugin configuration. The following snippet fixes this behavior:

eclipse {
   classpath {
        containers 'org.eclipse.buildship.core.gradleclasspathcontainer'
   }
}
2 Likes

Thank you, Donát. That’s exactly was I was looking for :slight_smile: