GridDragon
GridDragon

Reputation: 3135

How to combine maven settings files

I've been added to another team at work. Both teams use their own Nexus servers, and have provided me their own settings files, so currently I have to do a lot of hoop-jumping to make sure I'm using the correct settings.xml file to get the projects to build. Is there a good way to merge these files? I've been going through the maven documentation but getting really confused. Settings files are below (identifying info removed)

Team 1 settings file

<settings>
 <mirrors>
   <mirror>
    <id>Nexus</id>
    <name>Company Nexus Public Mirror</name>
    <url>http://build.company.com/nexus/content/groups/public/</url>
    <mirrorOf>central</mirrorOf>
   </mirror>
  </mirrors>
 <servers>
   <server>
    <id>snapshots</id>
    <username>deployment</username>
    <password>password1</password>
   </server>
   <server>
    <id>releases</id>
    <username>deployment</username>
    <password>password2</password>
   </server>
  </servers>
 <activeProfiles>
   <activeProfile>jenkins</activeProfile>
  </activeProfiles>
</settings>

Team 2 settings file

<settings>
    <mirrors>
        <mirror>
            <!--This sends everything else to /public -->
            <id>nexus</id>
            <mirrorOf>*</mirrorOf>
            <url>http://domain.company.com:8081/nexus/content/groups/public</url>
        </mirror>
    </mirrors>
    <profiles>
        <profile>
            <id>nexus</id>
            <!--Enable snapshots for the built in central repo to direct -->
            <!--all requests to nexus via the mirror -->
            <repositories>
                <repository>
                    <id>central</id>
                    <url>http://central</url>
                    <releases><enabled>true</enabled></releases>
                    <snapshots><enabled>true</enabled></snapshots>
                </repository>
            </repositories>
         <pluginRepositories>
                <pluginRepository>
                    <id>central</id>
                    <url>http://central</url>
                    <releases><enabled>true</enabled></releases>
                    <snapshots><enabled>true</enabled></snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>
    <activeProfiles>
        <!--make the profile active all the time -->
        <activeProfile>nexus</activeProfile>
    </activeProfiles>
    <servers>
        <server>
            <id>snapshots</id>
            <username>deployment</username>
            <password>password3</password>
        </server>
        <server>
               <id>releases</id>
               <username>deployment</username>
               <password>password4</password>
        </server>

    </servers>
</settings>

I tried just merging the <mirrors> sections so both mirrors were listed, but only one team's code builds. I also don't understand how the server id's work. I'll have different username/password combinations for the different servers. I'd think the server id should match the id in the mirrors sections, but that clearly isn't the case. But if not that, how would maven know which snapshots or releases server to use?

Upvotes: 5

Views: 3233

Answers (3)

You have to check what settings are maven taking during the deployment of the artifacts by using this command:

mvn help:effective-settings

Sometimes it can happen that you are using local settings and global settings (taking first the provided by the maven installation and then the provided by the repository), but maven is not recognizing both. With this command, you can verify the actual settings maven is taking during the deployment. In addition, you can locate the actual file maven is taking and, as a consequence, modify the file properly (with vim, for example) according to the configuration needed by the server.

Remember that there are two locations where a settings.xml file may live:

  1. Maven installation directory

    $M2_HOME/conf/settings.xml

  2. User-specific settings file

    ~/.m2/settings.xml

If both files exists, their contents gets merged, with the user-specific settings.xml being dominant. https://maven.apache.org/settings.html

Upvotes: 4

Bernhard Colby
Bernhard Colby

Reputation: 319

If you want to merge several settings file into one, you can (should) use XInclude. It is a W3C recommendation and the best solution for your case.

There is a great tutorial about the usage of Xinclude. Here is the link

I hope this helps.

Upvotes: -1

Manfred Moser
Manfred Moser

Reputation: 29912

In my experience there are really only one feasible and easy solution and it does NOT include merging the settings files.

Switch settings files whenever necessary. Just have both files somewhere and have a command line script that switches them (copying over top of settings.xml from a template file). Or alternatively .. have different commands that pass in -s .. problem with that is that you this does not integrate nicely with the IDE usage.

If the id values for the different servers use different names you could also install a Nexus Repository Manager locally on your machine and proxy the two external ones. But in your scenario that wont work since overlapping id values for different servers are used. That must stem from a misunderstanding on both teams on what the id is used for.

Upvotes: 0

Related Questions