Gradle Error when using maven publishing task - gradle

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
}
}
}
}

Related

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-release build and publish non-snapshot artifacts

I have a multi-module project. And, I want to be able do just do gradle release and get all artifacts of all modules released.
One of the problems is that when I include the plugins, I get
Task with name 'build' not found in root project '
So, following some advice on the internet I created a build task and added a bunch of dependencies on it:
plugins {
id "net.researchgate.release" version "2.6.0"
}
defaultTasks 'clean', 'assemble'
def repoUrl = System.env.REPO ?: "https://company.jfrog.io/company/maven"
allprojects {
repositories {
mavenCentral()
jcenter()
maven {
url 'https://dl.bintray.com/palantir/releases'
}
maven {
credentials {
username System.env.REPO_USER
password System.env.REPO_PASS
}
url repoUrl
name 'company'
}
}
}
task build{}
subprojects { thisProject ->
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'checkstyle'
apply plugin: 'maven-publish'
apply plugin: 'findbugs'
build.dependsOn "${thisProject}:build"
publishing {
repositories {
maven {
credentials {
username System.env.REPO_USER
password System.env.REPO_PASS
}
url repoUrl
name 'company'
}
}
publications {
"-$thisProject.name-"(MavenPublication) {
from components.java
}
}
}
sourceCompatibility = 1.8 // java 8
targetCompatibility = 1.8
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
archives javadocJar
}
}
tasks.each {println(it)}
afterReleaseBuild.dependsOn tasks.publish
when I run gradle tasks --all I get
task ':afterReleaseBuild'
task ':beforeReleaseBuild'
task ':build'
task ':checkCommitNeeded'
task ':checkSnapshotDependencies'
task ':checkUpdateNeeded'
task ':commitNewVersion'
task ':confirmReleaseVersion'
task ':createReleaseTag'
task ':createScmAdapter'
task ':initScmAdapter'
task ':preTagCommit'
task ':release'
task ':runBuildTasks'
task ':unSnapshotVersion'
task ':updateVersion'
"release" works. That is to say it bumps versions, tags, strips the "-SNAPSHOT" off, etc. But, there is a step missing here (and it could totally be my ignorance), but upon stripping the "-SNAPSHOT" from the version I need it to build the artifacts and publish them. This is like the maven release process but without the artifact upload. I am currently using gradle publish and not maven upload.
Caveats: Very new to gradle but not java
Can someone tell me what I am missing?
_
You just can try to remove build task from the relesae plugin configuration.
buildTasks = [] works fine for me.
release {
...
tagTemplate = '${version}'
versionPropertyFile = 'gradle.properties'
buildTasks = []
...
}

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

Error pushing docker image to dockerhub using gradle

I am trying this Spring boot with docket example using Gradle, but getting the following error on running the task gradle build buildDocker
C:\Users\zeeshan\Workspace\MyWorkspace\SpringBootDocker>gradle build buildDocker
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:findMainClass
:jar
:bootRepackage
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build
:buildDocker FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':buildDocker'.
> java.io.IOException: Cannot run program "docker": CreateProcess error=2, The system cannot find the file specified
* 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: 10.092 secs
The Docketfile exists in src\main\docker in my windows machine.
My build.gradle file:
buildscript {
repositories {
maven { url "${nexusUrl}/content/groups/public" }
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.5.RELEASE")
classpath('se.transmode.gradle:gradle-docker:1.2')
}
}
group = 'mydockergroup'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'docker'
jar {
baseName = 'gs-spring-boot-docker'
version = '0.1.0'
}
repositories {
maven { url "${nexusUrl}/content/groups/public" }
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
task buildDocker(type: Docker, dependsOn: build) {
push = true
applicationName = jar.baseName
dockerfile = file('src/main/docker/Dockerfile')
doFirst {
copy {
from jar
into stageDir
}
}
}
Note: I copied my workspace in a Ubuntu system and it worked fine. Is there a problem setting the file path for dockerfile in my build.gradle for Windows system?
We are specifying dockerDirectory in docker-maven-plugin. I think you may required to setup similar in gradle.
<dockerDirectory>${basedir}/docker</dockerDirectory>

"gradle publish" wrongly reporting up-to-date

I'm trying to build Apache S4 and publish it to our Nexus repository. gradle publishToMavenLocal worked, so I added
publishing {
repositories {
maven {
credentials {
username "admin"
password "admin123"
}
url "http://127.0.0.1:9081/nexus/content/repositories/releases/"
}
}
}
to build.gradle after apply plugin: 'maven-publish'. This worked for other projects on the same machine. However, now I get
aromanov#ws:~/etc/apache-s4-0.6.0$ gradle clean publish
Build file '/home/aromanov/etc/apache-s4-0.6.0/subprojects/s4-benchmarks/s4-benchmarks.gradle': line 42
The RepositoryHandler.mavenRepo() method has been deprecated and is scheduled to be removed in Gradle 2.0. Please use the maven() method instead.
Runs Apache RAT. Exclusions are defined in .rat-excludes fileUNDEFINED
:clean
:s4-base:clean
:s4-benchmarks:clean
:s4-comm:clean
:s4-core:clean
:s4-tools:clean
:test-apps:clean
:test-apps:consumer-app:clean
:test-apps:producer-app:clean
:test-apps:simple-deployable-app-1:clean
:s4-base:publish UP-TO-DATE
:s4-benchmarks:publish UP-TO-DATE
:s4-comm:publish UP-TO-DATE
:s4-core:publish UP-TO-DATE
:s4-tools:publish UP-TO-DATE
:test-apps:publish UP-TO-DATE
:test-apps:consumer-app:publish UP-TO-DATE
:test-apps:producer-app:publish UP-TO-DATE
:test-apps:simple-deployable-app-1:publish UP-TO-DATE
BUILD SUCCESSFUL
Total time: 10.468 secs
Using gradle -i I get
Selected primary task 'publish'
Tasks to be executed: [task ':s4-base:publish', task ':s4-benchmarks:publish', task ':s4-comm:publish', task ':s4-core:publish', task ':s4-tools:publish', task ':test-apps:publish', task ':test-apps:consumer-app:publish', task ':test-apps:producer-app:publish', task ':test-apps:simple-deployable-app-1:publish']
:s4-base:publish (Thread[main,5,main]) started.
:s4-base:publish
Skipping task ':s4-base:publish' as it has no actions.
:s4-base:publish UP-TO-DATE
:s4-base:publish (Thread[main,5,main]) completed. Took 0.005 secs.
... so on for other subprojects
There is no output at the Nexus repository. How can I fix this?
UPDATE: if I use configuration from chapter 51 of the User Guide, replacing ivy by maven:
uploadArchives {
repositories {
maven {
credentials {
username "admin"
password "admin123"
}
url "http://127.0.0.1:9081/nexus/content/repositories/releases/"
}
}
}
then upload works, but it uploads jars and ivy.xml, without POMs. With config from Chapter 52:
configurations {
deployerJars
}
dependencies {
deployerJars "org.apache.maven.wagon:wagon-http:2.2"
}
uploadArchives {
configuration = configurations.deployerJars
repositories {
mavenDeployer {
repository(url: "http://127.0.0.1:9081/nexus/content/repositories/releases/") {
authentication(userName: "admin", password: "admin123")
}
}
}
}
I get
aromanov#ws:~/etc/apache-s4-0.6.0$ gradle -i upload
Starting Build
Starting file lock listener thread.
...
Publishing configuration: configuration ':s4-base:deployerJars'
Publishing to repository 'mavenDeployer'
[ant:null] Error reading settings file '/tmp/gradle_empty_settings2934509160228124339.xml' - ignoring. Error was: /tmp/gradle_empty_settings2934509160228124339.xml (No such file or directory)
:s4-base:uploadArchives (Thread[main,5,main]) completed. Took 1.113 secs.
...
The complete build script.
If no any action found, it will mark as up to date .
I am using gradle 2.4 and this is working like a charm
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
repositories {
maven {
credentials {
username 'admin'
password 'password'
}
url "http://baseUrl/artifactory/libs-release-local"
}
}
}
you need to provide publish repositories and publications.

Resources