Tomcat - Maven插件:运行webapps但不是当前项目

dok*_*par 7 web-applications maven tomcat7 maven-tomcat-plugin

tomcat7-maven-plugin允许运行当前项目作为一个Web应用程序和额外的<webapps>可指定将被同时加载到Tomcat.

我的项目不是Web应用程序,但它访问webapps提供的服务.那么如何在不将项目本身作为webapp运行的情况下部署大量的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>
Run Code Online (Sandbox Code Playgroud)

解决方法:

即使您的应用程序本身不是webapp,您也需要为它配置a path和a contextFile:

<configuration>
    <path>/my/non/existing/webapp</path>
    <contextFile>src/test/resources/context.xml</contextFile>
    <webapps>
    ...
Run Code Online (Sandbox Code Playgroud)

指定的context.xml文件必须存在.以下对我有用,即使web.xml文件不存在:

<?xml version="1.0" encoding="utf-8"?>
<Context path="/my/non/existing/webapp">
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
Run Code Online (Sandbox Code Playgroud)

Tra*_*ger 6

这可能会滥用tomcat maven插件,但这是我找到的解决方案.BTW您的假上下文文件解决方案对我不起作用,因为我需要运行不同的webapp,我的应用程序也是一个webapp.

那里有一个jira可以为我们的问题提供更好的解决方案.请参阅https://issues.apache.org/jira/browse/MTOMCAT-228.现在我的解决方案......

首先,您需要将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>
Run Code Online (Sandbox Code Playgroud)

接下来,您需要配置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>
Run Code Online (Sandbox Code Playgroud)

要清楚,如果您只想支持tomcat:run,则不需要配置warDirectory或需要copy-war执行.相反,如果您只想支持tomcat:run-war,则无需配置warSourceDirectory或需要copy-war-unpack执行.

最后要注意的是,这种依赖项复制解决方法也适用于Jetty Maven插件,所以如果你想同时支持tomcat和jetty,这可能是一个很好的方法.