Intellij build jar artifact containing gradle dependencies

I basically want to do something simple - or at least think it should be pretty simple.

My goal is to create an Intellij gradle project, add some dependencies to the module using gradle, and add some java source code to it.

Then I just want to be able to somehow compile it all into 1 jar containing all the class dependencies and being able to execute using "java -jar"

However, it turned out to be not so easy, I thought.

I just created a new gradle project from intellij and added a main class.

I will tell you about my files:

settings.gradle:

rootProject.name = 'gradleTestNewJar' 

build.gradle:

 apply plugin: 'java' apply plugin: 'idea' apply plugin: 'application' sourceCompatibility = 1.6 version = '1.0' repositories { mavenCentral() } mainClassName = "com.randomPackage.StarterClass" dependencies { compile 'org.seleniumhq.selenium:selenium-java:2.46.0' testCompile group: 'junit', name: 'junit', version: '4.11' } 

main class:

 package com.randomPackage; import com.gargoylesoftware.htmlunit.BrowserVersion; import org.openqa.selenium.WebDriver; import org.openqa.selenium.htmlunit.HtmlUnitDriver; public class StarterClass { public static void main(String[] args){ System.out.println("test"); WebDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_38); driver.quit(); } } 

The main method "MyStart" is executed when starting from Intellij via debug. Thus, it works when all dependencies load correctly.

NOTE. I use Intellij Community Edition if that matters.

What I tried:

1. I tried to just use a "clean gradlew build".

This created a jar, but without libs. But I did not expect it to be that simple.

2. I tried to build a module artifact as suggested here:

http://blog.jetbrains.com/idea/2010/08/quickly-create-jar-artifact/

I tried it with extracted and not extracted dependencies. In both cases, dependencies were added to the jar, but they were added to the root of the jar. When I tried to run the jar file through "java -jar", it complained:

 "Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver ..." 

OK, so it could not load dependencies.
NOTE. I thought that dependencies were not added to the classpath, but I'm not sure about that. However, I would expect Intellij to add dependencies to the class path (or declare in the manifest file)

3. I also tried using the gradle application plugin.

However, this creates a zip / tar that contains the script executable and the bin folder, which was not my goal.

So, I started working on the Internet for hours and hours, but I can not find a solution to my problem.

Come on, it can't be that hard - it's just that.

I'm sure some genius can help me and point me to my, possibly stupid failure.

+7
java intellij-idea jar build gradle
source share
1 answer

My current solution is as follows:

I use gradle to build a flag containing all the libraries, I do this with a custom task called fatJar.

Here is part of my build.gradle

 apply plugin: 'java' jar { manifest { attributes("Manifest-Version": "1.0", "Main-Class": "com.randomPackage.MainClass"); } } task fatJar(type: Jar) { manifest.from jar.manifest classifier = 'all' from { configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) } } { exclude "META-INF/*.SF" exclude "META-INF/*.DSA" exclude "META-INF/*.RSA" } with jar } 

Then I just run "gradle fatJar" on the command line and get the perfect jar.

+7
source share

All Articles