As you start to work more with Maven, you will undoubtedly reach the point where you feel the need for a repository manager – something that manages all of your repositories of artifacts for you. You will likely have a few repositories for different types of artifacts that you care about.
There are a few repository managers out there, with varying capabilities, and some with commercial and free/community versions. In this post, we will use the community version of Artifactory from JFrog.
So the scenario here is that we are going to set up a binary repository to do three things:
- act as a mirror/proxy of Maven Central
- hold our work in progress project builds (snapshots)
- hold our finished products and other dependencies
We will set up Artifactory with three repositories for these three uses, named:
- repo1
- snapshot
- internal
Let’s get started by downloading Artifactory from here.
You can just download the zip file, then unzip it, and start up Artifactory by running the command:
<ARTIFACTORY_DIRECTORY>/bin/artifactoryctl start
You will need to replace <ARTIFACTORY_DIRECTORY> with the path to the
directory where you unzipped Artifactory.
Artifactory will start up and should be accessible at:
http://localhost:8081/artifactory
The first thing you will want to do is login as the admin user (the password is password). Then click on the Admin tab to go to the administration area. If you have to go through an HTTP proxy to get to Maven Central, you will need to define the proxy server details. You can do this in the Proxies page – click on Proxies in the left hand menu.
Then click on the New button, give the proxy a name (key) and fill out the server, port and any other details needed to use the proxy server.
Next, you need to go set the repo1 mirror to use this proxy server. To do this, go to the Repositories page, find repo1 in the Remote Repositories section, highlight it, click on Edit in the popup menu, go to the Advanced Settings tab, and in the Network section, choose the proxy you just defined from the dropdown list.
If you do not need to use a proxy server, you can continue from this point.
If you are not already there, click on the Admin tab and then the Repositories option in the left menu.
In the Local Repositories section (at the top), click on the New button and create a new repository called internal. Select the option to Handle Releases, but do not select Handle Snapshots.
Create another new local repository called snapshot, select the option to Handle Snapshots, but not Handle Releases.
So we have our repositories set up. Now we are going to want to create a Maven settings file to use them.
First, let’s create a master password so that we can store encrypted passwords in our Maven settings file. Run the following command:
mvn -emp some_password
Change ‘some_password’ to a password of your own choosing. Copy the output of this command and put it into a new file in $HOME/.m2/settings-security.xml as follows:
<settingsSecurity> <master>{vF59g61kOG4H/CNqjBmDav77Ne3tm1MChkIWwffySSM=}</master> </settingsSecurity>
Note that your encrypted password will be different to the one shown here.
Now you can encrypt passwords for accessing servers, like our Artifactory server. You will need to authenticate to publish new artifacts into the internal and snapshot repositories. By default, the admin user has the right to publish to these repositories, so you can encrypt the admin user’s password (which is password) as follows:
mvn -ep password
Copy the output of this command, we will need it later on.
Let’s take a look at the Maven settings file that lets us use our Artifactory server:
<settings> <profiles> <profile> <id>default</id> <repositories> <repository> <id>internal</id> <name>internal</name> <url>http://localhost:8081/artifactory/internal</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>snapshot</id> <name>snapshot</name> <url>http://localhost:8081/artifactory/snapshot</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> </profile> </profiles> <activeProfiles> <activeProfile>default</activeProfile> </activeProfiles> <mirrors> <mirror> <id>mirror</id> <name>mirror</name> <url>http://localhost:8081/artifactory/repo1</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> <servers> <server> <id>internal</id> <username>admin</username> <password>{+Ghh1+ORiAIHJ1XKXPEorNjGuMSWUgvXZJYgOaEEncY=}</password> </server> <server> <id>snapshot</id> <username>admin</username> <password>{+Ghh1+ORiAIHJ1XKXPEorNjGuMSWUgvXZJYgOaEEncY=}</password> </server> </servers> </settings>
So let’s review what we have in this file. First we have a profile, called default, which sets up the repositories and tells Maven which ones handle snapshots and releases. Then we activate that profile.
Then we have a mirror entry to tell Maven where to look for any artifact it wants from central.
Finally, we have the servers section which is used to provide credentials for accessing servers. In this case we are providing the encrypted admin user password so that we can publish to these repositories.
Save this file in $HOME/.m2/settings.xml and we are ready to test it out!
To test the mirror, you can use the following command:
mvn help:describe
Ultimately this command will produce an error message, because we have not provided enough arguments, but before that happens, it will need to go download a bunch of artifacts from central, and to do that, it will request them from your mirror. After this command has started running, you can browse the repo1 repository in the Artifactory web interface and you will see it starting to fill up with the various artifacts that Maven needs to execute that command.
So, that tells us that our mirror is working. Next, let’s try to publish a release artifact to the internal repository. You could use the Maven Synchronization Plugin as an example. This will be sitting in your WebLogic Server installation, under $ORACLE_HOME/oracle_common/plugins/maven/com/oracle/maven/oracle-maven-sync/12.1.3
You can install it into the internal repository using this command:
mvn deploy:deploy-file -Dfile=$ORACLE_HOME/oracle_common/plugins/maven/com/oracle/maven/oracle-maven-sync/12.1.3/oracle-maven-sync-12.1.3-0-0.jar -DpomFile=$ORACLE_HOME/oracle_common/plugins/maven/com/oracle/maven/oracle-maven-sync/12.1.3/oracle-maven-sync-12.1.3-0-0.pom -DrepositoryId=internal
After running this command, you can browse the internal repository in the Artifactory web interface and you will see the plugin in there.
Finally, let’s test our snapshot repository. To do that, we need a new project. Grab a new directory, and create a project using one of the simple archetypes, like maven-archetype-quickstart for example. You can do this using this command:
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.1 -DgroupId=com.test -DartifactId=project1 -Dversion=1.0-SNAPSHOT
Then, go into your project1 directory, and add the following distributionManagement section to your pom.xml:
<distributionManagement> <repository> <id>internal</id> <name>internal</name> <url>http://localhost:8081/artifactory/internal</url> </repository> <snapshotRepository> <id>snapshot</id> <name>snapshot</name> <url>http://localhost:8081/artifactory/snapshot</url> </snapshotRepository> </distributionManagement>
Now you can deploy the project! Run this command:
mvn deploy
Then go browse your snapshot repository and you will see your project’s artifact in there!
So that completes the basic setup of a Maven repository manager using Artifactory and configuration of Maven to use it. Enjoy!