tomcat7-maven-plugin
允许将当前项目作为 Web 应用程序和附加 <webapps>
运行可以指定将同时加载到 tomcat 中。
我的项目不是 Web 应用程序,但它访问由 webapps 提供的服务。那么如何在不将项目本身作为 webapp 运行的情况下部署多个 webapp 呢?以下 Maven 代码段导致 FileNotFoundExceptions,因为找不到 context.xml。
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<id>run-tomcat</id>
<phase>${tomcat.run.phase}</phase>
<goals><goal>run-war-only</goal></goals>
<configuration>
<webapps>
<webapp>
<contextPath>/my/app1</contextPath>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<type>war</type>
<asWebapp>true</asWebapp>
</webapp>
... possibly more webapps ...
</webapps>
</configuration>
</execution>
<execution>
<id>tomcat-shutdown</id>
<phase>${tomcat.shutdown.phase}</phase>
<goals><goal>shutdown</goal></goals>
</execution>
</executions>
</plugin>
解决方法:
即使您的应用程序本身不是 web 应用程序,您也需要配置一个
path
和一个 contextFile
为了它:<configuration>
<path>/my/non/existing/webapp</path>
<contextFile>src/test/resources/context.xml</contextFile>
<webapps>
...
指定的
context.xml
文件必须存在。以下对我有用,即使 web.xml
文件不存在:<?xml version="1.0" encoding="utf-8"?>
<Context path="/my/non/existing/webapp">
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
最佳答案
这可能是在滥用 tomcat maven 插件,但这是我找到的解决方案。顺便说一句,您的虚假上下文文件解决方案对我不起作用,因为我需要运行不同的 web 应用程序,而我的应用程序也是一个 web 应用程序。
有一个 jira 可以为我们的问题提供更好的解决方案。见 https://issues.apache.org/jira/browse/MTOMCAT-228 .现在开始我的解决方案......
首先,您需要将 war 复制到一个目录中。我建议目标目录,以便它可以轻松清理。取决于您是要支持 run 还是 run-war 目标,取决于您是只是复制 war 还是复制 war 并解压缩它。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-dependency-plugin.version}</version>
<executions>
<execution>
<id>copy-war</id>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.foo</groupId>
<artifactId>bar</artifactId>
<version>${bar.version}</version>
<type>war</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/bar/</outputDirectory>
</artifactItem>
</artifactItems>
<stripVersion>true</stripVersion>
</configuration>
</execution>
<execution>
<id>copy-war-unpack</id>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.foo</groupId>
<artifactId>bar</artifactId>
<version>${bar.version}</version>
<type>war</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/bar/bar</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
接下来,您需要配置tomcat插件以查看您在上述步骤中刚刚复制到的目录或war。下面显示了 tomcat 6 和 7 的配置,除了 Artifact id 之外,它是相同的。
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>${tomcat6-maven-plugin.version}</version>
<configuration>
<port>8090</port>
<path>${default.rice.context.path}</path>
<warDirectory>${project.build.directory}/bar/bar.war</warDirectory>
<warSourceDirectory>${project.build.directory}/bar/bar</warSourceDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>${tomcat7-maven-plugin.version}</version>
<configuration>
<port>8090</port>
<path>${default.rice.context.path}</path>
<warDirectory>${project.build.directory}/bar/bar.war</warDirectory>
<warSourceDirectory>${project.build.directory}/bar/bar</warSourceDirectory>
</configuration>
</plugin>
需要明确的是,如果您只想支持 tomcat:run,则不需要配置 warDirectory 或需要 copy-war 执行。反之,如果只想支持 tomcat:run-war,则不需要配置 warSourceDirectory 或需要 copy-war-unpack 执行。
最后要注意的是,此依赖项复制解决方法也适用于 Jetty Maven 插件,因此如果您想同时支持 tomcat 和 jetty,这可能是一个很好的方法。
关于maven - Tomcat - Maven 插件 : run webapps but not current project,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15480218/