How can I configure the maven to cope with both corporate/VPN and public repository setups?

5k views Asked by At

I need a setup to be able to compile a Java Maven project using either the:

  • corporate Maven repository (intranet Artifactory proxying Maven Central and others)
  • public Maven repositories (Central, ICM, io.confluent, etc.)

The project POM code already references the internal corporate repository:

<repositories>
    <repository>
        <id>artifactory-virtual</id>
        <name>My Artifactory Repository to download</name>
        <url>https://artifactory.corporation.com/maven</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

But to work with this repository requires a VPN connection that I would like to avoid.

I am trying to setup a repository mirror as using this Maven documentation.

Hence I am configuring in my ~/.m2/settings.xml the following configuration:

<?xml version="1.0" encoding="UTF-8"?>
<settings>
    ...
    <mirrors>
        <mirror>
            <id>icm-mirror</id>
            <url>http://maven.icm.edu.pl/artifactory/repo/</url>
            <mirrorOf>*,!central</mirrorOf>
        </mirror>
    </mirrors>
    ...
</settings>

but just providing a mirror alone (for all repos except central), the project is still trying to download from the corporate repository (I am not providing any):

[ERROR] Failed to execute goal on project ibis-customer-management:
    Could not resolve dependencies for project com.example.project:myproject:jar:3.7.1-SNAPSHOT:
    Failed to collect dependencies at com.oracle.jdbc:ojdbc8:jar:18.3.0.0:
    Failed to read artifact descriptor for com.oracle.jdbc:ojdbc8:jar:18.3.0.0: Could not transfer artifact com.oracle.jdbc:ojdbc8:pom:18.3.0.0 from/to artifactory-virtual (https://artifactory.corporation.com/maven):
    Transfer failed for https://artifactory.corporation.com/maven/com/oracle/jdbc/ojdbc8/18.3.0.0/ojdbc8-18.3.0.0.pom:
    Connect to artifactory.corporation.com [artifactory.corporation.com/1.2.3.4] failed:
    Operation timed out -> [Help 1]

The error is not fixed even if I create an active profile with an explicit entry for the ICM repository:

<settings>
    ...
    <mirrors>
        <mirror>
            <id>icm-mirror</id>
            <url>http://maven.icm.edu.pl/artifactory/repo/</url>
            <mirrorOf>*,!central</mirrorOf>
        </mirror>
    </mirrors>

    <profiles>
        <profile>
            <id>not-corpnet</id>

            <repositories>
                <repository>
                    <id>icm</id>
                    <name>ICM Repository</name>
                    <url>http://maven.icm.edu.pl/artifactory/repo/</url>
                </repository>
            </repositories>
        </profile>
    </profiles>

    <activeProfiles>
        <activeProfile>not-corpnet</activeProfile>
    </activeProfiles>
    ...
</settings>

If you think there might be anything else in my settings.xml, the above one is pretty much the complete one (without the dots, and with the correct XML namespaces.

The profile is also active and visible when I run mvn

[INFO] --- maven-help-plugin:3.2.0:active-profiles (default-cli) @ myproject ---
[INFO]
Active Profiles for Project 'com.example.project:myproject:pom:3.7.1-SNAPSHOT':

The following profiles are active:

 - not-corpnet (source: external)

How can I make my project, which has already inside its POM the corporate repository, to download artifacts from public repositories when I am not in the corporate network/VPN?

1

There are 1 answers

0
Gabriel Petrovay On BEST ANSWER

We finally reached an OK setup that is working the same way from both public and corporate networks by doing the following:

  • the project pom.xml contains the required public repositories:

    <repositories>
        <repository>
            <id>central</id>
            <name>Central Repository</name>
            <url>https://repo1.maven.org/maven2/</url>
        </repository>
        <repository>
            <id>icm</id>
            <name>ICM Repository</name>
            <url>http://maven.icm.edu.pl/artifactory/repo/</url>
        </repository>
        <repository>
            <id>confluent</id>
            <name>Confluent Repository</name>
            <url>http://packages.confluent.io/maven/</url>
        </repository>
    </repositories>
    

    Assuming that the project does not require non-public dependencies, this will allow "checkout and run" way of work in public environments.

  • if we are inside the corporate network (with access to the private repositories) and behind the corporate proxy, we use a settings.xml file that each developer can use (~/.m2/serttings.xml) for development inside the corporate environment. This file contains <proxies> and <mirrors>:

    <mirrors>
        <mirror>
            <id>internal-repository-mirror</id>
            <name>Maven Mirror</name>
            <url>https://artifactory.corporation.com/maven</url>
            <mirrorOf>*,!confluent</mirrorOf>
        </mirror>
        <mirror>
            <id>internal-confluent-mirror</id>
            <name>Confluent Mirror</name>
            <url>https://artifactory.corporation.com/confluent-maven</url>
            <mirrorOf>confluent</mirrorOf>
        </mirror>
    </mirrors>
    
    <proxies>
        <proxy>
            <id>corpnet-http-proxy</id>
            <active>true</active>
            <protocol>http</protocol>
            <host>proxy.corporation.com</host>
            <port>8079</port>
            <nonProxyHosts>artifactory.corporation.com</nonProxyHosts>
        </proxy>
        <proxy>
            <id>corpnet-https-proxy</id>
            <active>true</active>
            <protocol>https</protocol>
            <host>proxy.corporation.com</host>
            <port>8079</port>
            <nonProxyHosts>artifactory.corporation.com</nonProxyHosts>
        </proxy>
    </proxies>
    

No other repositories or profiles are necessary in the settings.xml file.

Regarding how the settings.xml is being exchanged in an automatic way, I use the following approach which suits my way of work where I always have a terminal open:

  • when I start a terminal window/tab the ~/.bash_profile (or equivalent) is being executed
  • depending on the network I am on (public or corporate/VPN) I copy/overwrite one of the two existing settings.xml files I have prepared: ** settings-public.xml almost empty (might have some special repositories that I use while playing/experimenting with code) ** settings-corpnet.xml containing at least the <mirrors> and <proxies> sections pointed above