如何使用 maven 插件 tomcat7:run with multiple contexts (WARs)?

Posted

技术标签:

【中文标题】如何使用 maven 插件 tomcat7:run with multiple contexts (WARs)?【英文标题】:How to use maven plugin tomcat7:run with multiple contexts (WARs)? 【发布时间】:2012-08-11 04:44:40 【问题描述】:

我一直在成功地使用mvn tomcat7-maven-plugin:run -am -pl :foo 在Tomcat like is shown here 中一次只运行一个项目。现在我想让多个模块在同一个端口但不同的上下文下运行。例如,我想要:

/    => foo.war
/bar => bar.war

这是我一直在使用的 pom.xml sn-p 示例:

<project><!-- ... -->
    <build><!-- ... -->
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.0-SNAPSHOT</version>
                    <configuration>
                        <path>/</path>
                        <port>8080</port>
                        <addContextWarDependencies>true</addContextWarDependencies>
                        <addWarDependenciesInClassloader>true</addWarDependenciesInClassloader>
                        <warSourceDirectory>$project.build.directory/$project.build.finalName/</warSourceDirectory>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>$project.groupId</groupId>
                            <artifactId>bar</artifactId>
                            <version>$project.version</version>
                            <type>war</type>
                            <scope>tomcat</scope>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
    <pluginRepositories>
        <pluginRepository>
            <id>apache.snapshots</id>
            <name>Apache Snapshots</name>
            <url>http://repository.apache.org/content/groups/snapshots-group/</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
     </pluginRepositories>
</project>

tomcat7-maven-plugin:run 插件可以做到这一点吗?我正在努力寻找正确的语法以使其正常运行。当我运行maven 命令来运行它时,它只运行它在项目层次结构中找到的第一个。如果我使用&lt;fork&gt;true&lt;/fork&gt; 或显然从不同的终端运行它们,那么我会得到“java.net.BindException:地址已在使用:8080”。

【问题讨论】:

【参考方案1】:

正如已经建议的那样,使用插件配置的&lt;webapps&gt;部分,但为每个webapp添加额外的参数&lt;asWebapp&gt;true&lt;/asWebapp&gt;,即:

<webapps> 
  <webapp> 
    <groupId>com.company</groupId> 
    <artifactId>mywebapp</artifactId> 
    <version>1.0</version> 
    <type>war</type>    
    <asWebapp>true</asWebapp> 
  </webapp> 
  <webapp> 
    <groupId>com.company</groupId> 
    <artifactId>mywebapp2</artifactId> 
    <version>2.0</version> 
    <type>war</type>    
    <asWebapp>true</asWebapp> 
  </webapp>
</webapps> 

默认情况下,这些额外的 web 应用程序使用 $artifactId 上下文路径进行部署。

如果没有这个参数,当你运行类似mvn tomcat7:run(在稳定版本 2.0 上尝试过)时,其他 webapps 会被静默丢弃。 Related Jira issue 告诉它是为了“向后兼容性”而完成的。

【讨论】:

嘿——只是关于这个可能对其他人有帮助的注释...... Vasyl 的这个建议解决了我的问题,但我不得不将 tomcat7-maven-plugin 从 2.0-beta- 升级到 2.0 1 在它起作用之前。在 2.0 之前的版本中有一个错误会阻止 webapps 工作:issues.apache.org/jira/browse/MTOMCAT-169 有没有办法用这种方法覆盖 webapp 的上下文路径? @hoshposh 我在插件的类org.apache.tomcat.maven.common.config.AbstractWebapp 中看到contextPath 参数,所以添加类似&lt;webapp&gt;...&lt;contextPath&gt;/mypath&lt;/contextPath&gt;...&lt;/webapp&gt; 的东西应该可以工作。 只是一个关于我被抓到的东西的注释。您需要此答案中描述的 广告,但您还需要插件依赖项中对项目的依赖项。但是,不要使用 tomcat,因为它无法识别。 如果依赖尝试在同一阶段运行插件和启动tomcat怎么办?【参考方案2】:

使用类似的东西

  <configuration>
    <webapps> 
      <webapp> 
        <groupId>com.company</groupId> 
        <artifactId>mywebapp</artifactId> 
        <version>1.0</version> 
        <type>war</type>    
      </webapp> 
    </webapps> 
  </configuration>

【讨论】:

我假设这是一个配置块,用于定义在当前模块中以 root 身份运行的上下文?即,这就是如何将我的“bar.war”作为“/bar”添加到我的“foo.war”的 pom.xml 中作为“/”?由于依赖关系,它不能直接工作,但我仍在使用它。 我似乎无法让它处理“bar.war”的依赖关系。我试过像这样***.com/a/9928987/43217 明确添加它们,但没有运气。 另外,如果我尝试将“bar.war”作为范围“tomcat”的依赖项添加到模块本身,那么它首先构建,然后 tomcat7:run 命令尝试执行它。

以上是关于如何使用 maven 插件 tomcat7:run with multiple contexts (WARs)?的主要内容,如果未能解决你的问题,请参考以下文章

Maven 插件 maven-tomcat7-plugin - 常用命令及配置

Maven tomcat和jetty的配置插件

用于嵌入式 Tomcat 8.5 的 Tomcat Maven 插件

如何在eclipse中使用maven停止tomcat 7

Maven启动tomcat7:run运行异常:org.apache.catalina.LifecycleException: Failed to start component

Maven+SSM项目弹出context initialization failed错误 调试了一天的Bug