Maven - <server/> in settings.xml
Asked Answered
P

2

29

I use tomcat-maven-plugin to deploy my war to a server. What I have to do is configure it like this in my pom.xml:

<configuration>
...
   <url>http://localhost/manager</url>
   <username>admin</username>
   <password>admin</password>
...
</configuration>

But then I obviously want to keep this settings in a different place since I work on my computer but then there's a staging and a live server as well where the settings of the server are different.

So let's use the .m2/settings.xml:

<servers>
    <server>
        <id>local_tomcat</id>
        <username>admin</username>
        <password>admin</password>
    </server>
</servers>

Now change the pom.xml:

<configuration>
    <server>local_tomcat</server>
</configuration>

But where to put the URL of the server? There's no place for that in the settings.xml under the server tag! Maybe like this?

<profiles>
  <profile>
     <id>tomcat-config</id>
      <properties>
    <tomcat.url>http://localhost/manager</tomcat.url>
      </properties>
  </profile>
</profiles>

<activeProfiles>
   <activeProfile>tomcat-config</activeProfile>
</activeProfiles>

..and use the ${tomcat.url} property.

But then the question is, why use the server tag in settings.xml at all? Why not use properties for the username and password as well? Or is there a place for the URL as well in the settings URL so I don't have to use properties?

Phytogenesis answered 1/7, 2011 at 14:46 Comment(0)
M
36

First off let me say, profiles are one of the most powerful features of Maven.

First make a profile in your pom.xml that looks like this:

<profiles>
    <profile>
        <id>tomcat-localhost</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <tomcat-server>localhost</tomcat-server>
            <tomcat-url>http://localhost:8080/manager</tomcat-url>
        </properties>
    </profile>
</profiles>

Then in your ~/.m2/settings.xml file add servers entries like this:

   <servers>
       <server>
           <id>localhost</id>
           <username>admin</username>
           <password>password</password>
       </server>
    </servers>

The configure your build plugin like this:

<plugin>
    <!-- enable deploying to tomcat -->
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>tomcat-maven-plugin</artifactId>
    <version>1.1</version>
    <configuration>
        <server>${tomcat-server}</server>
        <url>${tomcat-url}</url>
    </configuration>
</plugin>

This will enabled your tomcat-localhost profile by default and allow you to deploy to it with a simple mvn clean package tomcat:deploy.

To deploy to other targets, set up a new <server/> entry in settings.xml with the appropriate credentials. Add a new profile but leave off the <activation/> stanza and configure it to point to the appropriate details.

Then to use it do mvn clean package tomcat:deploy -P [profile id] where the [profile id] is the new profile.

The reason that credentials is set in the settings.xml is because your username and password should be secret in most cases, and there is no reason to deviate from the standard way of setting up server credentials that people will have to adapt to.

Moral answered 1/7, 2011 at 20:41 Comment(0)
P
6

settings.xml

<settings>
  <servers>
    <server>
        <id>company.jfrog.io</id>
        <username>user-name</username>
        <password>user-password</password>
    </server>   
  </servers>
</settings>

pom.xml

<repositories>
    <repository>
        <id>company.jfrog.io</id>
        <url>https://company.jfrog.io/company/release</url>
    </repository>
</repositories>

Put settings.xml to

c:/Users/user-name/.m2/settings.xml (for Windows),

~/.m2/settings.xml (for Linux).

company.jfrog.io can be any identifier, but it should be the same in settings.xml and pom.xml.

This works for Maven 3.

Pediatrician answered 15/4, 2019 at 16:51 Comment(4)
how can I apply the same username and password form settings.xml to different repositories? Because I cannot use two or more a repository tags with the same id.Willed
@Willed You need to add another server with a different id to the settings.xmlPediatrician
I have this two repos in the POM <url>https://nexus.mydomain.de/repository/app-snaphots/</url>and <url>https://nexus.mydomain.de/repository/app-releases/</url> As you can see it is the same server, but the one is used for snapshots and the other for releases. I was thinking there should be a way to have only one entry in the settings.xml. Thnx.Willed
@Willed Yes. I want to do the same, but looks like it is impossible.Pediatrician

© 2022 - 2024 — McMap. All rights reserved.