Configure multiple Maven repositories

What are build profiles?

Apache Maven 2.0 goes to great lengths to ensure that builds are portable. Among other things, this means allowing build configuration inside the POM, avoiding all filesystem references (in inheritance, dependencies, and other places), and leaning much more heavily on the local repository to store the metadata needed to make this possible.

However, sometimes portability is not entirely possible. Under certain conditions, plugins may need to be configured with local filesystem paths. Under other circumstances, a slightly different dependency set will be required, and the project’s artifact name may need to be adjusted slightly. And at still other times, you may even need to include a whole plugin in the build lifecycle depending on the detected build environment.

To address these circumstances, Maven 2.0 introduces the concept of a build profile. Profiles are specified using a subset of the elements available in the POM itself (plus one extra section), and are triggered in any of a variety of ways. They modify the POM at build time, and are meant to be used in complementary sets to give equivalent-but-different parameters for a set of target environments (providing, for example, the path of the appserver root in the development, testing, and production environments). As such, profiles can easily lead to differing build results from different members of your team. However, used properly, profiles can be used while still preserving project portability. This will also minimize the use of -f option of maven which allows user to create another POM with different parameters or configuration to build which makes it more maintainable since it is runnning with one POM only.

What are the different types of profile? Where is each defined?

 

If you would like to point to multiple Maven repositories, create “settings.xml” and add the following code:

<?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>user1</username>
            <password>password123</password>
            <id>central</id>
        </server>

        <server>
            <username>user2</username>
            <password>password123</password>
            <id>snapshots</id>
        </server>

    </servers>

    <profiles>

        <profile>

            <repositories>

                <repository>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                    <id>central</id>
                    <name>libs-release</name>
                    <url>https://mavenrepo.customdomain/artifactory/libs-release</url>
                </repository>

                <repository>
                    <snapshots />
                    <id>snapshots</id>
                    <name>libs-snapshot</name>
                    <url>https://mavenrepo.customdomain/artifactory/libs-snapshot</url>
                </repository>

                <repository>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                    <id>central</id>
                    <url>http://repo1.maven.org/maven2</url>
                </repository>

            </repositories>

            <pluginRepositories>

                <pluginRepository>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                    <id>central</id>
                    <name>plugins-release</name>
                    <url>https://mavenrepo.customdomain/artifactory/plugins-release</url>
                </pluginRepository>

                <pluginRepository>
                    <snapshots />
                    <id>snapshots</id>
                    <name>plugins-snapshot</name>
                    <url>https://mavenrepo.customdomain/artifactory/plugins-snapshot</url>
                </pluginRepository>

                <pluginRepository>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                    <id>central</id>
                    <url>http://repo1.maven.org/maven2</url>
                </pluginRepository>

            </pluginRepositories>

            <id>artifactory</id>

        </profile>

    </profiles>

    <activeProfiles>
        <activeProfile>artifactory</activeProfile>
    </activeProfiles>

</settings>

Now place this file in $USER_HOME/.m2/ directory.

This will enable connection with multiple repositories.

More documentation here.

Advertisements

Working with self-hosted Maven central repository

Ignore server SSL certificate validity check:

mvn -Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true -Dmaven.wagon.http.ssl.ignore.validity.dates=true

 

If you wish to add this setting globally, add an alias to mvn command:

In Linux (Ubuntu), edit ~/.bashrc and add the following line in the bottom of the file.

alias mvn="mvn -Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true -Dmaven.wagon.http.ssl.ignore.validity.dates=true"

Now, put this change into effect immediately.

source ~/.bashrc

Now, whenever you execute a mvn command, it always substitutes the command with mvn -Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true -Dmaven.wagon.http.ssl.ignore.validity.dates=true, which will always ignore the SSL certificate check.

What do these params mean?

  • -Dmaven.wagon.http.ssl.insecure=true – enable use of relaxed SSL check for user generated certificates.
  • -Dmaven.wagon.http.ssl.allowall=true – enable match of the server’s X.509 certificate with hostname. If disabled, a browser like check will be used.
  • -Dmaven.wagon.http.ssl.ignore.validity.dates=true – ignore issues with certificate dates.

 

Official documentation can be found here.

NOTE: The above method was purely intended for developers who constantly keep changing their machines and don’t wish to setup the SSL certificate in every machine. This is for those of you who are lazy! 🙂

Right way of setting up SSL certificates:

Get the valid SSL certificate file from certificate distributor/admin and place it in your machine.

Import the certificate into your Java environment:

keytool -import -keystore /path/to/java_home/lib/security/cacerts -alias mavenrepo -file /path/to/cer/file

You’re now good to go.

NOTE: Now this is a sane method of setting up SSL certificate check for your self-hosted Maven repository