简体   繁体   English

Gradle的Maven Publish插件不会将POM或适当的版本发布到Maven Local

[英]Gradle's Maven Publish plugin not publishing POM or proper version to Maven Local

I am trying to get the Gradle Maven Publish Plugin to publish a snapshot version of my Java library to my local Maven repo such that: 我正在尝试使用Gradle Maven Publish插件将我的Java库的快照版本发布到我的本地Maven仓库,这样:

  1. The version of the jar is 1.0.0.SNAPSHOT-<timestamp> , where <timestamp> is the current system time in millis (similar to something like System.currentTimeInMillis() ); 罐的版本是1.0.0.SNAPSHOT-<timestamp> ,其中<timestamp>是在米利斯当前系统时间(类似于 System.currentTimeInMillis() ); and
  2. I log to STDOUT/console the full name of the jar being published, including the version above; 我登录STDOUT / console发布的jar的全名,包括上面的版本; and
  3. A properly-formatted pom.xml is published to Maven local alongside the jar, so that any other Gradle/Maven projects can "pull it down" locally and fetch its transitive dependencies properly 正确格式化的pom.xml与jar一起发布到Maven local,这样任何其他Gradle / Maven项目都可以在本地“下拉”并正确获取其传递依赖项

My best attempt so far: 到目前为止我最好的尝试:

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

dependencies {
    compile(
        'org.hibernate:hibernate-core:5.0.12.Final'
        ,'com.fasterxml.jackson.core:jackson-core:2.8.10'
        ,'com.fasterxml.jackson.core:jackson-databind:2.8.10'
        ,'com.fasterxml.jackson.core:jackson-annotations:2.8.0'
    )

    testCompile(
        'junit:junit:4.12'
    )
}

repositories {
    jcenter()
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8
group 'com.me'

jar {
    baseName = 'my-lib'
    version = '1.0.0-SNAPSHOT'
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
}

However, with this setup, when I run ./gradlew publishToMavenLocal : 但是,使用此设置,当我运行./gradlew publishToMavenLocal

  • I do see the jar being deployed to ~/.m2/repository/com/me/my-lib/ but without a pom.xml and no 1.0.0.SNAPSHOT version appended to it 我确实看到jar被部署到~/.m2/repository/com/me/my-lib/但是没有pom.xml而没有附加1.0.0.SNAPSHOT版本
  • I don't even know how/where I would append the timestamp onto the version 我甚至不知道如何/在哪里将时间戳附加到版本上
  • I don't even know how/where I would do a println(...) to report the full name of the jar being published 我甚至不知道如何/在哪里我会用println(...)来报告正在发布的jar的全名

Any ideas? 有任何想法吗?

Regarding #3 , To install your artifact to a local repository you do not need the maven-publish plugin, rather the maven plugin 关于#3 ,要将工件安装到本地存储库,您不需要maven-publish插件,而是maven插件

See The Maven plugin documentation, specifically the Tasks section and the Installing to the local repository section with it, you can run gradle clean build install 请参阅Maven插件文档,特别是Tasks部分和使用它安装到本地存储库部分 ,您可以运行gradle clean build install

It works for me with a build.gradle file as simple as this 它对我来说很简单,只需要一个build.gradle文件

version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'maven'

Note, if you need to publish something other then the default generated jar then you need to change the archives configuration 注意,如果您需要发布除默认生成的jar之外的其他内容,则需要更改archives配置


Regarding #1 appending the timestamp, move the version line outside the jar clause and change it from 关于附加时间戳的#1 ,将版本行移到jar子句之外并从中更改

version = '1.0.0-SNAPSHOT'

to

version = "1.0-SNAPSHOT-${System.currentTimeMillis()}"

This is using Groovy GString (AKA string interpolation - note the change from single quotes to double quotes ) to append the current time in millis to the version 这是使用Groovy GString(AKA字符串插值 - 注意从单引号到双引号的更改 )将当前时间以毫秒为单位附加到版本


Last but not least, regarding #2 printing the jar full name append the following to the build.gradle file 最后但并非最不重要的是,关于#2打印,jar全名会将以下内容附加到build.gradle文件中

install.doLast {
        println jar.archiveName
}

Essentially we're appending to the install task (the one executed in the top of my answer) a println of the jar configuration's archiveName (see here if you want something else) 基本上我们将附加到安装任务(在我的答案的顶部执行的任务)一个jar配置的archiveName的println(如果你想要别的东西,请看这里


So all in all my build.gradle file looks like this: 总而言之,我的build.gradle文件如下所示:

group 'com.boazj'
version "1.0-SNAPSHOT-${System.currentTimeMillis()}"

apply plugin: 'java'
apply plugin: 'maven'

install.doLast {
        println jar.archiveName
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM