Templates by BIGtheme NET

Gradle to create jar file using eclipse

In this article, I will show How to create jar file using Graddle.
How to test the same jar file.

Tools Used :

1) eclipse version Luna 4.4.1.
2) Graddle 2.11
3) JDK 1.8

Simple Steps to be followed,

1) Create a Gradle project.

2) Modify the build.gradle file.

3) Add some java source and test classes.

4) Run and verify the jar file is created.

1) Create a Gradle project :

If you are completely new to Graddle and eclipse,
Pl. refer the link Graddle Hello World Using eclipse

I have created a Graddle project with name GraddleJarEmail.

1

2) Modify the build.gradle file :

Add jar file, versions and compatible version.

sourceCompatibility = 1.8
version = '1.0'
jar {
	manifest {
		attributes 'Implementation-Title': 'emailService',
				   'Implementation-Version': version
	}
}

Then the jar will be created once we run the Graddle build,
with name emailService.jar.

build.gradle

/*
 * This build file was auto generated by running the Gradle 'init' task
 * by 'nallave' at '11/2/16 8:08 PM' with Gradle 2.11
 *
 * This generated file contains a sample Java project to get you started.
 * For more details take a look at the Java Quickstart chapter in the Gradle
 * user guide available at https://docs.gradle.org/2.11/userguide/tutorial_java_projects.html
 */

// Apply the java plugin to add support for Java
apply plugin: 'java'
apply plugin: 'eclipse'

sourceCompatibility = 1.8
version = '1.0'
jar {
	manifest {
		attributes 'Implementation-Title': 'emailService',
				   'Implementation-Version': version
	}
}

// In this section you declare where to find the dependencies of your project
repositories {
    // Use 'jcenter' for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    mavenCentral()
}

// In this section you declare the dependencies for your production and test code
dependencies {
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
    testCompile group: 'junit', name: 'junit', version: '4.+'
}

3) Add some java source and test classes :

I have added simple java email service class, having simple method send(),
to send email to the toList users. Test class TestEmailService just to test the send() method.

EmailService.java

package com.devjavasource.graddle;

import java.util.List;

public class EmailService {
    
	String send(){
    	return "Send email";
    }
    
    private String from;
    private List toList;
    private String subject;
    private String body;
    
	public String getFrom() {
		return from;
	}
	public void setFrom(String from) {
		this.from = from;
	}
	public List getToList() {
		return toList;
	}
	public void setToList(List toList) {
		this.toList = toList;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getBody() {
		return body;
	}
	public void setBody(String body) {
		this.body = body;
	}
}

TestEmailService.java

package com.devjavasource.graddle;

import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class TestEmailService {
	@Test public void testSend() {
		EmailService email = new EmailService();
        assertEquals("Send email", email.send());
    }
}

4) Run and verify the jar file is created :

We can either run from eclipse or command prompt.
Command to run Gradle build is Gradle build
2

We can run from eclipse,

Right click on Gradle.build file and Run As -> gradle build Gradle build

3

The out will be generated,

Out Put :

:compileJava
:processResources UP-TO-DATE
:classes
:jar
:assemble
:compileTestJava
:processTestResources UP-TO-DATE
:testClasses
:test
:check
:build

BUILD SUCCESSFUL

Total time: 1.68 secs

You can see the build results in build folder.
In side the build folder, you can see the jar file.

4

You can download complete project, Here
GraddleJarEmail

*** Venkat – Happy learning ****