首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java/Groovy/Gradle :如何更改jar名称(但不是工件-id)以供发布?

Java/Groovy/Gradle :如何更改jar名称(但不是工件-id)以供发布?
EN

Stack Overflow用户
提问于 2021-07-09 02:50:08
回答 3查看 979关注 0票数 1

不久,我将发布一个新版本的我的图书馆,它由几个模块组成。对于这个版本,我现在使用的是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文件名:

代码语言:javascript
复制
jar {
    archiveBaseName.set("intellisrc-" + project.name)
}

publishing内部

代码语言:javascript
复制
publishing {
    publications {
       mavenJava(MavenPublication) {
           artifactId project.name
           from components.java
           
           pom {
               ...
               archivesBaseName = "intellisrc-" + project.name
               ...
           }
       }
    }
}

默认情况下,pom-default.xml中没有文件名。我跟踪了2.本建议,并补充道:

代码语言:javascript
复制
pom {
    properties = [
        "jar.finalName" : archivesBaseName + "-" + project.version
    ]
}

它添加了pom-default.xml文件:

代码语言:javascript
复制
  <properties>
    <jar.finalName>com.intellisrc.core-2.8.0-SNAPSHOT</jar.finalName>
  </properties>

然而,在module.json中,名称并没有改变:

代码语言:javascript
复制
...
      "files": [
        {
          "name": "thread-2.8.0.jar",
          "url": "thread-2.8.0.jar",
          "size": 92799,
          ...

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

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

如何在不更改工件id的情况下重命名jar,使其与publishpublishToMavenLocal一起工作?

这是完整的build.gradle

代码语言:javascript
复制
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()
    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-04-03 20:26:50

这是百分之百的错误。这不仅仅是一个“很好的拥有”,这个bug的存在如果库的依赖项恰好具有相同的版本号,则中断使用具有公共名称的模块(即“核心”)的库使用者包含相同名称的JAR的能力。

我遇到了同样的问题,并提交了一个跟踪错误。希望它能在即将推出的Gradle版本中得到解决。

票数 1
EN

Stack Overflow用户

发布于 2021-07-09 11:59:20

这是做不到的。

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

票数 1
EN

Stack Overflow用户

发布于 2022-08-23 11:45:51

您可以添加具有所需名称的模块属性(即模块=“期望的名称”)来发布子模块jars。

代码语言:javascript
复制
publishing {
    publications {
        ivy(IvyPublication) {
            organisation = 'org.gradle.sample'
            **module = 'project1-sample'**
            revision = '1.1'
            descriptor.status = 'milestone'
            descriptor.branch = 'testing'
            descriptor.extraInfo 'http://my.namespace', 'myElement', 'Some value'

            from components.java
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68310697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档