This post will show you how we can compile and package a simple Java Project using Gradle into a runnable JAR file

Project Layout 


The default project layout of a Java project with Gradle is following (just like Maven):

- The src/main/java directory contains the source code.
- The src/main/resources directory contains the resources (such as properties files).
- The src/test/java directory contains the test classes.
- The src/test/resources directory contains the test resources.  

So now make a new folder where you would like the project to start:

mkdir runnablejar
cd runnablejar  


Let’s create the project layout by using the following commands:

mkdir -p src/main/java
mkdir -p src/main/resources
mkdir -p src/test/java
mkdir -p src/test/resources  

As you can see from above the project layout looks just like a Maven project.

Creating the Main Class 


Let’s create the main class for this project and make it display “Hello World” cd into src/main/java and create the package layout.

mkdir -p com/johnathanmarksmith/gradle  

We have the project layout created and also the package layout so now it’s time to create the Main Class

vi com/johnathanmarksmith/gradle/HelloWorld.java  

and insert the following lines:

package com.johnathanmarksmith.gradle;
public class HelloWorld
{
    public static void main(String[] args) 
    { 
        System.out.println("Hello World!"); 
    } 
}   

Creating the Gradle Build File.. 


The next step is to create the Gralde Build file, Go back to the root of the project. 

cd ../../.. And create the build file

vi build.gradle   

Now add the following lines:

 apply plugin: 'java' 
 
 jar { 
        baseName = 'smith' 
        version = '1.0' 
        manifest { 
                     attributes 'Main-Class': 'com.johnathanmarksmith.gradle.HelloWorld' } 
     }    

Building the project and creating the JAR To build the project and to create the JAR you need to run the following command

gradle build

Now to run the JAR file you enter the following command

java -jar ./build/libs/smith-1.0.jar   
</param></param></param></embed>

If you have any questions or comments please email me at john@johnathanmarksmith.com



Published

04 February 2015

Tags