Maven -<服务器>>在设置.xml中

发布于 2024-11-18 02:27:13 字数 1505 浏览 1 评论 0原文

我使用 tomcat-maven-plugin 将我的 war 部署到服务器。我要做的就是在我的 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 位置,这样我就不必使用属性?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北风几吹夏 2024-11-25 02:27:13

首先我要说的是,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 部署到它包 tomcat:deploy

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

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

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

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.

习惯成性 2024-11-25 02:27:13

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文