Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting credentials in gradle via maven-publish

I am using gradle v3.4 and have populated properties from a secrets.properties file (passed into project.ext) but when I use the variables in the credentials section, I get an error from nexus complianing about authentication issues which makes me believe the string interpolation is not working correctly. I can print the variable value just before the credentials section.

build.gradle

        maven {
            credentials {
println(project.nexusUsername)  //prints the value
                username '${project.nexusUsername}'
                password '${project.nexusPassword}'
            }
            if (project.version.endsWith("-SNAPSHOT")) {
                url "http://nexus.somewhere.com/repository/some-java-snapshot/"
            } else {
                url "http://nexus.somewhere.com/repository/some-java-release/"
            }
        }

Update I updated the credentials section above to use double quotes (not single) but that did not solve the issue. Single quotes are String literals - if you need String interpolation, you need to use double quotes in groovy.

like image 715
ali haider Avatar asked May 10 '17 19:05

ali haider


People also ask

How can Gradle use repository settings from Maven's settings xml to publish artifacts?

To implement it, we just have to install and apply the plugin and replace the credentials block with name, which is the id of the server we defined in the settings. xml. With this, you should be able to publish artifacts to a secured repository as well. That's all.

What is Mavencentral () in Gradle?

Maven Central is a popular repository hosting open source libraries for consumption by Java projects. To declare the Maven Central repository for your build add this to your script: Example 1. Adding central Maven repository. build.gradle.


1 Answers

The issue was how the properties was specified in the external properties file. I was using double quotes for the String values in the properties file and that was resulting in authentication failures. Once I removed the double quotes from the external properties file, I was able to publish to nexus.

Incorrect external properties file setting

someUsername="someuser"

Correct external properties file setting

someUsername=someuser

build.gradle

publishing {
    publications {
        shadow(MavenPublication) {
            from components.shadow
            groupId project.group
            artifactId project.artifactId
        }
    }
    repositories {
        maven {
            credentials {
                username project.someUsername
                password project.somePassword
            }
            if (project.version.endsWith("-SNAPSHOT")) {
                url project.someSnapshot
            } else {
                url project.someRelease
            }
        }
    }
}

this works.

like image 52
ali haider Avatar answered Sep 19 '22 22:09

ali haider