Last active
September 21, 2021 22:56
-
-
Save tac-yacht/f6d6e6c760e0485c0ef66e0ed41252ce to your computer and use it in GitHub Desktop.
codeartifact's credentials get from profile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
buildscript { | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
classpath 'com.amazonaws:aws-java-sdk-codeartifact:1.11.801' | |
} | |
} | |
import com.amazonaws.services.codeartifact.AWSCodeArtifactClient; | |
import com.amazonaws.auth.profile.ProfileCredentialsProvider; | |
import com.amazonaws.services.codeartifact.model.GetAuthorizationTokenRequest; | |
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)) | |
.withRegion(region) | |
.build(); | |
def result = client.getAuthorizationToken(new GetAuthorizationTokenRequest() | |
.withDomain(artifactDomain) | |
.withDomainOwner(artifactOwner) | |
); | |
mavenArtifactRepository.credentials { | |
username "aws" | |
password result.authorizationToken | |
} | |
} | |
apply plugin: 'java' | |
apply plugin: 'eclipse' | |
repositories { | |
maven { | |
url 'https://trial-558497472117.d.codeartifact.us-west-2.amazonaws.com/maven/trial/' | |
setAuthorizationToken(owner, "profileName") | |
} | |
} | |
dependencies { | |
implementation platform('com.amazonaws:aws-java-sdk-bom:1.11.801') | |
implementation 'com.amazonaws:aws-java-sdk-codeartifact' | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment