【问题标题】:Get maven to do parallel deployments to Tomcat获取 maven 对 Tomcat 进行并行部署
【发布时间】:2012-06-19 16:13:44
【问题描述】:

我找不到让 Maven 使用 Tomcat 7 的并行部署来执行 Tomcat 7 部署的标准化方法:

http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Parallel_deployment

有没有可靠的方法来做到这一点?也许始终保持应用的两个版本有效?

【问题讨论】:

  • 我没有使用 MTOMCAT 的经验,但是您能定义您将要部署的 WAR 文件的名称吗?如果是这样,请确保它被称为 mywebapp##version.war

标签: tomcat maven


【解决方案1】:

您可以将tomcat7-maven-plugin 用于此用例:

        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <path>/WebappName##${maven.build.timestamp}</path>
                <url>http://localhost:8080/manager/text</url>
                <username>tomcat</username>
                <password>tomcat</password>
                <update>true</update>
            </configuration>
        </plugin>

如您所见,版本在 path 元素中指定,基于此示例中的构建时间戳。

当然,如果您不想让所有旧版本继续运行,您仍然需要在 server.xml 中的 &lt;Host&gt; 元素中使用 undeployOldVersions="true"

【讨论】:

    【解决方案2】:

    这就是我在问题How do I get Maven to create an appropriately named .war for use with Tomcat 7's parallel deployment feature? 中的意思。

    您的问题确实是它的要点。下面是我所做的。

    首先,让 Maven 生成正确命名的 .war 文件。我使用了 Maven 的时间戳插件。

    ...
    <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>${war.version}</version>
        <configuration>
            <warName>${war.name}</warName>
        </configuration>
    </plugin>
    ...
    

    使用时间戳作为版本可确保 .war 文件根据 Tomcat 7 用于确定最新版本的规则正确命名。

    我找不到任何有关让 Tomcat 7 Maven 插件使用此插件的信息。在 Tomcat IRC 和 Maven IRC 上询问。也许这是可能的,但我无法让它工作。

    我做的是这样的:

    ...
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
            <execution>
                <id>copy</id>
                <phase>install</phase>
                <goals>
                    <goal>copy</goal>
                </goals>
                <configuration>
                    <artifactItems>
                        <artifactItem>
                            <groupId>${group.id}</groupId>
                            <artifactId>${artifact.id}</artifactId>
                            <version>${version}</version>
                            <type>war</type>
                            <overWrite>true</overWrite>
                            <outputDirectory>${autodeploy.directory}</outputDirectory>
                            <destFileName>${war.name}.war</destFileName>
                        </artifactItem>
                    </artifactItems>
                </configuration>
            </execution>
        </executions>
    </plugin>
    ...
    

    这只是将文件从一个地方复制到另一个地方。在这种情况下,您构建到 Tomcat 的 autodeploy 目录的 .war。

    如果您使用 Jenkins 进行部署,您可以将副本设置为构建后操作。 (使用 target/appname*.war 作为 EAR/WAR 文件名。)

    为了完整起见,这里引用了各种属性:

    <properties>
        <artifact.id>myApp</artifact.id>
        <version>1.0</version>      
        <group.id>com.example.${artifact.id}</group.id>
        <autodeploy.directory xml:space="preserve">C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps</autodeploy.directory>
        <war.name>${artifact.id}##${maven.build.timestamp}</war.name>    
    ...
    

    (请注意xml:space="preserve",以便在目录中有空格时让 Windows 开心。)

    Tomcat 7 默认会自动部署,但不会删除旧版本。也许这就是您想要的,因为您将拥有所有旧版本,以防万一您搞砸了并且需要取消部署最新版本。

    但是如果你想用钢笔写,可以这么说,从你的 Tomcat 7 安装目录中找到 conf/server.xml。让&lt;Host&gt; 看起来像这样:

    <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true" undeployOldVersions="true">
    

    undeployOldVersions="true" 是魔法。

    真的是这样。现在,当你执行maven clean install 时,它会构建一个命名良好的战争并将其放入 Tomcat 的自动部署目录中,它应该可以正常工作。

    -科林

    【讨论】:

    • 感谢您分享这颗宝石!另外,要调整时间戳格式,可以按照此处提供的说明进行操作:maven.apache.org/guides/introduction/…
    • 添加到@bertie 的评论中,Windows 不喜欢 maven.build.timestamp 的默认格式中的冒号(“组装 WAR 时出错:创建战争时出现问题:”)。使用他在此处提供的页面中的说明是 Windows 证明格式的示例:yyyy-MM-dd'T'HH-mm-ss
    【解决方案3】:

    这里也一样,所以我编写了一个脚本来使用 Maven 完成此操作,请参阅 http://tynamo.org/Zero+downtime+deployment+to+Tomcat+7+with+Maven。此外,Tomcat 7.0.31 将具有 undeployOldVersions(请参阅https://issues.apache.org/bugzilla/show_bug.cgi?id=52777)。

    【讨论】:

      【解决方案4】:

      直接通过 Maven 执行此操作可能是一个挑战。您可以尝试让 Maven 在打包时将其版本号应用于 WAR 文件,然后部署它。

      就旧版本而言,“undeployOldVersions”属性将让 Tomcat 在不再使用旧版本时将其删除。

      【讨论】:

        【解决方案5】:

        您可以尝试使用更好的 Tomcat 管理器,例如 MeerCat:http://www.meercatmanager.com

        它不使用 Maven,但可以在多个 Tomcat 服务器实例上部署相同的应用程序并轻松管理它们的部署。看看吧!

        【讨论】:

          【解决方案6】:

          解决方案是使用 Cargo 插件。

          请在以下 3 个配置文件中找到可让您:

          • 本地部署到 Cargo 自动安装的 Tomcat
          • 本地部署到已安装并启动的 Tomcat
          • 远程部署。

          注意'context' 属性,它允许在部署期间使用## 为并行部署重命名工件。

          <profiles>
              <profile>
                  <!-- Local deploy - tomcat 7 automatically installed by Cargo -->
                  <id>local_deploy_auto_install</id>
                  <build>
                      <plugins>
                          <plugin>
                              <groupId>org.codehaus.cargo</groupId>
                              <artifactId>cargo-maven2-plugin</artifactId>
                              <configuration>
                                  <container>
                                      <containerId>tomcat7x</containerId>
                                      <zipUrlInstaller>
                                          <url>http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.35/bin/apache-tomcat-7.0.35.zip</url>
                                      </zipUrlInstaller>
                                  </container>
                                  <deployables>
                                      <deployable>
                                          <properties>
                                              <context>${project.artifactId}##${project.version}</context>
                                          </properties>
                                      </deployable>
                                  </deployables>
                                  <configuration>
                                      <properties>
                                          <cargo.servlet.port>9080</cargo.servlet.port>
                                      </properties>
                                  </configuration>
                              </configuration>
                          </plugin>
                      </plugins>
                  </build>
              </profile>
          
              <profile>
                  <!-- Local deploy - tomcat 7 must have been installed and started -->
                  <id>local_deploy</id>
                  <build>
                      <plugins>
                          <plugin>
                              <groupId>org.codehaus.cargo</groupId>
                              <artifactId>cargo-maven2-plugin</artifactId>
                              <configuration>
                                  <!-- When Cargo starts the container, the following tag instructs it to wait for you to kill the session with Crtl-C -->
                                  <!-- <wait>true</wait> -->
                                  <!-- The following tag details the container you want to deploy to. -->
                                  <container>
                                      <!-- Specifying "tomcat7x" is very important! This one tripped me up for quite a while. The issue is that instead 
                                          of being an identifier for you, "tomcat7x" is an identifier for Cargo that you want to deploy your webapp in Tomcat 7.x. 
                                          I had initially thought otherwise and hence just dropped the 'x', making it "tomcat7", but that never worked. -->
                                      <containerId>tomcat7x</containerId>
                                      <!-- Type == Installed means that you want to deploy to a container that's installed on your computer -->
                                      <type>installed</type>
                                  </container>
                                  <configuration>
                                      <!-- This is another one that confused me for long. Its not enough to specify 'installed' in the container tag. You 
                                          have to now specify another configuration with type == existing and the home path -->
                                      <type>existing</type>
                                      <home>${basedir}/../../tomcat7.0.37</home>
                                  </configuration>
                                  <!-- Here you specify 'deployables' -->
                                  <deployables>
                                      <!-- This deployable specifies the webapp you want to deploy -->
                                      <deployable>
                                          <properties>
                                              <context>${project.artifactId}##${project.version}</context>
                                          </properties>
                                      </deployable>
                                  </deployables>
                              </configuration>
                              <!-- Executions specify the targets that you want to run during build -->
                              <executions>
                                  <!-- Maven has the concept of a 'phase' which can be thought of a collection of goals. Hence here we are specifying 
                                      that during the 'install' phase first deploy the webapp to the container specific folder and then start the container. Both 
                                      'deployer-deploy' and 'start' are cargo specific goals. -->
                                  <execution>
                                      <id>verify-deploy</id>
                                      <phase>install</phase>
                                      <goals>
                                          <goal>deploy</goal>
                                      </goals>
                                  </execution>
                              </executions>
                          </plugin>
                      </plugins>
                  </build>
              </profile>
          
              <profile>
                  <!-- Remote dans un tomcat7 pré-installé, pré-démarré -->
                  <id>remote_deploy</id>
                  <build>
                      <plugins>
                          <plugin>
                              <groupId>org.codehaus.cargo</groupId>
                              <artifactId>cargo-maven2-plugin</artifactId>
                              <configuration>
                                  <!-- When Cargo starts the container, the following tag instructs it to wait for you to kill the session with Crtl-C -->
                                  <!-- <wait>true</wait> -->
                                  <!-- The following tag details the container you want to deploy to. -->
                                  <container>
                                      <!-- Specifying "tomcat7x" is very important! This one tripped me up for quite a while. The issue is that instead 
                                          of being an identifier for you, "tomcat7x" is an identifier for Cargo that you want to deploy your webapp in Tomcat 7.x. 
                                          I had initially thought otherwise and hence just dropped the 'x', making it "tomcat7", but that never worked. -->
                                      <containerId>tomcat7x</containerId>
                                      <!-- Type == Installed means that you want to deploy to a container that's installed on your computer -->
                                      <type>remote</type>
                                  </container>
                                  <configuration>
                                      <!-- This is another one that confused me for long. Its not enough to specify 'installed' in the container tag. You 
                                          have to now specify another configuration with type == existing and re-issue the home path -->
                                      <type>runtime</type>
                                      <properties>
                                          <cargo.protocol>http</cargo.protocol>
                                          <cargo.hostname>192.168.0.6</cargo.hostname>
                                          <cargo.servlet.port>8080</cargo.servlet.port>
                                          <cargo.remote.username>deploy</cargo.remote.username>
                                          <cargo.remote.password>purplerain</cargo.remote.password>
                                      </properties>
                                  </configuration>
                                  <!-- Here you specify 'deployables' -->
                                  <deployables>
                                      <!-- This deployable specifies the webapp you want to deploy -->
                                      <deployable>
                                          <properties>
                                              <context>${project.artifactId}##${project.version}</context>
                                          </properties>
                                      </deployable>
                                  </deployables>
                              </configuration>
                              <!-- Executions specify the targets that you want to run during build -->
                              <executions>
                                  <!-- Maven has the concept of a 'phase' which can be thought of a collection of goals. Hence here we are specifying 
                                      that during the 'install' phase first deploy the webapp to the container specific folder and then start the container. Both 
                                      'deployer-deploy' and 'start' are cargo specific goals. -->
                                  <execution>
                                      <id>verify-deploy</id>
                                      <phase>install</phase>
                                      <goals>
                                          <goal>deploy</goal>
                                      </goals>
                                  </execution>
                              </executions>
                          </plugin>
                      </plugins>
                  </build>
              </profile>
          </profiles>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-11-28
            • 1970-01-01
            • 2012-11-12
            • 2010-11-27
            • 2011-02-14
            • 2017-04-17
            • 2015-02-05
            • 2011-12-06
            相关资源
            最近更新 更多