簡體   English   中英

Gradle maven 發布插件在多模塊項目中失敗,並出現錯誤“此版本未生成工件”

[英]Gradle maven publish plugin fails in a multi-module project with error “Artifact wasn't produced by this build”

我有一個多模塊項目,它支持mavengradle構建,因此它包含pom.xml文件以及 ZC197964236023 和build.gradle 我正在做一個演示,我想展示如何使用gradlemaven構建和部署到同一個項目。 這就是為什么我有兩個不同的構建系統,以防你想知道為什么。

您可能會看到下面的項目結構。

項目結構

您可以在這里查看代碼。

我已經配置了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.jarmachinery-configrootProject的名稱。

一種解決方法是從rootProject中刪除publishing部分並將其復制到子項目中。 我不喜歡這種方法,因為我最終會得到很多重復的代碼。

您能否指出在多模塊項目中使用maven-publish的更好方法?

經過一番挖掘,我意識到我有兩個問題:

  1. publishing部分在subprojects部分之外,然后gradle嘗試使用忽略包含的模塊的rootProject.name部署工件。

  2. 我在subprojects之外也有groupversion屬性,因此部署的工件undefined為版本號,例如machinery-config-core-undefined.jar

為了解決第二個問題,我將groupversion移到了subprojects部分。

我的構建還產生了一個BOM工件,因此我需要from components.javafrom 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插件。

請參閱此處的代碼

參考

從 Gradle 構建腳本中提取常用方法

Java 平台插件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM