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

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
mckamey
  • 17,359
  • 16
  • 83
  • 116

2 Answers2

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

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Vasyl Shchukin
  • 596
  • 5
  • 4
  • 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: https://issues.apache.org/jira/browse/MTOMCAT-169 – Nathan Beach Feb 08 '13 at 05:15
  • Is there a way to override the context path for the webapp with this approach? – hoshposh Mar 07 '13 at 14:27
  • 5
    @hoshposh I see `contextPath` parameter in plugin's class `org.apache.tomcat.maven.common.config.AbstractWebapp`, so adding something like `.../mypath...` should work. – Vasyl Shchukin Mar 13 '13 at 14:39
  • 1
    Just a note on the thing I got caught out on. You need the 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 tomcat as it isn't recognized. – Victor Jan 15 '14 at 19:20
  • what do i do if the dependency tries to run the plugin and start tomcat in the same phase? – Jayen Nov 24 '14 at 06:43
  • another catch I got caught on: don't use the tomcat:run goal but really the tomcat7:run goal – Jonatan Cloutier Mar 09 '15 at 19:46
1

Use something like

  <configuration>
    <webapps> 
      <webapp> 
        <groupId>com.company</groupId> 
        <artifactId>mywebapp</artifactId> 
        <version>1.0</version> 
        <type>war</type>    
      </webapp> 
    </webapps> 
  </configuration>
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Olivier Lamy
  • 2,280
  • 1
  • 14
  • 12
  • 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. – mckamey Aug 15 '12 at 22:41
  • I can't seem to get it to handle the dependencies of "bar.war". I've tried explicitly adding them like this http://stackoverflow.com/a/9928987/43217 but no luck. – mckamey Aug 15 '12 at 23:04
  • 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. – mckamey Aug 15 '12 at 23:23