我找不到让 Maven 使用 Tomcat 7 的并行部署进行 Tomcat 7 部署的标准化方法:
http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Parallel_deployment
有没有可靠的方法来做到这一点?也许总是保持应用程序的两个版本?
我找不到让 Maven 使用 Tomcat 7 的并行部署进行 Tomcat 7 部署的标准化方法:
http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Parallel_deployment
有没有可靠的方法来做到这一点?也许总是保持应用程序的两个版本?
您可以为此用例使用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
元素中指定的,基于此示例中的构建时间戳。
当然,如果您不想让所有旧版本保持运行,您仍然需要undeployOldVersions="true"
在 server.xml元素中使用。<Host>
这就是我在我的问题如何让 Maven 创建一个适当命名的 .war 以与 Tomcat 7 的并行部署功能一起使用的问题?.
你的问题确实是它的要点。下面是我所做的。
首先,让 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 的自动部署目录的 .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。<Host>
看起来像这样:
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true" undeployOldVersions="true">
undeployOldVersions="true"
是魔法。
就是这样。现在,当你这样做时maven clean install
,它会构建一个命名良好的 war 并将其放入 Tomcat 的 autodeploy 目录中,它应该可以正常工作。
-科林
这里也一样,所以我编写了一个脚本来使用 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)。
直接通过 Maven 执行此操作可能是一个挑战。您可以尝试让 Maven 在打包时将其版本号应用于 WAR 文件,然后部署它。
至于旧版本,“undeployOldVersions”属性将让 Tomcat 在不再使用旧版本时将其删除。
您可以尝试使用更好的 Tomcat 管理器,例如 MeerCat:http ://www.meercatmanager.com
它不使用 Maven,但可以在多个 Tomcat 服务器实例上部署相同的应用程序并轻松管理它们的部署。看看这个 !
解决方案是使用 Cargo 插件。
请在下面找到 3 个配置文件,这些配置文件可让您:
请注意“上下文”属性,该属性允许在部署期间使用## 重命名工件以进行并行部署。
<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>