【问题标题】:Tomcat - Maven plugin: run webapps but not current projectTomcat - Maven 插件:运行 webapps 但不是当前项目
【发布时间】:2013-03-18 15:13:18
【问题描述】:

tomcat7-maven-plugin 允许将当前项目作为 Web 应用程序运行,并且可以指定额外的 <webapps>,它们将同时加载到 tomcat 中。

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

解决方法:

即使您的应用程序本身不是 webapp,您也需要为其配置 pathcontextFile

<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>

【问题讨论】:

  • 能贴出maven输出的相关日志吗?
  • 所以,Dominik,您在发布问题一个多小时后收到了回复,但您没有礼貌回复?
  • 我添加了一个解决方法,可以解决问题。但我仍然希望有更好的方法来做到这一点......
  • Dominik,你在上下文文件中放了什么?当我使用上面的配置时,Tomcat7 会抛出错误“启动静态资源时出错”。你明白了吗?
  • 我也需要能够做到这一点...寻找解决方案。您是否为此用例向 apache tomcat 团队提交了功能请求?

标签: maven web-applications tomcat7 maven-tomcat-plugin


【解决方案1】:

这可能是在滥用 tomcat maven 插件,但这是我找到的解决方案。顺便说一句,您的假上下文文件解决方案对我不起作用,因为我需要运行不同的 web 应用程序,而我的应用程序也是一个 web 应用程序。

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

首先,您需要将战争复制到一个目录。我建议使用目标目录,以便轻松清理它。取决于您是要支持 run 还是 run-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 的配置,除了工件 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 或执行复制战争。反之,如果只想支持tomcat:run-war,则不需要配置warSourceDirectory,也不需要执行copy-war-unpack。

最后一点,这个依赖副本解决方法也适用于 Jetty Maven 插件,所以如果你想同时支持 tomcat 和 jetty,这可能是一个好方法。

【讨论】:

  • 顺便说一句。我搞砸了所有不同的东西,比如外部 serverXml 文件、上下文文件和各种配置。这是我能够让 tomcat 插件不部署当前 webapp 的唯一方法。
  • 我在没有解压战争档案的情况下尝试了同样的方法,只是使用了 wsdlFile 标记。但得到同样的错误。 :(
  • 很好的解决方案!但是,如果项目类型不是war,则必须强制tomcat 插件跳过ignorePackaging=true 的检查。就我而言,我正在构建一个需要模拟服务(战争)来进行一些集成测试的 jar。我使用了dependency:copy mojo 和warDirectory=${project.build.directory}/warFile=mywar.war,一切正常。
【解决方案2】:

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

基本上,如果您想使用 jetty 或 tomcat 插件启动与当前“不同”的 web 应用程序,您可以这样做:

在您的项目中创建另一个 Maven 模块。此模块将是您要启动的 web 应用程序的空战争覆盖。然后你可以把你所有的 jetty 或 tomcat 配置放在那个新模块中(或者只是将 jetty 和 tomcat 配置集中在 pluginManagement 下的父 pom 中)。由于 jetty 和 tomcat 旨在启动“当前”应用程序,因此不需要特殊的破解配置。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-30
  • 2014-01-31
  • 1970-01-01
  • 2014-08-10
相关资源
最近更新 更多