You should have the experience to create the in-house libraries and integrate them with other projects by either using the multi-module development or publishing them as the AAR files for usage. Here comes another great option from AWS, you can use the CodeArtifact to host your local Maven repositories.
The pain point of using CodeArtifact is that the authentication token will be expired every 12 hours. So, you would need to update the token every day for syncing the latest libraries from the CodeArtifact. I have to say this is quite annoying for the Devs. Is there any better way to solve this issue? Yes, thanks Clarity for developing a Gradle plugin to solve this issue. Here are the steps for integrating this plugin with CodeArtifact.
You have to install the AWS CLI tool and set up the access key id and secret key string.
And then input your region, output format, and the access key config. After that, check if everything is good.
aws configure list
If you can see the configuration showing correctly, you are ready to go to the next step. Otherwise, please the related configuration before setting up the plugin.
There are four ways to set up your AWS profile for this plugin.
Set the AWS_PROFILE environment variable with the selected profile name.
Set the CODEARTIFACT_PROFILE environment variable with the selected profile name.
Set the profile using a system property in the gradle.properties.
Add the profile name to the repository URL as a query parameter.
After the profile setup ready, you now need to update your build.gradle fiile (project level), please check the following code:
After this setting, you are ready for happy coding now!
If you use the above setting locally, you should be good and worry about nothing. What if you wanted to use it with your CodePipeline? You will need to modify the setting and make it work as expected.
Update your buildspec.yml
You will need to define your AWS profile before building the project. I recommend that you store your access_key_id and secret_access_key into the AWS system manager instead of using them as plaintext.
Update your build.gradle
When you integrate this with CodePipeline, you would see that the 401 authentication error said that you could not access the Maven repository. It’s because even though you use this plugin to get the auth token for those in-house CodeArtifact libraries, Gradle might access those repositories before invoking the plugin to get the token. So there are two ways to solve this issue.
- Leverage the init.gradle to get the token in the very beginning.
- Update the build.gradle to get the authentication token if you can’t access the profile.
The second way is much easier. Let’s see how to use it.
We will need to leverage the AWS CLI tool to get the authentication token and check if we can get the profile. If we can’t successfully get the profile to do authentication, we use AWS CLI to fetch your CodeArtifact token.
That’s it. Happy coding.
#Android #AWS #Android/Gradle