Maven-<服务器/>在settings.xml中 [英] Maven - <server/> in settings.xml

查看:79
本文介绍了Maven-<服务器/>在settings.xml中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用tomcat-maven-plugin将战争部署到服务器.我要做的就是在pom.xml中像这样配置它:

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.

所以我们使用.m2/settings.xml:

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

现在更改pom.xml:

Now change the pom.xml:

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

但是将服务器的URL放在哪里?在server标记下的settings.xml中没有任何位置!也许是这样吗?

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>

..并使用$ {tomcat.url}属性.

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

但是接下来的问题是,为什么要完全使用settings.xml中的服务器标签?为什么不同时使用用户名和密码的属性?还是在设置URL中也有该URL的位置,所以我不必使用属性?

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?

推荐答案

首先让我说,profiles是Maven最强大的功能之一.

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

首先在您的pom.xml中创建一个如下所示的配置文件:

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>

然后在您的~/.m2/settings.xml文件中添加servers条目,如下所示:

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>

像这样配置build插件:

<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>

这将默认启用您的tomcat-localhost配置文件,并允许您使用简单的mvn clean package tomcat:deploy对其进行部署.

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

要部署到其他目标,请使用适当的凭据在settings.xml中设置一个新的<server/>条目.添加一个新的profile,但保留<activation/>节,并将其配置为指向适当的详细信息.

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.

然后使用它执行mvn clean package tomcat:deploy -P [profile id],其中[profile id]是新的配置文件.

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

settings.xml中设置凭据的原因是,在大多数情况下,您的用户名和密码应该是机密的,没有理由偏离人们必须适应的标准服务器凭据设置方式

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.

这篇关于Maven-&lt;服务器/&gt;在settings.xml中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆