Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 551
mckamey Avatar asked Aug 14 '12 20:08

mckamey


2 Answers

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

like image 142
Vasyl Shchukin Avatar answered Oct 19 '22 06:10

Vasyl Shchukin


Use something like

  <configuration>
    <webapps> 
      <webapp> 
        <groupId>com.company</groupId> 
        <artifactId>mywebapp</artifactId> 
        <version>1.0</version> 
        <type>war</type>    
      </webapp> 
    </webapps> 
  </configuration>
like image 29
Olivier Lamy Avatar answered Oct 19 '22 05:10

Olivier Lamy