Saturday, April 28, 2018

Customize maven to fetch and push to other repositories

Maven Customization

Settings.xml (Understanding)


When we install maven either there will not be any settings.xml (in .m2 folder) or the settings present in conf folder will have default settings. Hence we must first understand the importance of settings.xml file.

The first task for us is to identify where is the settings.xml in the file system for you to go and change. Either you would use maven using Sts/eclipse in which case the settings file will not be present by default. In this case you must create settings.xml file in the folder <<UserHomeDirectory>>/.m2 

If you have downloaded the maven installation then the settings.xml file would be present with default settings under for folder <<MavenHome>>/conf

Settings.xml (Explained)



<?xml version="1.0" encoding="UTF-8"?>
<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <servers> 
        <server>
            <username>admin</username> 
            <password>Darkhorse@123</password>
            <id>remote</id>
        </server>
    </servers>
    <distributionManagement> 
        <repository>
            <id>remote</id>
            <name>Darkhorses-MacBook-Pro-4.local-releases</name>
            <url>http://192.168.50.9:8081/artifactory/dh-repo1</url>
        </repository>
    </distributionManagement>
    <profiles> 
        <profile>
            <id>dh-repo1</id>
            <repositories>
                <repository>
                    <id>remote</id>
                    <name>Darkhorses-MacBook-Pro-4.local-releases</name>
                    <url>http://192.168.50.9:8081/artifactory/dh-repo1</url>
                </repository>
            </repositories>
        </profile>
    </profiles>
    <activeProfiles> 
        <activeProfile>dh-repo1</activeProfile>
    </activeProfiles>
</settings>

Further explanation :

Servers : It stores the credentials to connect to your distributionManagement repositories.

ID : This is the identifier with which it identifies the repository of distributionManagement. You can have multiple repositories in distributionManagement tag and each repository's credentials are identified by this tag

Distribution Management : This is used basically to push/deploy the application in some other repository apart from your local one. The server where you would deploy is provided in below url tag.

Profiles :  This stores the information of multiple profiles. Each profile can have multiple repositories defined which connects to different servers.

ActiveProfiles : Settings which you can change anytime to tell maven that fetch from which repository. This is used especially in situations where you want do refer dependencies from various different servers depending on the application.

Deployment to Custom repo :

mvn deploy -DaltDeploymentRepository=remote::default::http://192.168.50.9:8081/artifactory/dh-repo1
If you want to do it from sts, below image shows how to achieve that


It requires two things,

  • Goals : Entire command without mvn.
  • User Settings : settings.xml path which will be used to fetch distributionManagement details and deploy to that repository.
I hope I am clear in explaining how to fetch and how to publish to custom repositories.