2013-03-18 34 views
7

tomcat7-maven-plugin允许将当前项目作为Web应用程序运行,并且可以指定另外的<webapps>将同时加载到tomcat中。Tomcat - Maven插件:运行webapps但不是当前项目

我的项目不是一个Web应用程序,但它访问由webapps提供的服务。那么如何在不将项目本身作为Web应用程序运行的情况下部署多个Web应用程序?以下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> 
+0

你可以发布maven输出的相关日志吗? – ben75 2013-03-18 16:26:17

+0

因此,多米尼克,你在发布你的问题后一个多小时内得到答复,但你没有礼貌回应? – 2013-07-30 01:16:11

+0

我添加了一个解决方法,它解决了这个问题。但我仍然希望有更好的方法来做到这一点...... – dokaspar 2013-07-31 07:00:12

回答

6

这可能是滥用tomcat的Maven插件,但这里是一个解决方案,我发现。顺便说一句,你的假上下文文件解决方案不适合我,因为我需要运行一个不同的web应用程序,我的应用程序也是一个web应用程序。

有一个杰拉可以为我们的问题提供更好的解决方案。见https://issues.apache.org/jira/browse/MTOMCAT-228。现在我的解决方案...

首先,你需要将战争复制到一个目录。我建议目标目录以便于清理。取决于你是否支持运行或运行战争的目标取决于你是复制战争还是复制战争并解开它。

<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插件以查看刚刚在上述步骤中复制到的目录或战争。以下显示的是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> 

需要明确的是,你不需要配置warDirectory或需要复制的战争执行,如果你只是想支持Tomcat的:运行。相反,如果您只想支持tomcat:run-war,则不需要配置warSourceDirectory或需要执行copy-war-unpack执行。

最后一点需要注意的是,这种依赖性复制解决方案也适用于Jetty Maven插件,所以如果您想同时支持tomcat & jetty,这可能是一种很好的方法。

+0

顺便说一下。我搞砸了所有不同的东西,如外部serverXml文件,上下文文件和各种配置。这是我能够让tomcat插件不部署当前webapp的唯一方法。 – 2013-09-11 01:40:10

+0

我试过同样没有解压缩战争档案,只是使用wsdlFile标签。但是得到相同的错误。 :( – Cherry 2013-11-06 09:44:19

+0

好的解决方案!但是,如果项目类型不是'war',你必须强制tomcat插件跳过一个使用'ignorePackaging = true'的检查。在我的例子中,我正在构建一个需要模拟服务的jar war)做一些集成测试,我用'dependency:copy' mojo并使用'warDirectory = $ {project.build.directory}'/'warFile = mywar.war',一切正常。 – jmelanson 2013-12-19 16:42:51

2

我只是想了解如何以类似maven的方式解决这个问题。我想在这里记录这一点,因为这可能会帮助其他人。我还没有测试过这个解决方案,但我没有看到为什么它不起作用。

基本上,如果你想推出一个“不一样”的web应用比使用码头或Tomcat插件当前一个你可以做的是这样的:

在项目中创建另一个行家模块。这个模块将是你想要启动的webapp的一个空的战争覆盖。然后你可以把所有的jetty或者tomcat配置放到这个新的模块中(或者在pluginManagement下集中在父pom中的jetty和tomcat配置)。因为,tomcat旨在启动“当前”应用程序,因此不需要特别的黑客入侵配置。

+0

它看起来很棒黑客,但不幸的是,Tomcat Maven插件事实上具有相当有限的覆盖支持。请参阅此线程:https://mail-archives.apache.org/mod_mbox/tomcat-users/201204.mbox/%3CCANyrOm6g=a41ufKBfncxd6PvGSUA1M+-nWURpLDKxpBcDOhgJg @ mail.gmail.com%3E – 2014-02-14 11:39:50

+0

哦,是的,我也遇到了重叠问题,请注意JIRA的许多重叠问题,希望他们修复它们的3.0版本,这可能会支持tomcat 8。 /issues.apache.org/jira/browse/MTOMCAT – 2014-02-17 22:47:03

相关问题