Pergunta

I have a gradle project with several subprojects - all of which I want to be able to publish at once to a local artifactory repository. This is quite happily managed with uploadArchives. However, uploading requires credentials which I don't want to have stored anywhere. I've found several hacky ways of achieving this with setting extra properties as part of the root project and picking them up in subprojects, but it feels like the correct way to do this is something along the lines of:

task getAuth << {
    ext {
        username = getUsername()
        password = getPassword()
    }
}

uploadArchives.dependsOn(getAuth)

However, uploadArchives appears to be run before it's dependency, hence the auth is set before username or password is set and the script errors out. This seems like exceedingly unexpected behaviour to me.

Foi útil?

Solução 2

So it turns out my question was somewhat wrong. I'd wrongly assumed that the closure for adding publishing repos for maven would be run at the time of the task. However:

uploadArchives {
    addSomeRepos()
}

is configuring the uploadArchives task, and so is run at the time at which it is found in the buildscript. Hence setting the username and password in a task, which will run after the buildscript, means they're null at setup.

I fixed this by changing my getAuth task to a createPublishTargets task which does the configuration of the uploadArchives task inside the task. This works perfectly.

task createPublishTarget << {
  ext {
    username = System.console().readLine("\nusername: ")
    password = System.console().readPassword("password: ").toString()
  }

  allprojects {
    uploadArchives {
      repositories {
        mavenDeployer {
          repository(url: "my-artifactory") {
            authentication(userName: createPublishTarget.username, password: createPublishTarget.password)
          }
        }
      }
    }
  }
}

Although I did still come across an interesting issue that authentication(//blah) configures a different object than I was expecting, so you have to explicitly get the set properties from the task.

Outras dicas

Personally I would set username and password in a task action (uploadArchives.doFirst { ... }), but a configuration task should also work. If it doesn't, chances are that something is wrong with the rest of your build script(s). Note that you are setting extra propertiesgetAuth.username and getAuth.password, not project.username and project.password.

I would suggest to put credentials into $HOME/.gradle/gradle.properties or may be creating a $HOME/.gradle/init.gradle script which contains the definitions for the repositories.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top