Why does my jar file not not contain any class files?

旧街凉风 提交于 2020-04-11 04:35:49

问题


I'm trying to add a task (gen or gen2) to my build.gradle that does exactly the same as the Jar-task:

version = "0.0.1"
apply plugin: 'java'

task('gen', type: Jar) {
}

task gen2(type: Jar)

Running

gradle jar

generates a JAR-file that contains .class-files, while running

gradle gen

or

gradle gen2

generate a JAR-file that does NOT contain any .class-files.

Whats wrong with my class definition?


回答1:


To build a jar with all the classes from main, as a default jar task would, do this:

task gen2(type: Jar){
    baseName = 'gen2Jar'
    from sourceSets.main.output
}

You can also do from(sourceSets.main.output){ include "package" } to customize what packages are included.

Alternatively, to copy settings from the default jar task:

task gen(type: Jar){
    baseName = 'genJar'
    with jar
}

Infact you can have both of these in the same build.gradle. Running gradle jar builds default jar. gradle gen builds genJar.jar and gradle gen2 builds gen2Jar.jar, all of which contain all the classes from java.main



来源:https://stackoverflow.com/questions/35919372/why-does-my-jar-file-not-not-contain-any-class-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!