Java 想要在 build.gradle 中指定 jar 名称和版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31405818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:03:37  来源:igfitidea点击:

Want to specify jar name and version both in build.gradle

javagradle

提问by amuser

I want to specify the jar name and version in build.gradle, so my build.gradle looks like below.

我想在 build.gradle 中指定 jar 名称和版本,所以我的 build.gradle 如下所示。

apply plugin: 'java'

version = "1.00.00"

 dependencies { 
     compile files('../../lib/abc.jar') 
} 

jar{
    manifest{
        attributes ("Fw-Version" : "2.50.00", "${parent.manifestSectionName}")

    }
    archiveName 'abc.jar'
}

So when I do

所以当我做

gradle clean build, what I was expecting was that the generated jar name would be abc-1.00.00.jar

gradle clean build,我期望生成的 jar 名称将是 abc-1.00.00.jar

But its not happening, the output jar name is getting as abc.jar only and its just ignoring the version. I want to specify both jar name and version, so how can I achieve that?

但它没有发生,输出 jar 名称仅作为 abc.jar 并且它只是忽略版本。我想同时指定 jar 名称和版本,那么我该如何实现呢?

采纳答案by andyberry88

archiveName 'abc.jar'in your jarconfiguration is forcing the name to be 'abc.jar'. The default format for the jar name is ${baseName}-${appendix}-${version}-${classifier}.${extension}, where baseNameis the name of your project and versionis the version of the project. If you remove the archiveName line in your configuration you'll have the format you're after. If you want the name of the archive to be different from the project name set the baseNameinstead, e.g.

archiveName 'abc.jar'在您的jar配置中强制名称为“abc.jar”。jar 名称的默认格式为${baseName}-${appendix}-${version}-${classifier}.${extension},其中baseName是您的项目名称,version是项目的版本。如果您删除配置中的 archiveName 行,您将拥有您所追求的格式。如果您希望存档的名称与项目名称不同,请baseName改为设置,例如

jar {
    manifest{
        attributes ("Fw-Version" : "2.50.00", "${parent.manifestSectionName}")
    }
    baseName 'abc'
}

See https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Jar.html#org.gradle.api.tasks.bundling.Jar:archiveNamefor more info on configuring the jartask.

有关配置任务的更多信息,请参阅https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Jar.html#org.gradle.api.tasks.bundling.Jar:archiveNamejar

回答by RMorrisey

If you want to rename the JAR, source JAR and javadoc JAR with a single setting, while still including the classifier and version, set archiveBaseNameat the top level.

如果要重命名 JAR,请使用单个设置源 JAR 和 javadoc JAR,同时仍包括分类器和版本,请archiveBaseName在顶级设置。

回答by Dolphin

Read the doc of gradle,the baseName and archiveName is deprecated,you should use archivesBaseName in project level like this:

阅读gradle文档,不推荐使用 baseName 和 archiveName,您应该在项目级别使用archivesBaseName,如下所示:

project(":web") {
    description = "web"
    archivesBaseName = "your-project-name-" + getVersionCode()

    jar {
        // Will include every single one of your dependencies, project or not
        from {
            configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
        }
    }
}

It works fine for my project. I hope it will be helpful for your project.

它适用于我的项目。我希望它会对您的项目有所帮助。

回答by Ahmed Kamal

For Gradle 5+

对于Gradle 5+

build.gradlegroovy DSL

build.gradle时髦的DSL

archiveBaseName = 'foo'

build.gradle.ktskotlin DSL

build.gradle.kts科特林 DSL

archivesBaseName.set("foo") // Notice the double quotes