Home  >  Gradle

Java Project + Gradle + Eclipse Integration Example

By Arvind Rai, July 26, 2014
In this page, we will learn using Gradle in our project from scratch. We will see how to install plugin in eclipse for Gradle and installation of Gradle binary in OS. We will see how to write Gradle script to create JAR and WAR. We will run the Gradle command to configure classpath in eclipse. We will see all these step by step.

Software Dependency

In our demo to run a simple java program building with Gradle , we are using below softwares.
1. JDK 6
2. Eclipse Juno
3. Gradle-2.0

Integrate Gradle Plugin in eclipse

To integrate Gradle in eclipse go to Help->Eclipse Marketplace and search for Gradle. Install Gradle Integration for Eclispe. Find the below figure.
Java Project + Gradle + Eclipse Integration Example
Select the required software and click next.
Java Project + Gradle + Eclipse Integration Example
Follow all the steps, restart the eclipse and go to Window -> Preferences . Search for Gradle, if found it means you have successfully installed the Gradle plugin in eclipse.
Java Project + Gradle + Eclipse Integration Example

Install Gradle Binary in OS

You also need to download Gradle binary and install into your system. Find the Gradle binary from below location.
Download Gradle
After installation, set path variable for bin location in system environment.

Write a Gradle Script for War

Create a java project and create directory structure as below and set src as below.
1. src/main/java
2. src/main/resources
3. src/main/webapp
Find the eclipse structure.
Java Project + Gradle + Eclipse Integration Example
In the root directory of project, create build.gradle . And write below script.
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
archivesBaseName = 'Concretepage'
version = '1.0-SNAPSHOT' 
repositories {
    mavenCentral()
}
dependencies {
   compile  'org.apache.commons:commons-lang3:3.0'
   compile  'log4j:log4j:1.2.16'

} 
To create WAR, we need to write below command in our script.
apply plugin: 'war' 
You can set your jar repository. BY default, Gradle picks jar from Maven Central.
repositories {
    mavenCentral()
} 
Suppose we need to provide common- lang and log4j dependency in our project. Then we add below code in our Gradle script.
dependencies {
   compile  'org.apache.commons:commons-lang3:3.0'
   compile  'log4j:log4j:1.2.16'

} 
To run the build.gradle, open the command prompt and reach to the root directory of your project. And run the below command as below.
gradle clean build
Java Project + Gradle + Eclipse Integration Example

Configure Java Project Classpath in Eclipse Using Gradle

Gradle provides command to configure classpath of the project for all jar dependency. In the Gradle script the below line is required.
apply plugin: 'eclipse' 
Now open the command prompt and reach to the root directory of the project and run the command. gradle eclipse
Java Project + Gradle + Eclipse Integration Example
Check the project .classpath and you will see that all dependency JAR has been configured.

Create a Java Simple Program in Eclipse

Now we will create a simple java program to test. We are using common lang and log 4j class to test our gradle build.
Test.java
package com.concretepage;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
public class Test {
  public static void main(String[] args) {
	 Logger logger = Logger.getLogger(Test.class);
	 logger.debug("Starting demo");
	 String s = "Some Value"; 
	 if(!StringUtils.isEmpty(s)){
		 System.out.println("Welcome ");
	 }
	 logger.debug("End of demo");
  }
} 

Write a Gradle Script for Jar

To create a JAR you can use below Gradle script.
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'Concretepage'
version = '1.0-SNAPSHOT' 
repositories {
    mavenCentral()
}
jar {
	manifest {
		attributes 'Main-Class': 'com.concretepage.Test'
	}
}
dependencies {
   compile  'org.apache.commons:commons-lang3:3.0'
   compile  'log4j:log4j:1.2.16'

} 

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS





Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us