Gradle使用maven发布任务时出错

Gradle Error when using maven publishing task

我正在使用 Gradle 版本 6.7.1,目前,在我的应用程序中,我遇到了 maven 发布任务的问题。

我们将发布任务保存在名为 (nexusgradle-1.0.5.gradle) 的中央位置 Gradle 文件中,并通过 apply from

导入它

中心位置Gradle (nexusgradle-1.0.5.gradle) 的内容如下,其中包含用于快照和发布的 nexus repo 信息以及用于将工件推送到 nexus 的用户凭据.

apply plugin: 'maven-publish'

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.web
        }
   }
    repositories {
        maven {
            credentials {
                username 'uploader'
                password 'uploaderpassword'
            }
            println 'A message which is logged at QUIET level'
            name 'Nexus_Repo'
            def releasesRepoUrl = 'http://<hostname>/repository/<maven-releases>/'
            def snapshotsRepoUrl = 'http://<hostname>/repository/<maven-snapshots>/'
            url = project.version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
        }
    }
}

应用程序 Gradle(child Gradle 文件)如下所示

import org.apache.tools.ant.filters.ReplaceTokens

plugins {
    id 'war'
    // Add git release plugin for versioning snaphots and release builds
    id 'pl.allegro.tech.build.axion-release' version '1.10.1'
    id 'org.springframework.boot' version '2.1.4.RELEASE'
    // Add Git properties plugin.
    id 'com.gorylenko.gradle-git-properties' version '2.2.0'
    id 'jacoco'
 }

// apply from center location
apply from :'http://<hostaname>/repository/thirdparty/com/mf/nexusgradle/1.0.5/nexusgradle-1.0.5.gradle'






repositories {
        maven {
        url = 'http://<hostname>/repository/groupRepo/'
        }
    jcenter()
    }

test {
    testLogging.showStandardStreams = true
    maxParallelForks = 3

    ignoreFailures = true // to skip test Failures
    testLogging { //logging the test
        exceptionFormat = 'full'
        events "passed", "skipped", "failed"
    }
}

jacoco {
    toolVersion = '0.8.3'
}

jacocoTestReport {
    dependsOn test // tests are required to run before generating the report
    reports {
        xml.enabled true //enabling for generate xml for to capture data in sonarqube server
    }
}

// Customize Git properties plugin.
gitProperties {
    // Change date format in git.properties file.
    dateFormat = "yyyy-MM-dd HH:mm:ssZ"
    dateFormatTimeZone = 'GMT'
}

dependencies {
    implementation 'org.springframework.boot:spring-boot:2.1.4.RELEASE'
    // mutliple import below 
}

sourceCompatibility = '1.8'
targetCompatibility = '1.8'

scmVersion {
    repository {
        directory = project.rootProject.file('.')
    }
}

group = 'com.package'
description = 'appname'
project.version = scmVersion.version
project.ext.timestamp = new Date().format("dd/MM/yyyy HH:mm:ss")

processResources {
    filter ReplaceTokens, tokens:[BUILD_VERSION: project.version, BUILD_TIMESTAMP: project.ext.timestamp]
}

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

war {
    enabled = true
}



springBoot {
    buildInfo()
}

bootWar {
    archiveClassifier = 'boot'
    mainClassName = 'com.package.appname.SpringBootRunner'
}

当我运行发布Gradle命令时

gradlew clean build publish

任务将失败,因为发布任务将尝试将快照的工件推送到发布存储库而不是快照存储库。

  > Configure project :
A message which is logged at QUIET level

> Task :clean UP-TO-DATE
> Task :bootBuildInfo

> Task :compileJava
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

> Task :generateGitProperties
> Task :processResources
> Task :classes
> Task :bootWar
> Task :war
> Task :assemble
> Task :check
> Task :build
> Task :generateMetadataFileForMavenJavaPublication
> Task :generatePomFileForMavenJavaPublication
> Task :publishMavenJavaPublicationToNexus_RepoRepository FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':publishMavenJavaPublicationToNexus_RepoRepository'.
> Failed to publish publication 'mavenJava' to repository 'Nexus_Repo'
   > Could not PUT 'http://<hostname>/repository/maven-releases/com/package/appname/1.0.9-SNAPSHOT/maven-metadata.xml'. Received status code 400 from server: Repository version policy: RELEASE does not allow metadata in path: com/package/appname/1.0.9-SNAPSHOT/maven-metadata.xml

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

但是如果我删除 apply from: 项并将发布任务带到应用程序 Gradle(child Gradle 文件)文件中,它将工作正常,构建工件被毫无问题地推送到快照仓库。

> Configure project :
A message which is logged at the QUIET level

> Task :clean UP-TO-DATE
> Task :bootBuildInfo

> Task :compileJava
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

> Task :generateGitProperties
> Task :processResources
> Task :classes
> Task :bootWar
> Task :war
> Task :assemble
> Task :check
> Task :build
> Task :generateMetadataFileForMavenJavaPublication
> Task :generatePomFileForMavenJavaPublication
> Task :publishMavenJavaPublicationToNexus_RepoRepository
> Task :publish

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.7.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 29s
10 actionable tasks: 9 executed, 1 up-to-date

谁能指导我,我在将 maven 发布任务放入 parent Gradle 文件时犯了什么错误?为什么 child Gradle 无法正确解析 parent 中的值

我通过添加

更新中央Gradle/父文件(nexusgradle-1.0.5.gradle)的内容找到了修复它的方法

afterEvaluate

经过修改,效果很好。 这是正确的方法吗?或者有什么更好的方法吗?

apply plugin: 'maven-publish'


afterEvaluate {project ->
publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.web
        }
   }
    repositories {
        maven {
            credentials {
                username 'uploader'
                password 'uploaderpassword!'
            }
            name 'Nexus_Repo'
              def releasesRepoUrl = 'http://<hostname>/repository/<maven-releases>/'
            def snapshotsRepoUrl = 'http://<hostname>/repository/<maven-snapshots>/'
            url = project.version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
        }
    }
}

}