【问题标题】:How Do I Replace The Tomcat Port Using Maven-Cargo?如何使用 Maven-Cargo 替换 Tomcat 端口?
【发布时间】:2015-11-22 14:18:18
【问题描述】:

我正在使用 maven cargo 插件下载 tomcat 作为我构建的一部分,并将我的战争放在正确的位置。 然后我使用 maven 程序集将其压缩并在服务器上全部提取。

现在我想使用xmlReplacements 更改tomcat conf/server.xml 中的端口号。

这是我正在做的一个示例,但是如果您运行它,您会看到目标目录中的 server.xml 仍然显示 8080。

我唯一的选择是在项目中保留 server.xml 的修改副本并用它替换整个文件吗?还是我没有正确使用此功能?还是坏了?

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.stackoverflow</groupId>
  <artifactId>question</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <properties>
    <tomcat.version>8.0.24</tomcat.version>
  </properties>

  <build>
    <plugins>
      <!--Create a war-->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <!--This is an empty demo project-->
        <configuration>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
      <!--Create the Tomcat bundle with our war in it-->
      <plugin>
        <groupId>org.codehaus.cargo</groupId>
        <artifactId>cargo-maven2-plugin</artifactId>
        <version>1.4.15</version>
        <configuration>
          <container>
            <!--containerId must be equal to one of the containers supported by Cargo -->
            <!--https://codehaus-cargo.github.io/cargo/Container.html-->
            <containerId>tomcat8x</containerId>
            <artifactInstaller>
              <groupId>org.apache.tomcat</groupId>
              <artifactId>tomcat</artifactId>
              <version>${tomcat.version}</version>
            </artifactInstaller>
          </container>
          <configuration>
            <type>standalone</type>
            <home>${project.build.directory}/cargo/installs/tomcat-${tomcat.version}/apache-tomcat-${tomcat.version}
            </home>
            <!--Allegedly change the port number-->
            <xmlReplacements>
              <xmlReplacement>
                <file>conf/server.xml</file>
                <xpathExpression>/Server/Service/Connector[1]</xpathExpression>
                <attributeName>port</attributeName>
                <value>9090</value>
              </xmlReplacement>
            </xmlReplacements>
          </configuration>
          <deployables>
            <deployable>
              <groupId>${project.groupId}</groupId>
              <artifactId>${project.artifactId}</artifactId>
              <type>war</type>
            </deployable>
          </deployables>
        </configuration>
        <executions>
          <execution>
            <id>cargo-deploy</id>
            <phase>package</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

【问题讨论】:

    标签: maven tomcat maven-cargo


    【解决方案1】:

    如果你想改变 Tomcat 上的端口,你可以在配置属性中使用 cargo.servlet.port 属性,可以设置的所有可能属性列表是here。 如何设置配置属性的示例可以在here找到。

    【讨论】:

      【解决方案2】:

      我的问题有两个方面。我有错误的家和错误的阶段。简短的回答是我改成这样:

                <configuration>
                  <type>standalone</type>
                  <home>${project.build.directory}/apache-tomcat-${tomcat.version}</home>
                  <!--Change the port number-->
                  <xmlReplacements>
                    <xmlReplacement>
                      <file>conf/server.xml</file>
                      <xpathExpression>/Server/Service/Connector[1]</xpathExpression>
                      <attributeName>port</attributeName>
                      <value>9090</value>
                    </xmlReplacement>
                  </xmlReplacements>
                </configuration>
      
      
              </configuration>
              <executions>
                <execution>
                  <id>cargo-deploy</id>
                  <phase>package</phase>
                  <goals>
                    <goal>configure</goal>
                  </goals>
                </execution>
              </executions>
            </plugin>
          </plugins>
        </build>
      </project>
      

      详情:我克隆了源代码查看samples,然后发现差异。

      当我将执行绑定从 deploy 阶段更改为 configure 阶段时,它抱怨如下:

      [ERROR] Failed to execute goal org.codehaus.cargo:cargo-maven2-plugin:1.4.15:configure (cargo-deploy) on project question: Execution cargo-deploy of goal org.codehaus.cargo:cargo-maven2-plugin:1.4.15:configure failed: Failed to create a Tomcat 8.x standalone configuration: Invalid configuration dir [C:\Question\target/cargo/installs/tomcat-8.0.24/apache-tomcat-8.0.24]. When using standalone configurations, the configuration dir must point to an empty directory. Note that everything in that dir will get deleted by Cargo. -> [Help 1]
      [ERROR]
      

      然后我注意到样本有一个基于 tomcat 的home 目录。当我改变它(而不是指向货物安装)时,它终于奏效了!

      不过,我要注意的是,server.xml 中不止端口不同。其他一些属性的格式和排序也不同。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-14
        • 2013-02-01
        • 2011-04-19
        • 1970-01-01
        • 1970-01-01
        • 2016-08-18
        • 1970-01-01
        • 2011-10-22
        相关资源
        最近更新 更多