[英]How to publish a multi-module Gradle project to local Maven repository?
[英]Gradle maven publish plugin fails in a multi-module project with error “Artifact wasn't produced by this build”
我有一個多模塊項目,它支持maven和gradle構建,因此它包含pom.xml
文件以及 ZC197964236023 和build.gradle
我正在做一個演示,我想展示如何使用gradle或maven構建和部署到同一個項目。 這就是為什么我有兩個不同的構建系統,以防你想知道為什么。
您可能會看到下面的項目結構。
您可以在這里查看代碼。
我已經配置了gradle maven-publish插件,以便將所有模塊發布到我的本地 nexus 存儲庫但是當我運行gradle publish
時,我遇到了這個錯誤:
Execution failed for task ':publishMavenJavaPublicationToMavenRepository'.
> Failed to publish publication 'mavenJava' to repository 'maven'
> Artifact machinery-config-0.0.1.jar wasn't produced by this build.
該問題與$rootDir/build.gradle
中的publishing
部分有關。
maven-publish以某種方式令人困惑,它試圖發布一個不存在的工件machinery-config-0.0.1.jar
, machinery-config
是rootProject
的名稱。
一種解決方法是從rootProject
中刪除publishing
部分並將其復制到子項目中。 我不喜歡這種方法,因為我最終會得到很多重復的代碼。
您能否指出在多模塊項目中使用maven-publish的更好方法?
經過一番挖掘,我意識到我有兩個問題:
publishing
部分在subprojects
部分之外,然后gradle嘗試使用忽略包含的模塊的rootProject.name
部署工件。
我在subprojects
之外也有group
和version
屬性,因此部署的工件undefined
為版本號,例如machinery-config-core-undefined.jar
為了解決第二個問題,我將group
和version
移到了subprojects
部分。
我的構建還產生了一個BOM工件,因此我需要from components.java
和from components.javaPlatform
的兩個出版物,所以我編寫了這個腳本gradle/ext/publish-common.gradle
導出兩個函數,我稍后將在兩個出版物上使用以保留代碼在海灣重復。
def pom(it) {
it.licenses {
license {
name = 'The Apache License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
it.developers {
developer {
id = 'eljaiek'
name = 'Eduardo Eljaiek'
email = 'eduardo.eljaiek@gmail.com'
}
}
it.scm {
connection = 'scm:git:git://github.com/eljaiek/machinery-config.git'
developerConnection = 'scm:git:git@github.com:eljaiek/machinery-config.git'
url = 'https://github.com/eljaiek/machinery-config'
}
}
def nexusRepository(it) {
it.url = version.endsWith('SNAPSHOT') ? project.nexusSnapshotsUrl : project.nexusReleasesUrl
it.credentials {
username project.nexusUser
password project.nexusPasswd
}
}
ext {
pom = this.&pom
nexusRepository = this.&nexusRepository
}
我from components.javaPlatform
發布添加到machinery-config-dependencies/build.gradle
以部署BOM工件
apply plugin: 'maven-publish'
apply from: "$rootDir/gradle/ext/publish-common.gradle"
publishing {
publications {
mavenBom(MavenPublication) {
from components.javaPlatform
pom { x -> pom(x)}
}
}
repositories {
maven { x -> nexusRepository(x) }
}
}
apply from: "$rootDir/gradle/ext/publish-common.gradle"
將應用我編寫的腳本,這是將pom()
和nexusRepository()
函數導入我的構建腳本所必需的。
為了from components.java
工件進行部署,我在subprojects
中的if (it.name != 'machinery-config-dependencies')
語句中添加了一個publishing
部分到$rootDir/build.gradle
apply plugin: 'ru.vyarus.pom'
apply from: "$rootDir/gradle/ext/publish-common.gradle"
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
pom { x -> pom(x)}
}
}
repositories {
maven { x -> nexusRepository(x) }
}
}
我使用Gradle POM 插件而不是maven-publish
。 它提供了 maven 的依賴聲明簡單性並隱式應用maven-publish插件。
請參閱此處的代碼
參考
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.