intellij build jar artifact containing gradle depe

2020-08-14 07:37发布

I basically want to do something simple - or atleast i 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 have an option to somehow compile the whole thing into 1 jar, containing all grade dependencies and being able to execute using "java -jar"

However it turned out that this is not as easy is i had thought.

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

I´ll give you an overview over 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 of "MyStart" is executed when running from Intellij via debug. So it works, when all dependencies get loaded correctly.

NOTE: I use Intellij Community Edition if this makes any difference.

What i tried:

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

This created a jar, but without libs. But I didn´t expect it to be as easy as this.

2. I tried to build an artifact of the module 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 the dependencies were added into the jar, but they were added to the root of the jar. When i tried to run the jar file via "java -jar", it complained:

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

OK, so it couldn´t load the dependencies.
NOTE: I thought that the dependencies were not added to the classpath, but i am not sure about this. However, i would expect Intellij to add dependencies to the classpath( or declare in the manifest file)

3. I also tried to use the gradle application plugin.

However this creates a zip/tar which contains a execute script and a bin folder which was not my intention.

So i started googling for hours and hours but i cann´t find a solution to my problem.

Come on this cannot be so hard - it is just so basic.

I am sure some genius can help me out and point me to my - probably stupid - failure.

1条回答
贪生不怕死
2楼-- · 2020-08-14 07:46

My current solution is as follows:

I use gradle to build a jar containing all libs, I do this witha custom task called fatJar.

Here is a part from 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 execute "gradle fatJar" on the command line and get a perfect jar.

查看更多
登录 后发表回答