如何在build.gradle中将生成的文件添加到shadowJar?

时间:2019-01-25 01:05:56

标签: java gradle shadowjar

我正在gradle中使用shadowJar创建2个jar。一个罐子仅包含主文件,另一个罐子是一个胖罐子,其中包含src / main / java和src / main / generate下的所有Java文件。但是,当我运行此jar时,对于src / main / generated下的类,我会收到NoClassDefFoundError。如何实现呢?

我的示例shadowJar任务如下所示:

shadowJar {
    dependsOn compileLib
    from(sourceSets.generated.java.srcDirs[1]) 
    from(sourceSets.main.java.srcDirs[1])
    from { configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) } } 
}

task shadowJar2(type: Jar, dependsOn: shadowJar) {
    baseName = project.name + '-all'
    manifest {
        attributes 'Implementation-Title': project.name,
                   'Implementation-Version': version,
                   'Main-Class': 'XYZ'
    }
    doFirst {
        dependsOn compileLib
        from sourceSets.generated.java.srcDirs[1] include '**/*.class'
        from sourceSets.generated.java.srcDirs[0] include '**/*.class'
        from sourceSets.main.java.srcDirs[1]
        from sourceSets.logback.resources.srcDirs[0] include '**/logback.xml'
        from {configurations.applibs.collect { it.isDirectory() ? it : zipTree(it) } }
        from { configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) } }
    }
}

我尝试了https://github.com/johnrengelman/shadow/issues/138https://github.com/bodiam/epub-organizer/blob/master/epub-organizer-dropwizard/epub-organizer-dropwizard.gradlehttps://objectpartners.com/2013/07/16/creating-self-contained-executable-jars-with-gradle-and-shadow/和其他一些建议。但是我无法解决它。使用影子罐的原因是我的罐应该与内部依赖库的所有版本一起正确打包。任何帮助将不胜感激。

0 个答案:

没有答案