Java/Groovy/Gradle : 如何更改发布的 jar 名称(但不是 artifact-id)?

Java/Groovy/Gradle : How can I change the jar name (but not artifact-id) for publication?

很快我会发布新版本的my library,它由几个模块组成。对于此版本,我现在使用的是 Gradle 7,因此我正在考虑更改一些我认为需要修复的内容:

这是库声明(例如,thread 模块):

group: 'com.intellisrc', name: 'thread', version: '2.8.0'

发布后,它会生成一个名为:thread-2.8.0.jar 的 jar,我更愿意将其命名为:intellisrc-thread-2.8.0.jar,因为它更具描述性(除此之外,它工作正常) .

要修复它,一种选择是更改 artifact-id,因此声明将变为:

group: 'com.intellisrc', name: 'intellisrc-thread', version: '2.8.0'

但我不想更改它,因为消费者必须更新它,这可能会造成混淆。

我想如果我可以更改 jar 名称和发布文件 (pom.xml、module.json),它应该可以工作。

到目前为止,我可以使用以下方法更改在 build/libs/ 中生成的 jar 文件名:

jar {
    archiveBaseName.set("intellisrc-" + project.name)
}

和内部 publishing:

publishing {
    publications {
       mavenJava(MavenPublication) {
           artifactId project.name
           from components.java
           
           pom {
               ...
               archivesBaseName = "intellisrc-" + project.name
               ...
           }
       }
    }
}

默认情况下,pom-default.xml 中没有文件名。我关注了this recommendation,并添加了:

pom {
    properties = [
        "jar.finalName" : archivesBaseName + "-" + project.version
    ]
}

pom-default.xml 文件中添加:

  <properties>
    <jar.finalName>com.intellisrc.core-2.8.0-SNAPSHOT</jar.finalName>
  </properties>

但是在 module.json 中名称没有改变:

...
      "files": [
        {
          "name": "thread-2.8.0.jar",
          "url": "thread-2.8.0.jar",
          "size": 92799,
          ...

有没有办法将 module.json 中的这些值从 gradle 中更改?我可以考虑解码 json 文件并替换它的值,但我希望有更好的方法来做到这一点。

当我执行 gradle publishToMavenLocal 时,安装在 .m2/repository/com/intellisrc/ 上的 jars 没有改变:它仍然是 thread-2.8.0.jar(尽管在 build/libs 中是正确的),我'我不知道为什么。

如何在不更改 artifact-id 的情况下重命名 jar,以便它与 publishpublishToMavenLocal 一起使用?

这是完整的 build.gradle:

plugins {
    id 'groovy'
    id 'java-library'
    id 'maven-publish'
    id 'signing'
}

def currentVersion = "2.8.0-SNAPSHOT"

allprojects {
    group = groupName
    version = currentVersion
    repositories {
        mavenLocal()
        mavenCentral()
    }
}

/**
 * Returns project description
 * @param name
 * @return
 */
static String getProjectDescription(String name) {
    String desc = ""
    switch (name) {
        case "core":
            desc = "Basic functionality that is usually needed " +
                    "in any project. For example, configuration, " +
                    "logging, executing commands, controlling " +
                    "services and displaying colors in console."
            break
        case "etc":
            desc = "Extra functionality which is usually very " +
                    "useful in any project. For example, monitoring " +
                    "Hardware, compressing or decompressing data, " +
                    "store data in memory cache, manage system " +
                    "configuration in a multithreading safe environment " +
                    "(using Redis as default), simple operations with " +
                    "bytes, etc."
            break
        case "db":
            desc = "Manage databases, such as MySQL, SQLite, " +
                    "BerkeleyDB, Postgresql. Create, store and " +
                    "perform CRUD operations to data without having " +
                    "to use SQL (a light-weight implementation as " +
                    "alternative to Hibernate)."
            break
        case "net":
            desc = "Classes related to networking. For example, " +
                    "sending emails through SMTP, connecting or " +
                    "creating TCP/UDP servers, getting network " +
                    "interfaces and perform netmask calculations, etc."
            break
        case "serial":
            desc = "Manage serial communication easily. It uses " +
                    "JSSC library on the background."
            break
        case "web":
            desc = "Create restful HTTP (GET, POST, PUT, DELETE, etc) " +
                    "or WebSocket application services. Manage JSON " +
                    "data from and to the server easily. It is build " +
                    "on top of Spark-Java Web Framework, so it is " +
                    "very flexible and powerful, but designed to be " +
                    "elegant and easier to use."
            break
        case "crypt":
            desc = "Offers methods to encode, decode, hash and encrypt " +
                    "information. It is built using the BountyCastle " +
                    "library and simplifying its usage without reducing " +
                    "its safety."
            break
        case "thread":
            desc = "Manage Tasks (Threads) with priority and watches " +
                    "its performance. You can create parallel processes " +
                    "easily, processes which are executed in an interval, " +
                    "as a service or after a specified amount of time. " +
                    "This module is very useful to help you to identify " +
                    "bottlenecks and to manage your main threads in a " +
                    "single place."
            break
        case "term":
            desc = "Anything related to terminal is in this module " +
                    "(except AnsiColor, which is in core). It uses JLine " +
                    "to help you create interactive terminal (console) " +
                    "applications easily. It also contains some tools " +
                    "to display progress with colors."
            break
        case "img":
            desc = "Classes for using Images (BufferedImage, File, FrameShot) " +
                    "and non-opencv related code, trying to keep dependencies " +
                    "to a minimum. It also includes common geometric operations."
            break
        case "cv":
            desc = "Classes for Computer Vision (extension to OpenCV). " +
                    "Convert image formats, crop, rotate images or draw " +
                    "objects on top of them. It simplifies grabbing images " +
                    "from any video source."
            break
    }
    return desc
}

subprojects {
    apply plugin: 'groovy'
    apply plugin: 'java-library'
    apply plugin: 'maven-publish'
    apply plugin: 'signing'

    dependencies {
        //def groovyVer = "2.5.14"
        def groovyVer = "3.0.7"
        def groovyExt = "3.0.8.7"
        def spock = "2.0-groovy-3.0"
        def junit = "4.13.2"

        boolean isDev = currentVersion.contains("SNAPSHOT")
        def deps = ["org.codehaus.groovy:groovy-all:${groovyVer}"]
        if(isDev) {
            deps.each { api it }
        } else {
            deps.each { compileOnly it } //Link it so we can choose version
        }
        // Always include groovy-extend and expose it to consumers:
        api "com.intellisrc:groovy-extend:${groovyExt}"
        testImplementation "org.spockframework:spock-core:${spock}"
        testImplementation "junit:junit:${junit}"
    }
    jar {
        archiveBaseName.set("intellisrc-" + archiveBaseName.get())
    }
    java {
        withSourcesJar()
        withJavadocJar()
    }
    // Used to publish to MavenLocal
    publishing {
        repositories {
            maven {
                def releasesRepoUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2"
                def snapshotsRepoUrl = "https://oss.sonatype.org/content/repositories/snapshots"
                url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
                credentials {
                    username = ossrhUsername
                    password = ossrhPassword
                }
            }
        }
       publications {
           mavenJava(MavenPublication) {
               artifactId project.name
               from components.java

               pom {
                   name = projectName + " : " + project.name.capitalize() + " Module"
                   description = getProjectDescription(project.name) ?: projectDescription
                   url = projectURL
                   inceptionYear = projectSince
                   archivesBaseName = "intellisrc-" + project.name
                   properties = [
                        "jar.finalName" : archivesBaseName + "-" + project.version
                   ]

                   licenses {
                       license {
                           name = 'GNU General Public License v3.0'
                           url = 'https://www.gnu.org/licenses/gpl-3.0.en.html'
                           distribution = 'repo'
                       }
                   }
                   developers {
                       developer {
                           id = authorId
                           name = authorName
                           email = authorEmail
                       }
                   }
                   scm {
                       url = projectURL
                       connection = "scm:git:${projectURL}.git"
                       developerConnection = "scm:git:${projectDevURL}"
                   }
               }
           }
       }
    }

    signing {
        required {
            !version.endsWith("SNAPSHOT")
        }
        sign publishing.publications.mavenJava
    }

    tasks.named('test') {
        // Use JUnit Platform for unit tests.
        useJUnitPlatform()
    }
}

这是不可能的。

Maven 本地存储库中 JAR 的名称始终为 artifactId-version.jar(或者,如果您有分类器,则为 artifactId-version-classifier.jar)。这是固定的 Maven 结构。

这 100% 是一个错误。这不仅仅是一个“很高兴拥有”,这个错误的存在 breaks the ability for consumers of libraries that use modules with common names (i.e. "core") to include the same named JARs, if their dependencies happen to have the same version number.

我 运行 遇到了同样的问题并提交了 tracking bug。希望它会在即将推出的 Gradle.

版本中得到解决