Why does gradle throw an error when there is no project property

I am using the maven-publish plugin to release an artifact to the nexus repository. I do not want to specify the actual credentials of my nexus repository in the build.gradle file. So I decided to use the project properties as shown below

credentials {
            username "$nexus_username"
            password "$nexus_password"
}

Now I can use gradle publish -Pnexus_username=<myusername> -Pnexus_password=<mypass>to publish my artifact in my nexus repository. But it makes no sense to continue to pass these project properties to a normal gradle build. And if I don't go through gradle, it causes an error saying Could not find property 'nexus_username'Someone knows a better approach to solve this problem.

+4
source share
2 answers

Found a solution after some digging

if(! hasProperty("nexus_username")){
    ext.nexus_username=""
}
if(! hasProperty("nexus_password")){
    ext.nexus_password=""
}

, . , , . , , - .

+4

Gradle, , ( undefined) Gradle. , , .

Gradle : , , gradle.properties, . , -P, - .

gradle.properties , GRADLE_USER_HOME ( ), :

  • Linux/OS X: ~/.gradle/gradle.properties
  • Windows: %USERPROFILE%\.gradle\gradle.properties

hasProperty script , ( ) gradle.properties < > ( , build.gradle):

# Do not put real credentials here!
# Instead, copy to ~/.gradle/gradle.properties and set the values there.
nexus_username =
nexus_password =

, . , , , . , , , .

0

All Articles