2015-08-28 67 views
0

我正在使用maven貨物插件下載tomcat作爲我的構建的一部分,並把我的戰爭放在正確的地方。 然後,我使用maven程序集將其壓縮並將其全部提取到服務器上。如何使用Maven-Cargo替換Tomcat端口?

現在我想用xmlReplacements更改tomcat conf/server.xml中的端口號。

這是我在做什麼的例子,但如果你運行它,你會看到,在目標目錄在server.xml還稱8080

是我保持服務器的修改後的副本的唯一選擇.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> 

回答

0

我的問題是雙重的。我有錯誤的家庭和錯誤的階段。簡短的回答是我改變這樣的:

  <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的基dir來home。當我改變它(而不是指向貨物安裝)時,它終於奏效了!

但是,我應該注意,server.xml中的端口不同。一些其他屬性的格式和順序也不同。

1

如果要更改Tomcat上的端口,可以在配置屬性中使用cargo.servlet.port屬性,可以設置的所有可能屬性的列表爲here。 如何設置配置屬性的示例可以找到here