A few days ago I had an interesting task with Maven and AWS S3. We decided to use S3 bucket as a private Maven repository. Amazon S3 is a good place for keeping private Maven artifacts.
If you want to use S3 bucket as a private or public maven repo, first of all, you need to create an s3 bucket, create IAM user and configure AWS access by keys. It's a very simple task, let's do it.
Create an S3 Bucket
1. Create a new S3 bucket. Name it using your project domain and a prefix. For example, repo.myproject.io, repo is a prefix and myproject.io is the domain.
There's no need to configure any permissions for this bucket. Just create it through the Amazon S3 console.
Create an IAM User
Create a new IAM user.Name it like project-maven if your project name is project 😀
Add a new "inline policy" to the user:
{
"Statement": [
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::repo.myproject.io",
"arn:aws:s3:::repo.myproject.io/*"
]
}
]
}
So repo.myproject.io is the name of the S3 bucket we created a few minutes ago.
Make sure you have an "access key" for this new user. It must look similar to this:
key: AN4IANJTUN7ADI9XVKD5
secret: wuaRZQCCErX32avyxf5aTtfbmlFGE4iMFBOht5w3
The key is 20 characters (all caps), and the secret is 40 characters.
Configure maven settings.xml
Add this configuration to your ~/.m2/settings.xml file:
<settings>
<servers>
<server>
<id>repo.myproject.io</id>
<username>AN4IANJTUN7ADI9XVKD5</username>
<password>wuaRZQCCErX32avyxf5aTtfbmlFGE4iMFBOht5w3</password>
</server>
[…]
Now I need to deploy one *.jar file to our privet repo. How can I do that?
I will use mvn deploy file. For my example, lets take Java Wialon SDK https://github.com/wialon/java_wialon_sdk
1. Download wialon.jar Place it for example to a directory /opt/.
2. Unpack and try to deploy to S3 bucket.
Our command will look:
mvn deploy:deploy-file -Dfile=wialon-sdk-1.3.57.jar -DgroupId=com.wialon -DartifactId=sdk-core -Dversion=1.3.57 -Dpackaging=jar -DrepositoryId=repo.awspoc.io.lib -Durl=s3://repo.myproject.io/lib
I received several errors like:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy-file (default-cli) on project standalone-pom: Execution default-cli of goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy-file failed: An API incompatibility was encountered while executing org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy-file: java.lang.NoSuchFieldError: USE_DEFAULTS
That's why I downloaded few libraries. Placed it to main Maven lib directory.
apache-httpcomponents-httpcore.jar
aws-java-sdk-1.11.110.jar
aws-java-sdk-1.11.110-javadoc.jar
aws-java-sdk-1.11.110-sources.jar
aws-maven-5.0.0.RELEASE.jar
commons-logging-1.2.jar
httpclient-4.5.3.jar
httpcore-4.4.6.jar
jackson-annotations-2.1.2.jar
jackson-annotations-2.8.7.jar
jackson-core-2.9.0.pr2.jar
jackson-databind-2.8.7.jar
joda-time-2.9.9.jar
Also I removed apache-httpcomponents-httpcore.jar which was an old version in the maven/lib. It caused a conflict with httpcore.jar
Run our command once more and Voila!
mvn deploy:deploy-file -Dfile=wialon-sdk-1.3.57.jar -DgroupId=com.wialon -DartifactId=sdk-core -Dversion=1.3.57 -Dpackaging=jar -DrepositoryId=repo.awspoc.io.lib -Durl=s3://repo.myproject.io/lib
Also, you can delete all the deployed files with:
aws s3 rm s3://repo.myproject.io/lib –recursive
Stay tuned!
Leave a Reply