Creating a runnable JAR with Gradle

So far I have created executable JAR files using the Eclipse Export ... function, but now I have switched to IntelliJ IDEA and Gradle to automate the build.

Some articles suggest an "application plugin," but this does not completely lead to the expected result (just a JAR, no start scripts, or the like).

How can I achieve the same result as Eclipse with the Export ... dialog?

+117
java jar gradle
Feb 12 '14 at 7:15
source share
9 answers

The jar executable is just the jar file containing the Main-Class entry in the manifest. Therefore, you just need to configure the jar task to add this entry to your manifest:

jar { manifest { attributes 'Main-Class': 'com.foo.bar.MainClass' } } 

You may also need to add class entries to the manifest, but it will do the same.

See http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html

+135
Feb 12 '14 at 7:26
source share

The answers of JB Nizet and Jorge_B are correct.

In its simplest form, creating an executable JAR with Gradle is just a matter of adding the appropriate entries to manifest . However, it is much more common to have dependencies that need to be included in the class path, making this approach difficult in practice.

The application plugin provides an alternative approach; instead of creating an executable JAR, it provides:

  • a run task that facilitates simple application execution directly from the assembly
  • a installDist task that generates a directory structure, including an embedded JAR, all the JARs it depends on, and running a script that integrates all of this into a program that you can run
  • distZip and distTar tasks that create archives containing the complete application distribution distTar (startup scripts and JARs)

The third approach is to create a so-called "thick JAR", which is an executable JAR that includes not only your component code, but all its dependencies. There are several different plugins that use this approach. I have included links to a few that I know of; I am sure that there are more of them.

+88
Feb 13 '14 at 1:33
source share

As others noted, in order for the jar file to be executable, the entry point of the application must be set in the Main-Class attribute of the manifest file. If dependency class files are not placed together, they must be specified in the Class-Path entry of the manifest file.

I tried all kinds of combinations of plugins and that’s not for the simple task of creating an executable jar and anyway, including dependencies. It seems that all the plugins are somehow lacking, but finally I got it the way I wanted. No cryptic scripts, not a million different mini files that pollute the build directory, a pretty clean build script file, and most importantly: not a million third-party class files merged into my jar archive.

The following is a copy-paste from here for your convenience ..

[How-to] create a distribution zip file with jar dependencies in the /lib subdirectory and add all the dependencies to the Class-Path entry in the manifest file:

 apply plugin: 'java' apply plugin: 'java-library-distribution' repositories { mavenCentral() } dependencies { compile 'org.apache.commons:commons-lang3:3.3.2' } // Task "distZip" added by plugin "java-library-distribution": distZip.shouldRunAfter(build) jar { // Keep jar clean: exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA', 'META-INF/*.MF' manifest { attributes 'Main-Class': 'com.somepackage.MainClass', 'Class-Path': configurations.runtime.files.collect { "lib/$it.name" }.join(' ') } // How-to add class path: // https://stackoverflow.com/questions/22659463/add-classpath-in-manifest-using-gradle // https://gist.github.com/simon04/6865179 } 

Hosting is the essence here .

The result can be found in build/distributions and the unzipped content is as follows:

Lib / Common-lang3-3.3.2.jar
MyJarFile.jar

The content of MyJarFile.jar#META-INF/MANIFEST.mf :

Manifest Version: 1.0
Main class: com.somepackage.MainClass
Class Path: lib / commons-lang3-3.3.2.jar

+31
Mar 03 '15 at 6:23
source share

The smallest solution for me was to use the gradle-shadow-plugin plugin

In addition to using the plugin, all you need to do is:

Configure the jar task to put your main class in the manifest

 jar { manifest { attributes 'Main-Class': 'com.my.app.Main' } } 

Run the Gradle task

 ./gradlew shadowJar 

Take app-version-all.jar from the / libs / assembly

And finally, do this through:

 java -jar app-version-all.jar 
+21
Sep 09 '15 at 7:38
source share

Have you tried the 'installApp' task? Does this not create a complete directory with a set of startup scripts?

http://www.gradle.org/docs/current/userguide/application_plugin.html

+5
Feb 12 '14 at 7:18
source share

Thank you, Konstantin, it worked like a charm with a few nuances. For some reason, specifying the main class as part of the jar manifest did not quite work, and instead, it needed the mainClassName attribute. Here is a snippet from build.gradle that includes everything to make it work:

 plugins { id 'java' id 'com.github.johnrengelman.shadow' version '1.2.2' } ... ... apply plugin: 'application' apply plugin: 'com.github.johnrengelman.shadow' ... ... mainClassName = 'com.acme.myapp.MyClassMain' ... ... ... shadowJar { baseName = 'myapp' } 

After running gradle shadowJar, you will get myapp- {version} -all.jar in your build folder, which can be run as java -jar myapp- {version} -all.jar.

+4
Dec 6 '15 at 19:40
source share

You can define the jar artifact in the settings of the module (or project structure).

  • Right-click a module> Open Module Settings> Artifacts> +> JAR> from modules with dependencies.
  • Set the main class.

Creating a can is as simple as clicking "Build artifact ..." in the "Build" menu. As a bonus, you can pack all the dependencies in one jar.

Tested on IntelliJ IDEA 14 Ultimate.

+3
Feb 08 '15 at 13:04 on
source share

I checked quite some links for the solution, finally took the following steps to get it working. I am using Gradle 2.9.

Make the following changes to the build file: gradle:

  • Mentioning plugin:

     apply plugin: 'eu.appsatori.fatjar' 
  • Provide Buildscript:

     buildscript { repositories { jcenter() } dependencies { classpath "eu.appsatori:gradle-fatjar-plugin:0.3" } } 
  • Provide the main class:

     fatJar { classifier 'fat' manifest { attributes 'Main-Class': 'my.project.core.MyMainClass' } exclude 'META-INF/*.DSA', 'META-INF/*.RSA', 'META-INF/*.SF' } 
  • Create fatjar:

     ./gradlew clean fatjar 
  • Run fatjar from / build / libs /:

     java -jar MyFatJar.jar 
+2
Feb 03 '16 at 22:45
source share

You can also easily use the terminal to create a .jar file! For the gradle project you can:

$ cd /into/your/project/directory/

$ gradle build #so gradle builds jar file (s):

$ java -jar build/libs/mymodule-0.0.1-SNAPSHOT.jar # run a separate jar! java

$ java -jar build/libs/mymodule-0.0.1-SNAPSHOT.jar.original # part of the real meat - just in case!

-2
Sep 24 '16 at 10:44
source share



All Articles