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

2

20

I've been using mvn tomcat7-maven-plugin:run -am -pl :foo successfully 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:

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

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

<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:run plugin? I'm struggling to find the correct syntax to get it to play well. When I run the maven command 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".

Rowan answered 14/8, 2012 at 20:9 Comment(0)
O
20

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

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

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 issue tells it was done for "backward compatibility".

Onehorse answered 2/11, 2012 at 11:2 Comment(6)
Hey -- just a note on this one that might help someone else... This advice by Vasyl solved my problem, but I had to upgrade tomcat7-maven-plugin to 2.0 from 2.0-beta-1 before it would work. There was a bug in that pre-2.0 release that was preventing webapps from working: issues.apache.org/jira/browse/MTOMCAT-169Howarth
Is there a way to override the context path for the webapp with this approach?Hardly
@Hardly I see contextPath parameter in plugin's class org.apache.tomcat.maven.common.config.AbstractWebapp, so adding something like <webapp>...<contextPath>/mypath</contextPath>...</webapp> should work.Onehorse
Just a note on the thing I got caught out on. You need the <webapps> ad described in this answer but you also need the dependency on the project in the dependencies for the plugin as well. However, don't use the <scope>tomcat</scope> as it isn't recognized.Knighterrantry
what do i do if the dependency tries to run the plugin and start tomcat in the same phase?Eats
another catch I got caught on: don't use the tomcat:run goal but really the tomcat7:run goalHakeem
B
1

Use something like

  <configuration>
    <webapps> 
      <webapp> 
        <groupId>com.company</groupId> 
        <artifactId>mywebapp</artifactId> 
        <version>1.0</version> 
        <type>war</type>    
      </webapp> 
    </webapps> 
  </configuration>
Baking answered 15/8, 2012 at 21:20 Comment(3)
I'm assuming that is a chunk of config to define the context that is run within the current module as root? i.e., that would be how to add my "bar.war" as "/bar" into the pom.xml of my "foo.war" as "/"? It didn't work directly because of dependencies but I'm still playing with it.Rowan
I can't seem to get it to handle the dependencies of "bar.war". I've tried explicitly adding them like this https://mcmap.net/q/664020/-tomcat7-maven-plugin-extradependency-seems-not-being-loaded but no luck.Rowan
Also, if I try to add "bar.war" as a dependency of scope "tomcat" to the module itself, then it builds first and the tomcat7:run command tries to execute it instead.Rowan

© 2022 - 2024 — McMap. All rights reserved.