让maven对Tomcat进行并行部署

时间:2012-06-19 16:13:45

标签: tomcat maven

我无法找到一种标准化的方法来让Maven进行Tomcat 7部署,并使用Tomcat 7的并行部署:

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

有没有可行的方法呢?也许总是保留两个版本的应用程序?

6 个答案:

答案 0 :(得分:4)

您可以将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 undeployOldVersions="true"元素中使用<Host>

答案 1 :(得分:3)

这就是我在问题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>
...

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

我找不到任何有关获取Tomcat 7 Maven插件的信息。问Tomcat IRC&amp;在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进行部署,则可以将副本设置为构建后操作。 (对EAR / WAR文件名使用target / appname * .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。让<Host>看起来像这样:

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

undeployOldVersions="true"是神奇的。

真的是这样的。现在,当你执行maven clean install时,它将构建一个命名很好的war并将其放入Tomcat的autodeploy目录中,它应该可以工作。

-Colin

答案 2 :(得分:1)

同样在这里,所以我写了一个脚本来与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)。

答案 3 :(得分:0)

直接通过Maven这样做可能是一个挑战。您可以尝试让Maven在发生打包时将其版本号应用于WAR文件,然后进行部署。

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

答案 4 :(得分:0)

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

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

答案 5 :(得分:0)

解决方案是使用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>