One Gradle Plugin to JAR Them All
Learn about the One-JAR, a Gradle plugin that helps combine various elements of your code into a single JAR.
Join the DZone community and get the full member experience.
Join For FreeScenario
You have to pack a standalone application into one JAR that will contain your custom code and also all of the third-party libraries the code needs to function properly. You are using Gradle for your build scripts. The solution to this task has two steps.
Steps
Create a Gradle build that will compile and package your standalone application. The solution to this first problem it is easy if you are using Eclipse + Gradle Buildship plugin. After installing the plugin, you will be able to create a Gradle project from Eclipse with a generic build.gradle script.
Note: The Buildship plugin is an Eclipse project, and it is useful if one wants to run Gradle commands from inside Eclipse. For instance, when a new task has been added to the build script, that task is automatically visible in the Gradle Tasks view in Eclipse.
Now, you'll have to add your project dependencies to be managed through Gradle. The build.gradle script has a dependencies section where you can declare what libraries is your code using, like this:
dependencies {
compile group: 'org.springframework', name: 'spring-core', version: '4.3.2.RELEASE'
compile group: 'org.springframework', name: 'spring-beans', version: '4.3.2.RELEASE'
...
}
Note: Usually, you can find the Gradle notation for your third-party libraries on the MvnRepository.
For Gradle to find your dependencies, you'll also have to specify the Maven repository where they are located, in this case, the MvnRepository.
Add to your build.gradle the following:
repositories {
mavenCentral()
//some other maven repositories here
}
After you add all your dependencies you can compile your project to see if everything is OK. From the console run the following :
gradle compileJava
After compiling your project you can create a JAR from the console using:
gradle jar
Second step: For this task to succeed you'll need a specific Gradle plugin that knows how to do this.
This plugin is the One-JAR plugin that can be found here.
Add the following to you build.gradle:
apply plugin: 'gradle-one-jar'//this section contains declarations for the build script run.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.github.rholder:gradle-one-jar:1.0.4'
}
}
Then create a custom task to pack everything in one jar.
task createFatJar(type: OneJar) {
mainClass = 'com.mycompany.myawesomeapp.MyAppMainClass'
archiveName = 'myawesomeapp-all.jar'
targetConfiguration = configurations.fatJarBuild
}
Now, you can see above in the createFatJar definition that there is a reference to a configuration named fatJarBuild. This configuration will contain all the third-party libraries that will be included in the final fat jar.
Add fatJarBuild configuration to the dependencies section, like this:
dependencies {
......
// dependencies to be included in the final fat jar
fatJarBuild group: 'org.springframework', name: 'spring-core', version: '4.3.2.RELEASE'
fatJarBuild group: 'org.springframework', name: 'spring-beans', version: '4.3.2.RELEASE'//add all your dependencies here
}
Now you can test the newly createFatjar task with:
gradle createFatJar
If everything is OK, a new JAR named myawesomeapp-all.jar will be created in your current folder in a subfolder, like this: /my-current-project-folder/build/libs/myawesomeapp-all.jar.
If you run the JAR with java -jar myawesomeapp-all.jar, your code should work.
The content of myawesomeapp-all.jar will be the following:
In the example above, main.jar is the JAR that contains your custom code.
Note: This article is using Eclipse and the Buildship Gradle plugin. You do not necessarily need to use Eclipse, nor the Buildship plugin, to integrate the One-JAR plugin, nor to generate an Eclipse project from Gradle.
Gradle has its own Eclipse plugin, through which you can generate an Eclipse project. All other commands from this article are done from the console.
The One-Jar Plugin and Disabling Logging
Once you are using the One-JAR plugin, you'll notice that it logs everything. To disable the logging, you'll have to create a one-jar.properties file to be included in the root of your fat JAR.
The content of the one-jar.properties is only one line:
one-jar.silent=true
Now, you'll have to change the createFatJar task to also include one-jar.properties into your final fat JAR.
task createJar(type: OneJar) {
copy {
from sourceSets.main.resources.srcDirs
into'build/one-jar-build'
include 'one-jar.properties'
}
........
}
For the above code to work, the assumption is that you put the one-jar.properties inside your projects folder in sub-folder like this : ../myawesomeapp/src/main/resources/one-jar.properties.
Every time the One-JAR plugin runs, it will create a structure of folders like this:
../build/one-jar-build
../build/libs/
and others
That's all about the awesome One-JAR plugin. Happy building!
Opinions expressed by DZone contributors are their own.
Trending
-
10 Traits That Separate the Best Devs From the Crowd
-
Design Patterns for Microservices: Ambassador, Anti-Corruption Layer, and Backends for Frontends
-
What Is Envoy Proxy?
-
Azure Virtual Machines
Comments