Deploying artifacts to remote repository is our next goal in Maven process upon completing successful builds of Artifacts.

Configure settings.xml for Remote Repository Connectivity

Remote repository (eg., Artifactory) is both a source location for artifacts needed for a build, and a target place to deploy generated artifacts. Artifactory can be configured in Maven using settings.xml file located under your Maven home directory as below. For more information on configuring Maven please refer to the  Apache Maven Project Settings Reference.

<repositories>
    <repository>
        <id>central</id>
        <url>http://[host]:[port]/artifactory/libs-release</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>snapshots</id>
        <url>http://[host]:[port]/artifactory/libs-snapshot</url>
        <releases>
            <enabled>false</enabled>
        </releases>
    </repository>
</repositories>

Configuring Authentication for Remote Repository or Artifactory

Remote repository or Artifactory sometimes requires user authentication in following cases such as to avoid anonymous access, when access to repositories is limited set of users and when deploying builds is restricted publicly.

Authentication is configured in above cases using <server> elements in the settings.xml file of Maven.

Each <repository> and <mirror> element specified in the file must have a corresponding <server> element with a matching <id> that specifies the username and password.

In both cases the username is admin and the password is encrypted.

...

<servers>
    <server>
       <id>central</id>
       <username>admin</username>
       <password>\{JHSsydn\}Jksteyolqxi/0tdg==</password>
    </server>
    <server>
       <id>snapshots</id>
       <username>admin</username>
       <password>\{JHSsydn\}Jksteyolqxi/0tdg==</password>
    </server>
</servers>

...

Command for deploying Artifacts to remote repository

After setting up connectivity to remote repository in settings.xml – use the deploy:deploy-file goal under maven-deploy-plugin for deploying artifacts.

Use below command to execute deploy,

mvn deploy:deploy-file -DgroupId=<group-id> \
  -DartifactId=<artifact-id> \
  -Dversion=<version> \
  -Dpackaging=<type-of-packaging> \
  -Dfile=<path-to-file> \
  -DrepositoryId=<id-to-map-on-server-section-of-settings.xml> \
  -Durl=<url-of-the-repository-to-deploy>

Deploying Artifacts with a customized POM

If a POM is already existing for the 3rd Party JAR and you want to deploy it together with the JAR we should use the pomFile argument of the deploy-file goal. See sample below.

mvn deploy:deploy-file -DpomFile=<path-to-pom> \
  -Dfile=<path-to-file> \
  -DrepositoryId=<id-to-map-on-server-section-of-settings.xml> \
  -Durl=<url-of-the-repository-to-deploy>

Note that groupIdartifactIdversion and packaging arguments are not included here because deploy-file goal will get these information from the given POM.

Conclusion

Hence we went through following topics as guide for deploying artifacts to remote repository,

  1. Configuring remote repositories in settings.xml
  2. Authentication setup for repositories in settings.xml
  3. Command to deploy artifacts and with customized POM-file

Refer link to checkout more about settings.xml configuration.

Please follow and like us: