Maven - settings.xml中的<server />

Bal*_*eth 27 maven maven-tomcat-plugin

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

<configuration>
...
   <url>http://localhost/manager</url>
   <username>admin</username>
   <password>admin</password>
...
</configuration>
Run Code Online (Sandbox Code Playgroud)

但是后来我显然希望将这些设置保存在不同的地方,因为我在我的计算机上工作但是然后有一个升级和一个实时服务器以及服务器的设置不同.

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

<servers>
    <server>
        <id>local_tomcat</id>
        <username>admin</username>
        <password>admin</password>
    </server>
</servers>
Run Code Online (Sandbox Code Playgroud)

现在更改pom.xml:

<configuration>
    <server>local_tomcat</server>
</configuration>
Run Code Online (Sandbox Code Playgroud)

但是在哪里放置服务器的URL?在服务器标签下的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>
Run Code Online (Sandbox Code Playgroud)

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

但问题是,为什么要使用服务器标签settings.xml呢?为什么不使用属性作为用户名和密码呢?或者URL是否也存在于设置URL中,因此我不必使用属性?

小智 31

首先让我说,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>
Run Code Online (Sandbox Code Playgroud)

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

   <servers>
       <server>
           <id>localhost</id>
           <username>admin</username>
           <password>password</password>
       </server>
    </servers>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

这将tomcat-localhost默认情况下启用您的配置文件,并允许您使用简单部署到它mvn clean package tomcat:deploy.

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

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

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


v.l*_*nev 5

设置.xml

<settings>
  <servers>
    <server>
        <id>company.jfrog.io</id>
        <username>user-name</username>
        <password>user-password</password>
    </server>   
  </servers>
</settings>
Run Code Online (Sandbox Code Playgroud)

pom.xml

<repositories>
    <repository>
        <id>company.jfrog.io</id>
        <url>https://company.jfrog.io/company/release</url>
    </repository>
</repositories>
Run Code Online (Sandbox Code Playgroud)

settings.xml

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

~/.m2/settings.xml(对于 Linux)。

company.jfrog.iosettings.xml可以是任何标识符,但在和中应该相同pom.xml

这适用于 Maven 3。