Gradle uploadArchives publishes POM twice - gradle

Gradle tries to publish maven's POM twice (through uploadArchives task)
I have multiproject build with apply from:s where specified how to publish my artifacts (publish-jars.gradle):
task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allSource
}
build.dependsOn(sourcesJar)
artifacts {
archives sourcesJar
}
uploadArchives {
repositories {
mavenDeployer {
repository(url: 'releases repo') {
authentication(
userName: rootProject.properties.get('artifactory.username'),
password: rootProject.properties.get('artifactory.token')
)
}
snapshotRepository(url: 'snapshots repo) {
authentication(
userName: rootProject.properties.get('artifactory.username'),
password: rootProject.properties.get('artifactory.token')
)
}
}
}
}
def installer = install.repositories.mavenInstaller
def deployer = uploadArchives.repositories.mavenDeployer
[installer, deployer]*.pom*.whenConfigured { pom ->
pom.project {
packaging 'jar'
}
pom.withXml {
asNode().dependencies.'*'.each {
if (it.scope*.value != null) {
it.scope*.value = 'compile'
}
}
asNode().dependencyManagement.dependencies.'*'.each {
if (it.scope*.value != null) {
it.scope*.value = 'compile'
}
}
def repos = asNode().appendNode('repositories')
project.repositories.findAll { it.name != 'MavenLocal' }.each {
def repo = repos.appendNode('repository')
repo.appendNode('id', it.name)
repo.appendNode('name', it.name)
repo.appendNode('url', it.url)
}
}
if (project.hasProperty('pomConfigurer')) {
pom.withXml(pomConfigurer)
}
}
install.dependsOn build
uploadArchives.dependsOn build
task 'publish-snapshot'() {
dependsOn uploadArchives
}
task publish() {
dependsOn uploadArchives
}
and I have module which contains only parent pom (:my-project:parent):
dependencyManagement {
generatedPomCustomization {
enabled = true
}
}
ext.pomConfigurer = {
// some pom configuration
}
and finally I have root project build.gradle:
allprojects {
apply plugin: 'maven'
apply plugin: 'io.spring.dependency-management'
}
subprojects {
apply from: file('publish-jars.gradle')
}
Is it gradle bug, or am I doing something wrong?
UPD: Gradle output
> Task :my-project:some-module:compileKotlin
> Task :my-project:some-other-module:compileKotlin
> Task :my-project:some-another-module:compileKotlin
> Task :my-project:some-yet-another-module:compileKotlin
> Task :my-project:one-more-module:compileKotlin
> Task :my-project:last-module:compileTestKotlin
> Task :mkGitTag
##teamcity[setParameter name='env.release_tag' value='v2.1.1-rc20']
> Task :my-project:parent:uploadArchives
Could not transfer artifact groupname:parent:pom:2.1.1-rc20 from/to remote (https://releases repo): Failed to transfer file: https://releases repo/groupname/parent/2.1.1-rc20/parent-2.1.1-rc20.pom. Return code is: 409, ReasonPhrase: Conflict.
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':my-project:parent:uploadArchives'.
> Could not publish configuration 'archives'
> Failed to deploy artifacts: Could not transfer artifact groupname:parent:pom:2.1.1-rc20 from/to remote (https://releases repo): Failed to transfer file: https://releases repo/groupname/parent/2.1.1-rc20/test-parent-2.1.1-rc20.pom. Return code is: 409, ReasonPhrase: Conflict.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
* Get more help at https://help.gradle.org
BUILD FAILED in 22s
43 actionable tasks: 43 executed

Related

Gradle Error when using maven publishing task

I am using Gradle version 6.7.1, Currently, in my application I facing an issue with the maven publishing task.
We have kept the publishing task in the central location Gradle file named ( nexusgradle-1.0.5.gradle) and importing it via apply from
the content of the central location Gradle (nexusgradle-1.0.5.gradle) is the below which contain the information of nexus repo for snapshot and release along with user credentials for pushing artefacts to 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
}
}
}
The application Gradle ( child Gradle file) looks like the one below
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'
}
when I run the Gradle command for publishing
gradlew clean build publish
The task will fail as the publishing task will try to push artefacts of the snapshot to the release repo instead of the snapshot repo.
> 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
But if I remove the apply from: item and bring the Publishing task to the application Gradle ( child Gradle file) file it will work fine, the build artefact is pushed to snapshot repo without any issue.
> 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
Can someone guide me, what mistake I am making when putting the maven publishing task in a parent Gradle file? why child Gradle cant resolve values from parent properly
I found a way to fix it by updating the content of the central Gradle /parent file (nexusgradle-1.0.5.gradle) by adding
afterEvaluate
with the modification, it worked fine.
Is it the correct approach? or is there any better way to do it?
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
}
}
}
}

Maven publication 'maven' cannot include multiple components

When I am building multi-project with multibranch using this command with Gradle 6.0:
./gradlew :soa-report-consumer-multi_master:soa-report-consumer-api:build publishMavenPublicationToMavenRepository -x test -PpubRepoUrl=https://nexus.balabala.com/repository/maven-releases/ -PmultibranchProjDir=soa-report-consumer-multi_master
shows this eror:
FAILURE: Build failed with an exception.
* Where:
Build file '/Users/dabaidabai/.jenkins/workspace/t-consumer-multi_feature_happygo/build.gradle' line: 35
* What went wrong:
A problem occurred evaluating project ':t-consumer-multi_feature_happygo'.
> Maven publication 'maven' cannot include multiple components
* 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
this is my build.gradle config in sub project:
project(":$consumerMultibranchProjDir:soa-report-consumer-api") {
jar {
enabled = true
}
bootJar {
enabled = false
}
archivesBaseName = "soa-report-consumer-api"
version = "1.0.0-RELEASE"
jar {
enabled = true
}
dependencies {
api("com.sportswin.soa:soa-misc-biz:1.0.0-RELEASE")
api project(":soa-wallet:soa-wallet-api")
}
publishing {
publications {
maven(MavenPublication) {
groupId 'com.sportswin.soa'
artifactId 'soa-report-consumer-api'
version '1.0.0-RELEASE'
from components.java
artifact sourceJar {
classifier "sources"
}
}
}
}
}
where is the problem? and what should I do to fix it?
from the part config you shared - I think that you have 2 sources jars
remove explicit sources jar in publishing
project(":$consumerMultibranchProjDir:soa-report-consumer-api") {
jar {
enabled = true
}
bootJar {
enabled = false
}
archivesBaseName = "soa-report-consumer-api"
version = "1.0.0-RELEASE"
dependencies {
api("com.sportswin.soa:soa-misc-biz:1.0.0-RELEASE")
api project(":soa-wallet:soa-wallet-api")
}
publishing {
publications {
maven(MavenPublication) {
groupId 'com.sportswin.soa'
artifactId 'soa-report-consumer-api'
version '1.0.0-RELEASE'
from components.java
}
}
}
}

gradle publish does not pick up artifacts: "Task has not declared any outputs despite executing actions."

I'm migrating a java project from gradle 2.x to 5.3.1.
The last problem is that no artefacts (a tar in our case) are published to artifactory.
The config in main build.gradle is
subprojects {
apply plugin: "com.jfrog.artifactory"
project.ext {
artifactoryConfig = new File(project.gradle.gradleUserHomeDir, "artifactory.gradle")
}
if (artifactoryConfig.exists()) {
apply plugin: 'maven'
apply from: artifactoryConfig
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven { url $OUR_ARTIFACTORY }
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
}
artifactory {
publish {
contextUrl = "${artifactoryBaseUrl}"
repository {
repoKey = "libs-release-local"
username = "${artifactoryUsername}"
password = "${artifactoryPassword}"
}
}
resolve {
repoKey = 'repos'
}
}
And in the module
task('dist', type: Tar) {
dependsOn('jar')
archiveName "$MODULE-${version}.${extension}"
...
}
publishing {
publications {
mavenJava(MavenPublication)
{
artifact dist {
}
}
}
}
configurations.archives.artifacts.clear()
artifacts {
archives dist
}
Now when I do ./gradlew --info :$MODULE:artifactoryPublish it complains about missing outputs and does not publish the tar to artifactory.
How to fix this?
> Task :$MODULE:artifactoryPublish
Task ':$MODULE:artifactoryPublish' is not up-to-date because:
Task has not declared any outputs despite executing actions.
:$MODULE:artifactoryPublish (Thread[Execution worker for ':',5,main]) completed. Took 0.002 secs.
:artifactoryDeploy (Thread[Execution worker for ':',5,main]) started.
> Task :artifactoryDeploy
Task ':artifactoryDeploy' is not up-to-date because:
Task has not declared any outputs despite executing actions.
Deploying build descriptor to: $OUR_ARTIFACTORY/artifactory/api/build
Build successfully deployed. Browse it in Artifactory under $OUR_ARTIFACTORY/artifactory/webapp/builds/$MODULE/1554465369336
:artifactoryDeploy (Thread[Execution worker for ':',5,main]) completed. Took 0.155 secs.
Turns out the solution was to add
artifactoryPublish {
publications('mavenJava')
}
For me the problem was that the url and credentials were null. It resulted in the same error message(Task has not declared any outputs).
artifactory {
contextUrl = artifactoryUrl
publish {
repository {
username = artifactoryPublishUser
password = artifactoryPublishPassword
The most valuable hint is a running example
https://github.com/jfrog/project-examples/tree/master/gradle-examples/gradle-android-example

gradle artifactory plugin fails to publish with groupID

In my build.gradle script, publishing works when the groupId is left undefined. I would like to use "org.company.foobar.common" for the groupId.
When I uncomment the groupId lines in the following build.gradle script, I receive an error. Below the script is the execution results when this groupId is defined.
buildscript {
repositories {
maven { url "http://tribe.ust.doj.gov:8085/artifactory/jcenter/"}
maven { url "http://tribe.ust.doj.gov:8085/artifactory/MavenCentral/"}
maven { url "http://tribe.ust.doj.gov:8085/artifactory/gradlePlugins/"}
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:latest.release"
}
}
repositories {
maven { url "http://tribe.ust.doj.gov:8085/artifactory/jcenter/"}
maven { url "http://tribe.ust.doj.gov:8085/artifactory/MavenCentral/"}
maven { url "http://tribe.ust.doj.gov:8085/artifactory/gradlePlugins/"}
}
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: "com.jfrog.artifactory"
def getArtifactoryUrl() {
return "http://tribe.ust.doj.gov:8085/artifactory/"
}
allprojects {
repositories {
def artifactoryMcentralUrl = getArtifactoryUrl() + "MavenCentral/"
maven {url artifactoryMcentralUrl }
}
}
dependencies {
}
sourceSets {
main {
java {
srcDir "/src"
}
}
}
//project.group = 'org.company.foobar.common'
task printProps {
doLast {
println artifactory_user
println artifactory_contextUrl
//println project.group
}
}
publishing {
publications {
mavenJava(MavenPublication) {
//groupId project.group
artifactId project.getName()
version '1.0.0'
from components.java
}
}
}
artifactory {
def artifactoryUrl = getArtifactoryUrl()
contextUrl = artifactoryUrl
publish {
repository {
repoKey = 'libs-snapshot-local'
username = "${artifactory_user}"
password = "${artifactory_password}"
}
defaults {
publications('mavenJava')
publishArtifacts = true
publishPom = true
}
}
}
The output from "gradle artifactoryPublish" when using the groupId (uncommented) is:
$ gradle artifactoryPublish
:generatePomFileForMavenJavaPublication
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:artifactoryPublish
Deploying artifact: http://tribe.ust.doj.gov:8085/artifactory/libs-snapshot-local/org/company/foobar/common/kambucha/1.0.0/kambucha-1.0.0.jar
:artifactoryPublish FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':artifactoryPublish'.
> java.io.IOException: Failed to deploy file. Status code: 409 Response message: Artifactory returned the following errors:
The repository 'libs-snapshot-local' rejected the resolution of an artifact 'libs-snapshot-local:org/company/foobar/common/kambucha/1.0.0/kambucha-1.0.0.jar' due to conflict in the snapshot release handling policy. Status code: 409
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 1.84 secs
As it seems, you are trying to publish a RELEASE artifact to a SNAPSHOT repository. When using maven repositories in Artifactory, you will need to make sure that you are following both the Maven layout, and the release / snapshot policy.
In this specific example it seems that your issue is as following:
Artifactory, following maven policy, is recognizing the following path:
'libs-snapshot-local:org/company/foobar/common/kambucha/1.0.0/kambucha-1.0.0.jar' as a release, while the repository is set to handle only snapshots. For this specific path to work, and in case this is really a snapshot artifact, you will need to change the path to be:
libs-snapshot-local:org/company/foobar/common/kambucha/1.0.0-SNAPSHOT/kambucha-1.0.0-SNAPSHOT.jar
If this is a release, change your deployment path to use 'libs-release-local' repository
You can read more on the repository configuration here

Publishing both jar and sources jar to Artifactory from Gradle

Here is my build.gradle:
buildscript {
repositories {
maven {
url 'http://localhost:8081/artifactory/plugins-release'
credentials {
username = "admin"
password = "password"
}
name = "maven-main-cache"
}
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.0.1"
}
}
apply plugin: 'groovy'
apply plugin: 'maven'
apply plugin: 'codenarc'
apply plugin: 'maven-publish'
apply plugin: "com.jfrog.artifactory"
version="0.0.2"
group = "mylib"
repositories {
mavenCentral()
add buildscript.repositories.getByName("maven-main-cache")
maven {
url "http://localhost:8081/artifactory/myapp-snapshots"
}
}
dependencies {
compile 'commons-validator:commons-validator:1.4.0'
testCompile 'junit:junit:4.11'
}
artifactory {
contextUrl = "http://localhost:8081/artifactory"
publish {
repository {
repoKey = 'myorg-snapshots'
username = "admin"
password = "password"
maven = true
}
defaults {
publications ('mavenJava')
}
}
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
artifacts {
archives sourcesJar
}
task dist(type: Zip, dependsOn: build) {
classifier = 'buildreport'
from('build/test-results') {
include '*.xml'
into 'tests'
}
from('build/reports/codenarc') {
into 'reports'
}
from('build/docs') {
into 'api'
}
from(sourcesJar) {
into 'source'
}
from('build/libs') {
exclude '*-sources.jar'
into 'bin'
}
}
Based on this current setup:
To build my JAR I have to run gradle clean build groovydoc sourcesJar dist and then
To publish to Artifactory, I have to run a second command of gradle artifactoryPublish
Two things I'm looking to change here:
gradle artifactoryPublish only publishes my built JAR and a dynamically-created POM to Artifactory. I'd like it to also publish the sources JAR that my build is creating. How?; and
Ideally I'd like to be able to do all of the above by just invoking gradle publish instead of having to run the 2 commands sequentially. Is this possible? If so, how?
When it comes to publishing source you need to modify your script in the following way:
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifact (sourcesJar) {
classifier = 'sources'
}
}
}
}
When it comes to a single command what you need to do is to define dependencies between tasks. Unfortunately I can't try the script so it may be redundant but should do the job:
artifactoryPublish.dependsOn('clean', 'build', 'groovydoc', 'sourcesJar', 'dist')
publish.dependsOn(artifactoryPublish)

Resources