[JAVA] Build Maven repository on AWS S3 service

At the beginning

When using Apache Maven, it is convenient to build an in-house repository, so until now we have built an environment on a web server that can use WebDAV. Since it can be built on S3 on AWS, it is definitely advantageous in terms of cost, so let's use this this time.

Preparation environment

The prepared environment is as follows. Windows7 Pro Java 1.8 Maven 3.3.3 Eclipse4.6 Spring Boot 1.5.6.RELEASE (for demo)

AWS work

AWS can be an existing account, but this time, as a security measure, we have prepared an account that can access S3. I think the access policy should look like this. your-bucketPlease rewrite the place by yourself.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:ListAllMyBuckets"
            ],
            "Resource": "arn:aws:s3:::*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:*"
            ],
            "Resource": [
                "arn:aws:s3:::your-bucket",
                "arn:aws:s3:::your-bucket/*"
            ]
        }
    ]
}

Java & eclipse work

Create a suitable Maven project. I tried it in the Spring Boot project. The following is an excerpt, so please rewrite it if necessary.

pom.xml


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <!-- (abridgement) -->

  <repositories>
    <repository>
      <id>s3-repos</id>
      <name>AWS S3 Repository</name>
      <url>s3://your-bucket/</url>
    </repository>
  </repositories>

  <distributionManagement>
    <repository>
      <id>aws-snapshot</id>
      <name>AWS S3 Repository</name>
      <url>s3://your-bucket/snapshot</url>
    </repository>
  </distributionManagement>

  <dependencies>
    <dependency>
  <!-- (abridgement) -->
    </dependency>
  </dependencies>


  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <!--Supports garbled characters when running Junit in a Windows environment-->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <junitArtifactName>junit:junit</junitArtifactName>
          <encoding>UTF-8</encoding>
          <inputEncoding>UTF-8</inputEncoding>
          <outputEncoding>UTF-8</outputEncoding>
          <argLine>-Dfile.encoding=UTF-8</argLine>
          <!-- <skipTests>true</skipTests> -->
        </configuration>
      </plugin>
    </plugins>
    <extensions>
      <extension>
        <groupId>org.springframework.build</groupId>
        <artifactId>aws-maven</artifactId>
        <version>5.0.0.RELEASE</version>
      </extension>
    </extensions>
  </build>
</project>

After that, set the user settings to be used with the Maven command. For Eclipse, specify Window-> Settings-> Maven-> User Settings or specify user settings when running Maven. I think that it is okay to set the parameters when executing Maven. If proxy settings are required to access S3, add proxy server settings as appropriate.

setting.xml


<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
  <proxies>
    <proxy>
      <id>http_proxy</id>
      <active>true</active>
      <protocol>http</protocol>
      <host>xx.xx.xx.xx</host>
      <port>xx</port>
    </proxy>
    <proxy>
      <id>https_proxy</id>
      <active>true</active>
      <protocol>https</protocol>
      <host>xx.xx.xx.xx</host>
      <port>xx</port>
    </proxy>
    <proxy>
      <id>s3_proxy</id>
      <active>true</active>
      <protocol>s3</protocol>
      <host>xx.xx.xx.xx</host>
      <port>xx</port>
    </proxy>
  </proxies>
  <servers>
    <server>
      <id>aws-release</id>
      <username>Access key ID</username>
      <password>Secret access key</password>
    </server>
    <server>
      <id>aws-snapshot</id>
      <username>Access key ID</username>
      <password>Secret access key</password>
    </server>
  </servers>
</settings>

Execution result

Execute the mvn deploy command to register with S3. I think it's OK to switch between snapshot and release by switching the repository name to access. I think there is another good way, but for the time being, this is it.

[INFO] --- maven-deploy-plugin:2.8.2:deploy (default-deploy) @ ews ---
[INFO] Uploading: s3://your-bucket/snapshot/com/example/1.0.0/sample-1.0.0.jar
[INFO] Configuring Proxy. Proxy Host: xx.xx.xx.xx Proxy Port: xx
[INFO] Uploaded: s3://your-bucket/snapshot/com/example/1.0.0/sample-1.0.0.jar (2000 KB at 1000.0 KB/sec)
[INFO] Uploading: s3://your-bucket/snapshot/com/example/1.0.0/sample-1.0.0.pom
[INFO] Uploaded: s3://your-bucket/snapshot/com/example/1.0.0/sample-1.0.0.pom (5 KB at 1.0 KB/sec)
[INFO] Downloading: s3://your-bucket/snapshot/com/example/maven-metadata.xml
[INFO] Uploading: s3://your-bucket/snapshot/com/example/maven-metadata.xml
[INFO] Uploaded: s3://your-bucket/snapshot/com/example/maven-metadata.xml (300 B at 0.1 KB/sec)

Consideration

I searched on the internet, but I didn't understand the procedure well, so I actually tried it. I haven't tried to pull it from other projects, but if you don't want to make the jar file public, you can use it quite a bit.

Recommended Posts

Build Maven repository on AWS S3 service
Build a Maven repository on AWS S3
Build a Maven in-house repository on Google Cloud Storage
Dependency management in Gradle using Maven repository on Amazon S3
Build a Minecraft server on AWS
Build Redmine on Azure App Service
Build Java 8 development environment on AWS Cloud9
Build an environment with Docker on AWS
How to save images on Heroku to S3 on AWS
Build a Laravel environment on an AWS instance
Memo to build a Servlet environment on AWS EC2
Build a Ruby on Rails development environment on AWS Cloud9
Maven repository local cache
Maven on CentOS 7 tutorial
Build Zabbix on Ubuntu 20.04
[Amateur remarks] Build multiple WordPress on AWS using Docker Compose