Profile-derived automation of Code Artifact authentication with Gradle


The repository will be managed and it will be very convenient. However, when I read [Official Document] doc, I should load the token in the environment variable ... Do you do such annoying things every time? do not want to!

So I wrote to read from the profile that would have been set in the AWS CLI.


You have already set up an AWS CLI profile that allows you to access any CodeArtifact

How to use

Copy the whole below


buildscript {
	repositories {
	dependencies {
		classpath 'com.amazonaws:aws-java-sdk-codeartifact:1.11.801'

import com.amazonaws.auth.profile.ProfileCredentialsProvider;
def setAuthorizationToken = {mavenArtifactRepository, profile ->
	def domainLevels = mavenArtifactRepository.url.getHost().split('\\.')
	def artifactDomain = domainLevels[0].substring(0,domainLevels[0].lastIndexOf("-"))
	def artifactOwner = domainLevels[0].substring(domainLevels[0].lastIndexOf("-")+1)
	def region = domainLevels[domainLevels.length -3]

	def client = AWSCodeArtifactClient.builder()
		.withCredentials(new ProfileCredentialsProvider(profile))

	def result = client.getAuthorizationToken(new GetAuthorizationTokenRequest()

	mavenArtifactRepository.credentials {
		username "aws"
		password result.authorizationToken

apply plugin: 'java'
apply plugin: 'eclipse'

repositories {
	maven {
		url ''
		setAuthorizationToken(owner, "profileName")

dependencies {
	implementation platform('com.amazonaws:aws-java-sdk-bom:1.11.801')
	implementation 'com.amazonaws:aws-java-sdk-codeartifact'

If you replace the following `here` with the profile name, it's OK

setAuthorizationToken(owner, "here")//← Profile name



If it is s3, it is not necessary to pull it with buildscript from ( lib \ plugins \ in the wrapper of gradle. Since the buildscript aws-java-sdk-codeartifact cannot be closed at first, it cannot be made stand-alone with only complete CodeArtifact. I can do it after caching locally, but ... There wasn't a AWSCodeArtifactReadOnlyAccess -like thing in the AWS management policy yet. (Can you do it in the future? Note that CodeArtifact is not enough and you also need `` `sts: GetServiceBearerToken```!

Recommended Posts

Profile-derived automation of Code Artifact authentication with Gradle
Until you run Hello World of JavaFX with VS Code + Gradle
Static code analysis with Checkstyle in Java + Gradle
[Java] Explanation of Strategy pattern (with sample code)
Using Gradle with VS Code, build Java → run
Java Repository of Eclipse with Maven: Missing artifact ~
The procedure I did when I prepared the environment of gradle + Java with VS Code (Windows 10)
Build ruby debug environment with VS Code of Windows 10
Implement the UICollectionView of iOS14 with the minimum required code.