This is a PoC of running a self hosted Apache Maven Repository, Reposilite, and use it as a distribution management.
Just running the usual mvn clean deploy
, but deploy the jar into Reposilite.
All Apache Maven commands will run inside the Apache Maven Docker image
Building Maven containers needs to 'see' Reposilite ones. So we are going to create a network so as to all containers can see each other.
docker network create -d bridge maven-network
All containers need to be started with --network=maven-network
option
Another option could be connecting all maven container to this reposilite container network
We are going to use the Apache Maven Docker image, available here
Reposilite is described by its authors as a lightweight repository manager for Maven artifacts.
It is also mentioned in Apache Maven documentation
docker run -it --network=maven-network -v reposilite-data:/app/data -p 80:80 --name reposilite dzikoysk/reposilite:latest
In order to deploy artifacts into the repository, we need a user with write permissions, as stated in the Official documentation
Do not bash into the Reposilite running container, start it in interactive mode.
keygen /snapshots snapshot w
13:46:40.108 INFO |
13:46:40.216 INFO | Stored tokens: 1
13:46:40.216 INFO | Generated new access token for snapshot (/snapshots) with 'w' permissions
13:46:40.216 INFO | /0Oog+M0YnoLoYhe9EklYSvFHTkXt1geqGMD3l/8MutA8lW9k0XMyCvBNxS8s/2+
Keep at hand this information, because it will be needed when configuring Maven in next steps.
I have just created a user called snapshot
with access to snapshots
repository. Maybe not the best naming strategy, it may lead to missunderstandings.
We are trying to deploy a Java project resulting artifact into Reposilite. You can use any Java project of your own. Just in case you do not have access to any, let us make a quick one.
Follow this guide if you want to know more about Quarkus.
docker run -it --rm -v /mnt/c/Users/yo/.m2/repository:/root/.m2/repository -v $(pwd):/src -w /src maven:3-openjdk-11 mvn io.quarkus:quarkus-maven-plugin:1.11.3.Final:create -DprojectGroupId=org.acme -DprojectArtifactId=getting-started -DclassName="org.acme.getting.started.GreetingResource" -Dpath="/hello"
- -v /mnt/c/Users/yo/.m2/repository:/root/.m2/repository, my local repository, so I do not neet to download all dependencies all times along
- -v $(pwd):/src, the project is going to be generated at . in my host
- -w /src, working directory for the Apache Maven container
- maven:3-openjdk-11, image and tag
- rest of options are quarkus dependant. You can check more about them in the doc
You will end up with something like this
.
└── getting-started
├── README.md
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
├── main
│ ├── docker
│ │ ├── Dockerfile.fast-jar
│ │ ├── Dockerfile.jvm
│ │ └── Dockerfile.native
│ ├── java
│ │ └── org
│ │ └── acme
│ │ └── getting
│ │ └── started
│ │ └── GreetingResource.java
│ └── resources
│ ├── META-INF
│ │ └── resources
│ │ └── index.html
│ └── application.properties
└── test
└── java
└── org
└── acme
└── getting
└── started
├── GreetingResourceTest.java
└── NativeGreetingResourceIT.java
This is really not important. As any Java project will be OK, provinding it generate an artifact.
Apache Maven distributionManagement reference
Add the following into pom.xml
file of your Java project.
<distributionManagement>
<repository>
<id>reposilite</id>
<url>http://reposilite/snapshots</url>
</repository>
</distributionManagement>
- id, you can choose whatever id suits you, but it needs to be the same you specify later in
settings.xml
file - url, the url of your reposilite. Keep in mind any network issues you may have changed from the configuration in this document.
Apache Maven settings.xml reference
Just make an empty file and name it settings.xml
. It does not need to be inside the Java project. It will be mounted it into the Apache Maven image.
<settings>
<servers>
<server>
<id>reposilite</id>
<username>snapshot</username>
<password>/0Oog+M0YnoLoYhe9EklYSvFHTkXt1geqGMD3l/8MutA8lW9k0XMyCvBNxS8s/2+</password>
</server>
</servers>
</settings>
You need to use the exact same id
you used in pom.xml
file. And the same user
and password
you used when configuring auth in Reposilite
docker run -it --rm --network=maven-network -v $(pwd)/settings.xml:/root/.m2/settings.xml -v /mnt/c/Users/yo/.m2/repository:/root/.m2/repository -v $(pwd)/getting-started:/src -w /src maven:3-openjdk-11 mvn -Dmaven.test.skip=true clean deploy
- -v $(pwd)/settings.xml:/root/.m2/settings.xml, we mount the previous composed
setting.xml
file - -v /mnt/c/Users/yo/.m2/repository:/root/.m2/repository, we reuse our local repository
- -v $(pwd)/getting-started:/src, we mount the Java project source folder inside the container
- -w /src, we change the container working directory to the source folder mounted before
- maven:3-openjdk-11, we use a jdk11 image
- mvn -Dmaven.test.skip=true clean deploy, the regular
mvn clean deploy
command. We ommit testing stuff to make a quicker build. This is only for learning purposes.
After a while, you will see the jar is uploaded to Reposilite
...
[INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ getting-started ---
Uploaded to reposilite: http://reposilite/snapshots/org/acme/getting-started/1.0.0-SNAPSHOT/getting-started-1.0.0-20210220.151633-3.jar (5.4 kB at 15 kB/s)
Uploaded to reposilite: http://reposilite/snapshots/org/acme/getting-started/1.0.0-SNAPSHOT/getting-started-1.0.0-20210220.151633-3.pom (4.5 kB at 13 kB/s)
Uploaded to reposilite: http://reposilite/snapshots/org/acme/getting-started/1.0.0-SNAPSHOT/maven-metadata.xml (0 B at 0 B/s)
Uploaded to reposilite: http://reposilite/snapshots/org/acme/getting-started/maven-metadata.xml (0 B at 0 B/s)
Point your browser to http://localhost/snapshots/org/acme/getting-started/1.0.0-SNAPSHOT
and you will see your jar published
If you get something similar to his. You may have some missconfiguration regarding the user.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project getting-started: Failed to deploy artifacts: Could not transfer artifact org.acme:getting-started:jar:1.0.0-20210220.132514-1 from/to local-repository (http://reposilite/snapshots): Transfer failed for http://reposilite/snapshots/org/acme/getting-started/1.0.0-SNAPSHOT/getting-started-1.0.0-20210220.132514-1.jar 401 Unauthorized -> [Help 1]