Skip to content

Instantly share code, notes, and snippets.

@jdigger
Last active December 12, 2015 08:18
Show Gist options
  • Save jdigger/4742917 to your computer and use it in GitHub Desktop.
Save jdigger/4742917 to your computer and use it in GitHub Desktop.
Initialization script (in ~/.gradle) to add my credentials for repos.
import org.gradle.api.artifacts.repositories.AuthenticationSupported
import org.gradle.api.tasks.Upload
import org.gradle.api.publication.maven.internal.ant.RepositoryBuilder
apply from: 'config.gradle'
logger.info "Applying init.gradle to add Artifactory credentials for ${security.host}"
gradle.projectsEvaluated {
security.each {secRepoConfig ->
rootProject.allprojects {
buildscript {
repositories.withType(AuthenticationSupported).matching { it.url.host == secRepoConfig.host }.all {
credentials {
username = secRepoConfig.username
password = secRepoConfig.password
}
}
}
tasks.withType(Upload).all {uTask ->
uTask.repositories.matching { it.hasProperty('repository') && URI.create(it.repository.url).host == secRepoConfig.host }.all {rep ->
def rb = new RepositoryBuilder()
def auth = rb.factories.authentication.newInstance(rb, "authentication", null, null)
auth.userName = secRepoConfig.username
auth.password = secRepoConfig.password
rep.repository.authentication = auth
}
}
repositories.withType(AuthenticationSupported).matching { it.url.host == secRepoConfig.host }.all {
credentials {
username = secRepoConfig.username
password = secRepoConfig.password
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment