13

我有兴趣在一个可执行的 jar 文件中构建一个包含所有模块依赖项和外部 jar 的单个 jar,我将能够使用java -jar myApp.jar.

我有依赖于模块 B 的模块 A。目前我正在使用 gradle,我的build.gradle脚本如下所示:

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

当我通过 gradle 命令构建它时:clean build fatjar按预期创建了一个胖 jar 'A.jar'。但是按照我上面写的那样运行它会导致: no main manifest attribute, in A.jar 如何修改我的build.gradle文件并指定主类或清单?

4

2 回答 2

17

我自己弄清楚了:我使用了 uberjar Gradle 任务。现在我的 build.gradle 文件如下所示:

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"
}

现在我将它与命令一起使用:

清洁构建 uberjar

它构建了一个不错的可运行 jar :)

于 2014-07-02T07:12:16.090 回答
5

为了使用 fatjar 让它工作,我在 fatJar 任务中添加了一个清单部分:

task fatJar(type: Jar) {
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version
        attributes 'Main-Class': 'com.organization.project.package.mainClassName'
    }
}
于 2014-09-12T10:01:05.957 回答