来自单个项目的多个Jar

来自单个项目的多个Jar

本文介绍了Gradle-来自单个项目的多个Jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:要从单个项目(多个包gradle项目)创建多个可执行jar



问题:我指的是中提供的解决方案,这有助于在build-> libs文件夹中生成jar,但是当我尝试时执行jar不会发生任何事情



注意:即使我将包名称与java文件名相同,生成的jar也不会执行。 / p>

我还注意到生成的所有jar的文件大小是相同的。希望面临的问题是明确的



我的项目结构(说明性目的):

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

Gradle 文件(相关部分在下面,其余部分根据上面的链接):

 工件{
档案jarPackage( pkgCalculator, 1.0),
jarPackage( pkgScale, 1.0),
jarPackage( pkgMusicPlayer, 1.0),
jarPackage( pkgVideoPlayer, 1.0)

}


解决方案

与@iCoder讨论之后,问题的第一部分已解决: jarPackage 函数不能按原样重用,而必须进行调整以支持通用的类/包布局:

  def jarPackage(String jarName,String className,artifactVersion){
if(artifactVersion == || artifactVersion == null){
artifactVersion = 1.0.0
}
返回task.create( jar $ {jarName},Jar){
baseName = jarName

版本= artifactVersion

def字符串pkgName = className.substring(0,className.lastIndexOf(。))
def字符串pkgDir = pkgName.replaceAll( \\。, /)
def字符串clazzName = className.substring(className.lastIndexOf(。)+1)
from(sourceSets.main.output){
包括 $ pkgDir // **
}

来自{
configuration.compile.collect {
it.isDirectory()吗?它:zipTree(it)
}
}

清单{
属性 Implementation-Title: $ clazzName,
Implementation-Version : $ version,
Main-Class: $ pkgName。$ clazzName
}
}
}

工件{
档案jarPackage( calculator, pkg1.pkcalculator.Calculator, 1.0.0)
}

另一个问题仍然存在,与Gradle无关,但与JDK 11链接问题有关:@iCoder,您应该为剩下的问题(如果尚未解决)打开另一个问题。


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

}
解决方案

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.

这篇关于Gradle-来自单个项目的多个Jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 14:50