A Java Gradle Capsule is a single standalone compiled .jar file which contains all the dependencies necessary for running a Java application.
We can follow the steps below for creating a Capsule Template Project.
0. Install NetBeans 8.2 and Gradle Plugin
- Download and install Netbeans from https://netbeans.org/downloads/ . I recommend Java SE or Java EE edition.
- Open Netbeans and install the Gradle plugin by going to Tools -> Plugins -> Available Plugins
1. Create the Capsule Template Project
2. Edit the build.gradle file
- highlighted lines are the new lines added to the default file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
apply plugin: 'java' apply plugin: 'application' sourceCompatibility = '1.8' [compileJava, compileTestJava]*.options*.encoding = 'UTF-8' // NetBeans will automatically add "run" and "debug" tasks relying on the // "mainClass" property. You may however define the property prior executing // tasks by passing a "-PmainClass=<QUALIFIED_CLASS_NAME>" argument. // // Note however, that you may define your own "run" and "debug" task if you // prefer. In this case NetBeans will not add these tasks but you may rely on // your own implementation. //if (!hasProperty('mainClass')) { // ext.mainClass = 'cool.devis.capsule.template.Main' //} mainClassName = 'cool.devis.capsule.template.Main' version = '0.1.0' configurations{ capsule } repositories { mavenCentral() // You may define additional repositories, or even remove "mavenCentral()". // Read more about repositories here: // http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:repositories } dependencies { // TODO: Add dependencies here ... // You can read more about how to add dependency here: // http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:how_to_declare_your_dependencies testCompile group: 'junit', name: 'junit', version: '4.10' capsule 'co.paralleluniverse:capsule:1.0' compile group: 'com.google.guava',name:'guava', version:'22.0' } task capsule(type: Jar,dependsOn: classes){ archiveName='CapsuleTemplate.jar' from jar //embed our application jar from{configurations.runtime} //embed dependencies from(configurations.capsule.collect {zipTree(it)}){ include 'Capsule.class' } // we just need the single Capsule class from(sourceSets.main.resources) manifest{ attributes ( 'Main-Class' : 'Capsule', 'Premain-Class' : 'Capsule', 'Application-Class' : mainClassName, 'Application-Version' : version, 'Min-Java-Version' : '1.8.0', 'JVM-Args' : run.jvmArgs.join(' '), 'System-Properties' : (run.systemProperties).collect { k,v -> '$k=$v'}.join(' '), ) } } |
3. Use the new included Google Guava library
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package cool.devis.capsule.template; import com.google.common.base.Joiner; public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { Joiner joiner=Joiner.on(", "); String message=joiner.join("Hello","Capsule","using Guava"); System.out.println(message); } } |
4. Build the Capsule .jar
- For building the capsule file: right click on the project -> go to Tasks and select the capsule option
- In the output window we can see details about the build process, the path of the compiled capsule file is not shown, but we can find it easily using this formula: [ProjectPath]/build/libs/CapsuleTemplate.jar
- For seeing the capsule in action we can open a terminal/command prompt window and execute the .jar file using the command :
java -jar [CapsuleFilePath]
- The generated capsule file is containing all the necessary dependencies and can be deployed and executed on any systems which supports Java
v0.1.0
To Do in next version:
- Create Bitbucket project and add download link
- Add possibility to rename the TemplateProject and download the renamed version
Hi, I did the same as you did.I am getting an error
“ERROR: Could not get unknown property ‘run’ for object of type org.gradle.api.java.archives.internal.DefaultManifest.”
on the line ” ‘JVM-Args’ : run.jvmArgs.join(‘ ‘),”
I am not understanding what is the problem.
I am using dropwizard version 1.3.9 which is the latest one.