Search code examples
maventomcatmaven-cargo

How Do I Replace The Tomcat Port Using Maven-Cargo?


I am using the maven cargo plugin to download tomcat as part of my build and put my war in the correct place. I then use maven assembly to zip it up and extract it all on the server.

Now I want to change the port number in the tomcat conf/server.xml using xmlReplacements.

This is an example of what I am doing but if you run it you will see that the server.xml in the target dir still says 8080.

Is my only option to keep a modified copy of the server.xml in the project and replace the whole file with that? Or am I not using this feature correctly? Or is it broken?

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

Solution

  • My problem was two-fold. I had the wrong home and the wrong phase. The short answer is I changed to this:

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

    Detail: I cloned the source to look at the samples then played spot the difference.

    When I changed the execution binding from the deploy phase to the configure phase it was complaining like this:

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

    Then I noticed that the samples had a tomcat-base dir for home. When I changed that (instead of pointing to the cargo install) it finally worked!

    However, I should note that more than the port is different in the server.xml. Formatting and ordering of some other attributes are different too.