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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11959975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:05:45  来源:igfitidea点击:

How to use maven plugin tomcat7:run with multiple contexts (WARs)?

javamaventomcatdeploymenttomcat7

提问by mckamey

I've been using mvn tomcat7-maven-plugin:run -am -pl :foosuccessfully to run just a single project at a time in Tomcat like is shown here. Now I'd like to have multiple modules run under the same port but different contexts. For instance, I'd like to have:

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

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

Here's an example pom.xml snippet that I've been working with:

这是我一直在使用的示例 pom.xml 片段:

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

Is this possible with the tomcat7-maven-plugin:runplugin? I'm struggling to find the correct syntax to get it to play well. When I run the mavencommand to run it, it only runs the first one it finds in the project hierarchy. And if I run them with the <fork>true</fork>or obviously from different terminals then I get "java.net.BindException: Address already in use :8080".

这可以通过tomcat7-maven-plugin:run插件实现吗?我正在努力寻找正确的语法来让它发挥良好的作用。当我运行maven命令来运行它时,它只运行它在项目层次结构中找到的第一个。如果我使用<fork>true</fork>或显然来自不同的终端运行它们,那么我会得到“java.net.BindException:地址已经在使用:8080”。

回答by Vasyl Shchukin

As it was already suggested, use <webapps>section of plugin configuration, but add additional parameter <asWebapp>true</asWebapp>for every webapp, i.e:

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

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

These additional webapps are deployed with ${artifactId}context path by default.

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

It looks like without this parameter additional webapps get silently discarded when you run something like mvn tomcat7:run(tried on stable version 2.0). Related Jira issuetells it was done for "backward compatibility".

看起来如果没有这个参数,当您运行类似mvn tomcat7:run(在稳定版本 2.0 上尝试)时,额外的 webapps 会被悄悄丢弃。相关的 Jira 问题表明它是为了“向后兼容性”而完成的。

回答by Olivier Lamy

Use something like

使用类似的东西

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