Skip to content

Building a project with eclipse gradle

Phil Callender edited this page Nov 8, 2013 · 4 revisions

Notes screwing around with Eclipse / Gradle

There are a couple of tricks to getting a ToolTwist application building correctly in the Eclipse workbench.

Here is a typical build.gradle file for a ToolTwist extension project:

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

group = 'com.tooltwist'
version = '8.1-SNAPSHOT'
sourceCompatibility = 1.7

repositories {
	ivy {
		url "file:/${localRepo}"
		layout "maven"
	}
	mavenCentral()
}


//----------------------------------------------------------------------
// Gradle lacks the "provided" dependency so we'll have to add it.
// http://blog.codeaholics.org/2012/emulating-mavens-provided-scope-in-gradle/
configurations {
	provided
}
sourceSets {
    main.compileClasspath += configurations.provided
    test.compileClasspath += configurations.provided
    test.runtimeClasspath += configurations.provided
}    
eclipse.classpath.plusConfigurations += configurations.provided
javadoc.classpath=sourceSets.main.compileClasspath

dependencies {
	compile project(':ttWbd')

	provided 'javax:javaee-api:6.0'
	
	compile group: 'javax.mail', name: 'mail', version: '1.4.6'
	compile group: 'ch.qos.logback', name: 'logback-core', version: '0.9.30'
	compile group: 'ch.qos.logback', name: 'logback-classic', version: '0.9.30'
	compile group: 'org.slf4j', name: 'slf4j-api', version: '1.6.2'

    testCompile group: 'junit', name: 'junit', version: '4.+'
}


//----------------------------------------------------------------------
//	Copy extra resources into the jar file
//
processResources {
	from('WebContent')
	into 'build/resources/main/META-INF/resources'
}
processResources.dependsOn("copyConfig")
processResources.dependsOn("copyWidgets")

task copyConfig(type: Copy) {
    from('config')
    into 'build/resources/main/META-INF/myProject/config'
}

task copyWidgets(type: Copy) {
    from('widgets')
    into 'build/resources/main/META-INF/myProject/widgets'
}


//----------------------------------------------------------------------
//	Upload the jar file to the repo
//
uploadArchives {
    repositories {
	ivy {
		url "file:/${localRepo}"
		layout "maven"
	}

    }
}

Things to note:

  1. the apply plugin: 'eclipse' line adds extra tasks to gradle that can be used by Eclipse to update it's classpath.

  2. The group and version definitions describe the project. The name identifier is taken from the current directory name.

  3. The url "file:/${localRepo}" line tells Gradle where you wish to store jar files on your machine. The localRepo property can be set system wide, as seen here:

    $ cat ~/.gradle/gradle.properties localRepo=/Users/philipcallender/Development/gradle-repo

  4. Gradle does not have a provided keyword for dependencies like Maven, to allow a jar to be used while compiling, but not to be installed to production. The most common case for this is the J2EE Servlet API, where we need a jar while compiling, but don't need that jar later because it's supplied by Tomcat.

  5. Where a dependency is another project in the workbench, we want Eclipse to provide the class files so we can modify the code any time and run the debugger. We do this with a dependency declaration like compile project(':filemap') in the build.gradle file. This must be matched up with a line in settings.gradle in the parent directory (i.e. devel). Note that gradle always loads the definitions from all the build.gradle files in all the directories below the settings.gradle file.

    includeFlat 'xdata'
    includeFlat 'filemap'

  6. The processResources task installs the WebContent, widgets, and config directories into the jar file (Notice that myProject should be replaced).

Building from the command line

If you have gradle installed properly, you should be able to compile and install your project using:

gradle uploadArchives

To update the class path in Eclipse, run

gradle eclipse

Building from within the Workbench.

We do not use the Spring STS Gradle plugin. If it is installed, do not use the menu options in Eclipse.

After you've run the command line option mentioned above, Eclipse should just build normally. If you any problems contact Phil.

--

Clone this wiki locally