如何使用gradle.build fatjar插件时指定主类 [英] How To Specify main class when using fatjar plugin in gradle.build

查看:2149
本文介绍了如何使用gradle.build fatjar插件时指定主类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感兴趣的是建立包含在一个单一的可执行的JAR文件中的所有模块依赖和外部罐一个罐子,我将能够与 Java的罐子myApp.jar

I am interested in building a single jar containing all the module dependencies and external jars in a single executable jar file which I will be able to run with java -jar myApp.jar.

我有一个模块是依赖模块B.
目前我使用gradle这个,我的的build.gradle 脚本如下:

I have module A which is dependent on module B. Currently I'm using gradle, and my build.gradlescript looks like this:

    apply plugin: 'fatjar'
    description = "A_Project"
    dependencies {
      compile project(':B_Project')
      compile "com.someExternalDependency::3.0"
    }

当我建立它的gradle通过命令:干净构建fatjar 如预期创造了一个胖罐子a.jar文件。
但正如我在上面写的结果运行它:
无主清单属性,在a.jar文件
如何修改我的的build.gradle 文件,并指定主类,或清单?

When I build it through gradle command: clean build fatjar a fat jar 'A.jar' is created as expected. But running it with as I written above results in: no main manifest attribute, in A.jar How can I modify my build.gradle file and specify the main class, or the manifest?

推荐答案

我已经想通了自己:
我用uberjar摇篮任务。
现在我的build.gradle文件看起来像这样:

I have figured it out myself: I've used uberjar Gradle task. now my build.gradle file looks like this:

apply plugin: 'java'
apply plugin: 'application'

mainClassName  = 'com.organization.project.package.mainClassName'

version = '1.0'

task uberjar(type: Jar) {
    from files(sourceSets.main.output.classesDir)
    from {configurations.compile.collect {zipTree(it)}} {
        exclude "META-INF/*.SF"
        exclude "META-INF/*.DSA"
        exclude "META-INF/*.RSA"
}

manifest {
    attributes 'Main-Class': 'com.organization.project.package.mainClassName'
    }
}


dependencies {
compile project(':B_Project')
compile "com.someExternalDependency::3.0"
}

现在我用我的命令使用它:

and now I i use it with the command:

干净构建uberjar

clean build uberjar

和它建立一个很好的运行的JAR:)

and it builds one nice runnable jar :)

这篇关于如何使用gradle.build fatjar插件时指定主类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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