简体   繁体   中英

Multiple maven repositories with a known unknown host

Description

I'm configurating a maven build, to execute in two different environments, the first one in my localhost, and the second one inside a docker cluster using jenkins.

Both builds will use the same pom.xml file. Inside the pom.xml file I'm referencing a private nexus repository.

The nexus repository is inside the docker cluster.

Both the URL's defined inside pom.xml redirect to the same nexus repository.

So to access the nexus repository from my localhost, we've configured our private DNS to resolve "git.consignet.intranet" to the nexus repo. And then to access the nexus repo inside docker, I can use just its service name "nexus-repo".

The pom.xml's relevant contents are shown below:

<project>
    ...
    <repositories>
        <repository>
            <id>nexus-aws</id>
            <name>Nexus Amazon</name>
            <url>http://nexus.consignet.intranet/repository/maven-releases/</url>
        </repository>
        <repository>
            <id>nexus-cluster</id>
            <name>Nexus Inside Cluster</name>
            <url>http://nexus-repo/repository/maven-releases/</url>
        </repository>
    </repositories>
    ...
</project>

Problem

While building in my localhost I'm not able to access the repo using its service name inside docker to resolve to the host.

While building inside jenkins (Inside the docker container), I'm not able to access our DNS server to resolve the URL.

Maven output

Maven throws the following error message:

[ERROR] Failed to execute goal on project DB1ConsignetWebService: Could not 
resolve dependencies for project DB1ConsignetWebService:DB1ConsignetWebService:war:1.0-SNAPSHOT: 
Failed to collect dependencies at Consignet:jaxb-api:jar:2.2.7-facets-1.0.5:
Failed to read artifact descriptor for Consignet:jaxb-api:jar:2.2.7-facets-1.0.5: 
Could not transfer artifact Consignet:jaxb-api:pom:2.2.7-facets-1.0.5
from/to my-repo1 (http://nexus-repo/repository/maven-releases/): nexus-repo: 
Unknown host nexus-repo -> [Help 1]

Final considerations

Only one URL can be resolved in each environment:

Inside development environment, I'll just be able to resolve the "git.consignet.intranet" dns.

Inside jenkins build environment, I'll just be able to resolve the "nexus-repo" dns.

Question

Is it possible to ignore a repository in maven if it is a unknown host? If so, how can I configure it?

Use profiles:

<profiles>
    <profile>
         <id>dev</id>
         <repositories>
            <repository>
                <id>nexus-aws</id>
                <name>Nexus Amazon</name>
                <url>http://nexus.consignet.intranet/repository/maven-releases/</url>
            </repository>
        </repositories>
    </profile>

    <profile>
        <id>jenkins</id>
        <repositories>
            <repository>
                <id>nexus-cluster</id>
                <name>Nexus Inside Cluster</name>
                <url>http://nexus-repo/repository/maven-releases/</url>
            </repository>
        </repositories>
    <profile>
</profiles>

Then build with mvn -Pjenkins or mvn -Pdev , depending on which environment you are in. The problem is now (hopefully) reduced to reading environment flags.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM