For the uninitiated, Artifactory is a repository manager that integrates really well with host of build teachnologies like ant, maven, gradle. Setting up a local maven repository may not make sense for an individual developer, but it can certainly cater well to small to medium sized teams. A quick introduction screencast video on artifactory website got me curious to check this out.
Setting up Artifactory on linux machine was simpler than I thought. Just download the zip file from official site, extract the contents and run the script file to start Artifactory. Thats all. Artifactory has a web interface that allows user manage settings, repository configurations etc. More on that later.
To login as admin, use default username/password as admin/password
Alternatively the Artifactory can also be setup as a service. Just need to execute the installservice script as root. The script is available in the bin folder of install location.
sudo ./installService.sh
This makes Artifactory available as a service and can be used like one. To check status, or start / stop script use following commands
service artifactory [ status | start | stop ]
Thats as quick as we can get to have a working Artifactory instance on a dev machine. Next up is how to integrate this with Maven. By default, maven will look to resolve its dependencies by trying to find them on repo1.maven.org. To setup maven to look up these artifacts on Artifactory we need to tweak the settings.xml file found in .m2 folder. If one doesnt exist yet, artifactory has a settings section which allows users to quickly create one. On the Artifactory UI, click on Maven settings under Client settings. The Maven Settings Generator has few options that user can tweak. The defaults are sufficient for local use. Click on ‘Generate Settings’. This creates a default settings.xml file which can be dropped into the .m2 folder. Maven will now look for artifacts on the local instance of artifactory which in turn would get the artifacts from repo1.maven.org.
Executing maven commands on a local project now will show console output similar to the one below. This implies the artifactory setup in now in place and well integrated with maven.
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------------------------------------------------------
[INFO] Building maven1 0.0.1-SNAPSHOT
[INFO] ---------------------------------------------------------------------
Downloading: http://localhost:8081/artifactory/plugins-release/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom
Downloaded: http://localhost:8081/artifactory/plugins-release/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom (4 KB at 1.5 KB/sec)
Downloading: http://localhost:8081/artifactory/plugins-release/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.jar
Downloaded: http://localhost:8081/artifactory/plugins-release/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.jar (25 KB at 23.9 KB/sec)
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven1 ---
[INFO] Deleting /home/koolksp/Dev/IdeaProjects/maven1/target
[INFO] ---------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ---------------------------------------------------------------------
[INFO] Total time: 5.626 s
[INFO] Finished at: 2014-10-08T10:00:47+05:30
[INFO] Final Memory: 6M/71M
[INFO] ---------------------------------------------------------------------
Here the required artifacts are supplied by the artifactory’s repository. For a team for several developers, it makes sense to setup a artifactory maven setup and source their dependencies from it, rather than individually managing them from maven repos. This also makes artifact sharing within team members effortless and painless. For instance, say a library developed by one developer needs to be used by another. Since it is not published to maven yet, the second developer has little option but to manually make the library available in his m2 repository and continue with his development activities. Also if the library itself is in active development and more changes are anticipated, the second developer has to keep maintaining newer versions locally, losing on precious time, rather than working on his own code.
Artifactory will work well for both here. A stable release of some library can be deployed onto Artifactory, which is then available for other developers to work with. All they would need to do is add the proper artifact and version to their respective pom xml files.
To illustrate deployment of artifacts, I am going to run through my sample project again. The maven properties for this are
<groupId>com.sample</groupId>
<artifactId>maven1</artifactId>
<version>0.0.1-SNAPSHOT</version>
I do a mvn clean install
and find that the generated jar is available at location ~/.m2/repository/com/sample/maven1/0.0.1-SNAPSHOT/. When I check the same on artifactory repository browser, I do not see the jar! The reason being, maven installed the jar into its m2 repo, but has no configuration information to publish an artifact to Artifactory.
For this we need to set up the distribution management for Maven. Artifactory manages local releases into separate repositories. So we have a libs-release-local and libs-snapshot-local repositories which need to be set up with maven to get deployment working.
Add the distribution management section from artifactory to project pom. Change <id> value to something suitable. Now go to .m2/settings.xml and edit the servers section to add a server configuration with username password and id. This is the same id as configured in distribution management. I am adding two configurations for artifact distribution to snapshot-local and release-local repositories.
So the distribution management section of the project pom looks like so
<distributionManagement>
<repository>
<id>releases</id>
<name>koolksp-3000-N100-releases</name>
<url>http://localhost:8081/artifactory/libs-release-local </url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>koolksp-3000-N100-snapshots</name>
<url>http://localhost:8081/artifactory/libs-snapshot-local </url>
</snapshotRepository>
</distributionManagement>
and the settings.xml reflects this as
<servers>
<server>
<username>admin</username>
<password>password</password>
<id>releases</id>
</server>
<server>
<username>admin</username>
<password>password</password>
<id>snapshots</id>
</server>
</servers>
Now doing a mvn clean deploy uploads the snapshot release to the libs-snapshot-local repo.
Uploading: http://localhost:8081/artifactory/libs-snapshot-local/com/sample/maven1/0.0.1-SNAPSHOT/maven-metadata.xml
Uploaded: http://localhost:8081/artifactory/libs-snapshot-local/com/sample/maven1/0.0.1-SNAPSHOT/maven-metadata.xml (766 B at 32.5 KB/sec)
Uploading: http://localhost:8081/artifactory/libs-snapshot-local/com/sample/maven1/maven-metadata.xml
Uploaded: http://localhost:8081/artifactory/libs-snapshot-local/com/sample/maven1/maven-metadata.xml (312 B at 3.3 KB/sec)
[INFO] ---------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ---------------------------------------------------------------------
The snapshot release is now available on artifactory.
Final releases will go into the libs-release-local repository.
Hope this gives sufficient insight to start using Artifactory for all dependency management needs for an individual as well as team of active developers.