包含gradle依赖关系的intellij构建jar工件 [英] intellij build jar artifact containing gradle dependencies

查看:283
本文介绍了包含gradle依赖关系的intellij构建jar工件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上想做些简单的事情-或者至少我认为它应该很简单。


我的目标是创建Intellij gradle项目,并使用gradle将一些依赖项添加到模块中然后添加一些Java源代码。


然后,我只想拥有一个选项,可以将整个内容编译到1个jar中,其中包含所有等级依赖项,并且可以使用 java -jar


但是事实证明,这并不是我想象的那么容易。


我刚刚从intellij创建了一个新的gradle项目并添加了一个Main class。


我将为您提供有关我的文件的概述:


settings.gradle:

  rootProject.name ='gradleTestNewJar'

build.gradle:

 应用插件: java 
Apply插件: idea
apply插件: application

sourceCompatibility = 1.6
版本='1.0'

存储库{
mavenCentral()
}

mainCl assName = com.randomPackage.StarterClass

依赖项{

编译'org.seleniumhq.selenium:selenium-java:2.46.0'

test编译组:'junit',名称:'junit',版本:'4.11'
}

主类:

  package com.randomPackage; 

import com.gargoylesoftware.htmlunit.BrowserVersion;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

公共类StarterClass {

公共静态void main(String [] args){
System.out.println( test));

WebDriver驱动程序=新的HtmlUnitDriver(BrowserVersion.FIREFOX_38);
driver.quit();

}
}

MyStart的主要方法是通过调试从Intellij运行时执行。
因此,当所有依赖项都正确加载后,它就可以工作。


注意:如果有任何区别,我会使用Intellij Community Edition。


我试过的:


1。我试图只使用 gradlew clean build。


这创建了一个jar,但没有库。
但是我没想到它会像这样简单。


2。我尝试按照此处的建议构建模块的构件:


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


I尝试使用提取的而不是提取的依赖项。
在两种情况下,依赖项都添加到了jar中,但是它们却添加到了jar的根中。
当我尝试通过 java -jar运行jar文件时,它抱怨:

 线程中的异常。主要 java.lang.NoClassDefFoundError:org / openqa / selenium / WebDriver 
...

确定,因此它无法加载依赖项。

注意:我认为依赖项没有添加到类路径,但我对此不确定。但是,我希望Intellij将依赖项添加到类路径(或在清单文件中声明)


3。我还尝试使用gradle应用程序插件。


但是,这会创建一个zip / tar,其中包含执行脚本和一个bin文件夹,这不是我的意图。


所以我开始了几个小时的搜寻,但是我找不到解决我的问题的方法。


在这上很难做到这一点-它是如此基础。 / p>

我确信某些天才可以帮助我,并指出我的-可能是愚蠢的-失败。

解决方案

我当前的解决方案如下:



我使用gradle来构建一个包含所有库的jar,我通过名为fatJar的自定义任务来完成此操作。



这是我的build.gradle的一部分

 应用插件:'java '

jar {
manifest {
properties( Manifest-Version: 1.0,
Main-Class: com.randomPackage.MainClass );
}
}

任务fatJar(type:Jar){
manifest.from jar.manifest
classifier ='all'
from {
configuration.runtime.collect {it.isDirectory()吗?它:zipTree(it)}
} {
排除 META-INF / *。SF
排除 META-INF / *。DSA
排除 META-INF /*.RSA
}
with jar
}

然后,我只需在命令行上执行 gradle fatJar并获得一个完美的jar。


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.

解决方案

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.

这篇关于包含gradle依赖关系的intellij构建jar工件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆