Setting a private Maven repository on Amazon S3 for a Grails 2.4 plugin

If you are like me, looking for a private Maven repository to share your Grails 2 plugin just among your team and are still waiting for Grails 3 to get more mature, this is the right place. I am going to show in a total of three posts, distinct services that can be used to achieve this goal.

Here are some available options :

In the previous post, I showed how to privately host your at a starting cost of . This price can be considered pretty high for a startup or a small team that needs to share few private libraries. So, if you need an affordable solution at a rough price of (), this is the right tutorial to read. Today I am going to show the cheapest option I have found to make your library exclusive for you and your team.

In order to make your life easier (mine too), some parts on how to create a Grails plugin and configure your pom.xml will be replicated from my previous post.

Creating a plugin

We can start creating a new Grails plugin:

> grails create-plugin sample-plugin

Update the BuildConfig.groovy file of your new plugin with the newest version of the release plugin ( to get the most up to date Maven dependencies) and add the Tomcat plugin ( quick way to solve an , if you know a better solution, please leave a comment ) as follows:

plugins {
  build “org.grails.plugins:release:3.1.2”
  build “:tomcat:7.0.55”
}

The create-plugin command will generate almost the same objects as a grails application generates. The main difference is the plugin descriptor file, which in my case is called SamplePluginGrailsPlugin.groovy. In this file, you can set the plugin’s version, authors etc.

Next, generate the pom.xml file:

> grails create-pom com.yourcompany

It will look like the following:

<groupId>com.yourcompany</groupId>
<artifactId>sample-plugin</artifactId>
<packaging>grails-plugin</packaging>
<version>0.1</version><name>sample-plugin</name>
<description>sample-plugin</description><properties>
    <grails.version>2.4.4</grails.version>
    <h2.version>1.3.170</h2.version>
</properties><dependencies>
    ...
</dependencies><build>
  <pluginManagement/>
  <plugins>
    ...
  </plugins>
  <repositories>
    ...
  </repositories>
</build>

Now, I am going to add a service called HelloWorldService just for testing purposes.

package sample.pluginclass HelloWorldService {public void hello(String name){
     println “hello ${name}”
  }
}

At this point, you should be able to test if everything is working fine with the following command (make sure you have the Maven command line available — brew install maven — if you are using Mac )

>mvn package -DskipTests
...
[INFO] — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
[INFO] BUILD SUCCESS
[INFO] — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
...

Configuring your Amazon S3 account

Under your account’s name at the top right of your Amazon AWS page, click on Security Credentials. Then at the left menu, click on Users and click on the Create New Users button. I am going to create an user called s3-maven. Then, click Create. Do not forget to save the generated user credentials, it will be necessary later. Now, from the user’s list, single click on top of the created user and then a new window will open where you can attach the AmazonS3FullAccess policy, as shown on the following screen shoot:

We will also need the Amazon Resource Name (ARN) later. Now, we are going to open the Amazon S3 service and Create a new Bucket called myprivatemaven.repository.com . Once created, we select the properties button on the top left of the page and then, we click on Edit bucket policy button. A new window will open. We are going to need something like the following (you can edit your policy by clicking on the bottom left link of the AWS Policy Generator)

{
  "Version": "2012–10–17",
  "Id": "Policy000000000000000",
  "Statement": [
  {
    "Sid": "Stmt000000000000",
    "Effect": "Allow",
    "Principal": {
    "AWS": "arn:aws:iam::99999999999:user/s3-maven"
    },
    "Action": "s3:*",
    "Resource": [
       "arn:aws:s3:::myprivatemaven.repository.com",
       "arn:aws:s3:::myprivatemaven.repository.com/*"
     ]
  }
  ]
}

If you want, you can find more details on these excellent articles from , , and .

At this point, you should have your repository configured on Amazon S3. Time to go back to your terminal in order to make few more configurations.

Configuring your plugin

Now, we need to configure Maven to be able to connect to your S3 bucket. Open your ~/.m2/settings.xml. According to the :

There are two locations where a settings.xml file may live:

The Maven install: $M2_HOME/conf/settings.xml

A user’s install: ${user.home}/.m2/settings.xml

Tip: If you need to create user-specific settings from scratch, it’s easiest to copy the global settings from your Maven installation to your ${user.home}/.m2 directory. Maven’s default settings.xml is a template with comments and examples so you can quickly tweak it to match your needs.

So, if you cannot find the local settings file, like me, just copy from the global configuration file. ( if you want, you can make all changes to the global settings file rather than configuring a private settings.xml just for the current user) . To find the global configuration file, just get the Maven home folder by typing:

> mvn —version
Apache Maven 3.3.3 
Maven home: /usr/local/Cellar/maven/3.3.3/libexec
Java version: 1.7.0_79, vendor: Oracle Corporation
...

Then, you will be able to find settings.xml inside of the conf folder and copy it to your local m2 folder. Next open your local settings.xml and choose an id and include your credentials to the servers list:

<servers>
  <server>
    <id>maven-s3-target-repo</id>
    <username>QWEH8787DSMNQWN2NQ</username>
    <password>***************************************</password>
  </server>
</servers>

Next, add your repository to the pom.xml file and include the (included on the xml code below) to the extensions list. Use the same repository id used on the settings.xml (in my case, maven-s3-target-repo)

<project>
  ...
  <distributionManagement>
    <repository>
      <id>maven-s3-target-repo</id>
      <name>AWS Release Repository</name>
      <url>s3://myprivatemaven.repository.com</url>
    </repository>
  </distributionManagement>
  ...
  <build>
  ...
  <extensions>
    <extension>
      <groupId>org.springframework.build</groupId>
      <artifactId>aws-maven</artifactId>
      <version>5.0.0.RELEASE</version>
      </extension>
  </extensions>
  ...
  </build>
  ...
</project>

If both settings.xml and pom.xml are configured, it is time to deploy your plugin.

> mvn deploy -DskipTests
...
[INFO] — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
[INFO] BUILD SUCCESS
[INFO] — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
...

Testing your private plugin

In order to test if the plugin is available for your apps, lets do the following

> grails create-app mytestapp

Add the following lines to your BuildConfig.groovy

repositories {
 inherits true 
 ...
 mavenRepo(url: "s3://myprivatemaven.repository.com"){
  auth([
   username: "QWEH8787DSMNQWN2NQ",
   password: "******************************************"
  ])
 }
 ...
}
plugins {
 ...
 compile "com.yourcompany:sample-plugin:0.1"
}

If you want hide your credentials, you can take a look at . Now, a quick test, add your new service to your BootStrap

import sample.plugin.HelloWorldServiceclass BootStrap {
  HelloWorldService helloWorldService 
  def init = { servletContext ->
     println helloWorldService.hello("pargles")
  }
  def destroy = {
  }
}

And, run your app.

> grails run-app

Done, you and your team are ready to go secretly !

Please leave your comments, suggestions or questions. Thanks :)

ROSS Intelligence’s Technical Blog

ROSS Intelligence builds technologies to help lawyers scale. Here we will share interesting and useful tech related things that we find along the way.

Pargles Dall'Oglio

Written by

Backend Engineer and co-founder of ROSS Intelligence. Enthusiast in Artificial Intelligence, Groovy/Grails, performance optimization, video compression …

ROSS Intelligence’s Technical Blog

ROSS Intelligence builds technologies to help lawyers scale. Here we will share interesting and useful tech related things that we find along the way.