Create Eclipse projects with the usage of Gradle

Why using a build tool, to create Eclipse projects files, which are only created once? When you are working in a distributed team, with multiple developers on different platforms (like Linux, Windows or Mac OS X), you want them to create their own Eclipse project files, in their environment – and you don’t want these files to be shared in your version control system. But you really want, that the use the same folder and project structure, with the same external libs (in maybe different locations).

It could also make sense, to recreate the project files, when you’ve added some new libs to your project – which you not want to share via your VCS system.

What do you need to do, to get Gradle to support you.

Gradle is using different plugins, which are included into you build file by the usage of the keyword apply. And what a surprise, there is a plugin for Eclipse and for the reason that want to do a Java project, we also use this plugin.

build.gradle:

apply plugin: 'java'
apply plugin: 'eclipse'

With the command gradle eclipse, we create the Eclipse project files.

Results:

- .classpath
- .project
- .settings
  - org.eclipse.jdt.core.prefs

.classpath

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
	<classpathentry kind="output" path="bin"/>
	<classpathentry kind="src" path="src/main/java"/>
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER" exported="true"/>
</classpath>

What is directly working out of the box, is the usage of the source directory, src/main/java. The output folder is not following the normal Gradle folder structure (maybe someone wanted that, but with the usage of Ant, I always used the same folder structures for Eclipse build projects and Ant projects), which would be normally build/classes/main instead of bin.

But as said before, you can nearly customize everything in Gradle. So lets change the output folder in this case.

build.gradle with defined output folder:

apply plugin: 'java'
apply plugin: 'eclipse'

eclipse {
  classpath {
    defaultOutputDir = compileJava.destinationDir
  }
}

With the keyword classpath the target file is described, and with defaultOutputDir the output-tag in the .classpath file will be changed. compileJava is a object, which was included by the Java plugin, and finally the attribute destinationDir contains the output folder, used by Gradle.

Again the command gradle eclipse will update the files. But stop, you should do a cleanup before, done with gradle cleanEclipse, otherwise you will get some values doubled (in this case the output), because Gradle tries to update the files, instead of overwritting them.

.classpath with customized output

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
	<classpathentry kind="src" path="src/main/java"/>
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER" exported="true"/>
	<classpathentry kind="output" path="build/classes/main"/>
</classpath>

As a last step in this example, a name is defined for the Eclipse project and an external lib (Java Mail) will be included.

build.gradle enhanced:

apply plugin: 'java'
apply plugin: 'eclipse'

repositories {
	mavenCentral()
}

configurations {
    mail
}

dependencies {
    mail 'javax.mail:mail:1.4.6'
}

eclipse {
  project {
    name = 'MyMailExample'
  }

  classpath {
    plusConfigurations += configurations.mail

    defaultOutputDir = compileJava.destinationDir
  }
}

How to include a external lib into Gradle is described here. Newly added is here the part project { } and the more important external lib plusConfiguration += configurations.mail. With the part configurations, the related section is be linked, and with mail it is defined, which part of the configuration should be added. This lib part will be added to the .classpath.

.classpath with external lib:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
	<classpathentry kind="src" path="src/main/java"/>
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER" exported="true"/>
	<classpathentry kind="output" path="build/classes/main"/>
	<classpathentry sourcepath="/Users/example/.gradle/caches/artifacts-23/filestore/javax.mail/mail/1.4.6/source/8ddcd26291a284821bc93a342733bea80cc65db7/mail-1.4.6-sources.jar" 
             kind="lib" path="/Users/example/.gradle/caches/artifacts-23/filestore/javax.mail/mail/1.4.6/jar/383bdbd7b4bc3e7960370db219f883b4062fc1ca/mail-1.4.6.jar" exported="true"/>
	<classpathentry sourcepath="/Users/example/.gradle/caches/artifacts-23/filestore/javax.activation/activation/1.1/source/3697e9f67a2ed7ac5f589cff30f21173299440/activation-1.1-sources.jar" 
             kind="lib" path="/Users/example/.gradle/caches/artifacts-23/filestore/javax.activation/activation/1.1/jar/e6cb541461c2834bdea3eb920f1884d1eb508b50/activation-1.1.jar" exported="true"/>
</classpath>

.project with a new project name:

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
	<name>MyMailExample</name>
	<comment/>
	<projects/>
	<natures>
		<nature>org.eclipse.jdt.core.javanature</nature>
	</natures>
	<buildSpec>
		<buildCommand>
			<name>org.eclipse.jdt.core.javabuilder</name>
			<arguments/>
		</buildCommand>
	</buildSpec>
	<linkedResources/>
</projectDescription>

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.