Gradle - Multiple Jar from single project

隐身守侯 提交于 2019-12-11 15:59:03

问题


Context: To create multiple executable jar's from a single project - multiple package gradle project

Issue: I refer to the solution provided in Link, this helps in generating the jar in build->libs folder, but when I try executing the jar nothing happens

Note: Even if I make the package name same as java file name, the generated jar does not execute.

Also I notice the file size of all the jar's generated is the same. Hope the issue faced is clear & await inputs as to where I am making a mistake.

My Project Structure (illustrative purpose):

ProjectA
-src
--main
---java
----pkg1
-----pkgCalculator         
------Calculator.java
-----pkgScale              
------Scale.java
----pkg2
-----pkgMusicPlayer        
------MusicPlayer.java
-----pkgVideoPlayer        
------VideoPlayer.java
---resources
----fxml
----css
--test
---java
---resources

Gradle file (relevant portion below, rest as per the link above ):

artifacts {
    archives jarPackage("pkgCalculator", "1.0"),
            jarPackage("pkgScale","1.0"),
            jarPackage("pkgMusicPlayer","1.0"),
            jarPackage("pkgVideoPlayer","1.0")

}

回答1:


After discussion with @iCoder, first part of the issue has been solved: the jarPackage function could not be reused "as is", and had to be adapted to support generic class/package layout:

def jarPackage(String jarName, String className, artifactVersion) {
    if (artifactVersion == "" || artifactVersion == null) {
        artifactVersion = "1.0.0"
    }
    return tasks.create("jar${jarName}", Jar) {
        baseName = jarName

        version = artifactVersion

        def String pkgName = className.substring(0, className.lastIndexOf("."))
        def String pkgDir = pkgName.replaceAll("\\.", "/")
        def String clazzName = className.substring( className.lastIndexOf(".") +1 )
        from(sourceSets.main.output) {
            include "$pkgDir//**"
        }

        from {
            configurations.compile.collect {
                it.isDirectory() ? it : zipTree(it)
            }
        }

        manifest {
            attributes "Implementation-Title": "$clazzName",
                    "Implementation-Version": "$version",
                    "Main-Class": "$pkgName.$clazzName"
        }
    }
}

artifacts {
    archives jarPackage("calculator", "pkg1.pkcalculator.Calculator" , "1.0.0")
}

Another issue remains, not related to Gradle but to JDK 11 linkage problem: @iCoder you should open another question for that remaining problem, if not fixed yet.



来源:https://stackoverflow.com/questions/52839439/gradle-multiple-jar-from-single-project

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