【问题标题】:Maven - <server/> in settings.xmlMaven - settings.xml 中的 <server/>
【发布时间】:2011-07-01 14:46:23
【问题描述】:

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

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

但是我显然想将此设置保留在不同的位置,因为我在我的计算机上工作,但是还有一个暂存服务器和一个实时服务器,其中服务器的设置不同。

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

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

现在更改 pom.xml:

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

但是将服务器的 URL 放在哪里呢?在 server 标签下的 settings.xml 中没有那个地方!也许是这样?

<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} 属性。

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

【问题讨论】:

    标签: maven maven-tomcat-plugin


    【解决方案1】:

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

    首先在您的pom.xml 中创建一个如下所示的个人资料:

    <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 条目,如下所示:

       <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 对其进行部署。

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

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

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

    【讨论】:

      【解决方案2】:

      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>
      

      settings.xml放到

      c:/Users/user-name/.m2/settings.xml(适用于 Windows),

      ~/.m2/settings.xml(适用于 Linux)。

      company.jfrog.io 可以是任何标识符,但在settings.xmlpom.xml 中应该相同。

      这适用于 Maven 3。

      【讨论】:

      • 如何将相同的用户名和密码表单 settings.xml 应用到不同的存储库?因为我不能使用两个或多个具有相同 id 的存储库标签。
      • @Elio 您需要在settings.xml 中添加另一个具有不同idserver
      • 我在 POM &lt;url&gt;https://nexus.mydomain.de/repository/app-snaphots/&lt;/url&gt;&lt;url&gt;https://nexus.mydomain.de/repository/app-releases/&lt;/url&gt; 中有这两个存储库,您可以看到它是同一台服务器,但一个用于快照,另一个用于发布。我在想应该有一种方法可以在settings.xml 中只有一个条目。谢谢。
      • @Elio 是的。我也想这样做,但看起来不可能。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-05
      • 2016-10-15
      • 2013-11-23
      • 2016-07-25
      • 1970-01-01
      • 2016-12-26
      相关资源
      最近更新 更多