How to make defaultOutputDir point to a linked directory?


(Luke U) #1

Our build environment (transitioning from Maven) places all generated output to a location outside the project directory. (In Gradle, each project defines this location as ${buildDir}.)

With source folders I was happily surprised that Buildship detects it and creates a linked directory in Eclipse to add to the source-path (e.g. for generated-source directories) and this all works very smoothly.

However it doesn’t seem to do the same with the output directories.

Just to confirm, a relative defaultOutputDir works as expected:

eclipse.classpath.defaultOutputDir = file('target/eclipseClasses')

But when making the “obvious” change to an absolute path, the auto-magic doesn’t happen and we end up with a munged path (still relative to the project) in Eclipse:

eclipse.classpath.defaultOutputDir = file("${buildDir}/eclipseClasses")

To create an “expected result” I edited the projects via the Eclipse UI. I then tried to follow the Gradle docs to define something that generates it. It seems (on the surface) that something like this should work:

eclipse {
    pathVariables 'PROJECT_BUILD_DIR': file("${buildDir}")
    project {
        linkedResource name: 'projBuildDir', type: '2', locationUri: 'PROJECT_BUILD_DIR'
    }
    classpath {
        defaultOutputDir = file('projBuildDir')
    }
}

However I see the following issues:

  1. the path variables do not show up in the .project file at all
  2. the linked resource ‘projBuildDir’ does not get generated
  3. the linked resource that BuildShip nicely created for my generated-source directories disappear! (See antlr in the example below)
  4. the classpath entries for the generated-source directories disappear from the .classpath file

Expected .project file:

<projectDescription>
<name>my-project</name>
<comment>Project my-project created by Buildship.</comment>
<projects>
</projects>
<buildSpec>
	<buildCommand>
		<name>org.eclipse.jdt.core.javabuilder</name>
		<arguments>
		</arguments>
	</buildCommand>
</buildSpec>
<natures>
	<nature>org.eclipse.jdt.core.javanature</nature>
	<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
<linkedResources>
	<link>
		<name>antlr</name>
		<type>2</type>
		<location>T:/build/my-project/target/generated-sources/antlr</location>
	</link>
	<link>
		<name>projBuildDir</name>
		<type>2</type>
		<locationURI>PROJECT_BUILD_DIR</locationURI>
	</link>
</linkedResources>
<variableList>
	<variable>
		<name>PROJECT_BUILD_DIR</name>
		<value>file:/T:/build/my-project</value>
	</variable>
</variableList>
</projectDescription>

Observed .project file:

<projectDescription>
<name>my-project</name>
<comment>Project my-project created by Buildship.</comment>
<projects>
</projects>
<buildSpec>
	<buildCommand>
		<name>org.eclipse.jdt.core.javabuilder</name>
		<arguments>
		</arguments>
	</buildCommand>
</buildSpec>
<natures>
	<nature>org.eclipse.jdt.core.javanature</nature>
	<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
</projectDescription>

More detail:

  • To regenerate these project files, I normally zap them all (e.g. with gradlew cleanEclipse) and then inside Eclipse right-click > Gradle > Refresh Gradle Project.
  • (interestingly doing gradlew eclipseProject resolves issue 2. But the others still fail.)

I feel I’m missing something important in my knowledge of how this all works or fits together - or some Gradle internals are clobbering each other.

Any insights, anyone?

Versions used:

  • Gradle 3.4.1
  • Buildship 2.0.1
  • Eclipse Neon.3