0
votes

I try to build a web application based on multiple maven modules. One of the modules is called "web" and is solely responsible to package a war which should be deployed to a tomcat using the tomcat7-maven-plugin. I have following modules defined in my parent.pom:

  • common
  • persistence
  • persistence-embedded
  • service
  • rest
  • web

All of them are combined into one web-application-war, the web module has set packaging to war. The problem is, that my war file is deployed for each submodule (and the main-parent-module) over and over again when I run mvn tomcat7:redeploy, which leads to 7 deployments. Apparently, this is not how it should be. The tomcat7-maven-plugin configuration currently looks like this:

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <ignorePackaging>true</ignorePackaging>
        <url>http://localhost:8080/manager/text</url>
        <server>tomcatServer</server>
        <path>/webapp</path>
        <warFile> /home/username/dev/maven-multimodule-example/web/target/maven-multimodule-example-1.0-SNAPSHOT.war</warFile>
        <username>admin</username>
        <password>password</password>
    </configuration>
</plugin>

As you can see, I need to specify the warFile (which is not a solution but rather a hack, because I can't use ${project.basedir} which would lead to the submodule-dir) to make it work.

However, if I run the web application with mvn tomcat7:run, it looks quite good, because the other non-war-building modules are skipped by the plugin.

How can I configure the plugin the right way to deploy the war file only once?

2
Where exactly have you configured the tomcat plugin? Which POM, which section in the POM?dunni
in the main-parent-pomDaniel

2 Answers

1
votes

Every configuration within the <build> section of a parent POM will be inherited and thus executed in all child modules. So if you want to deploy only once, add it to only one POM (e.g. the web POM).

1
votes

Thanks to dunni's help I noticed my missunderstanding of how multimodule projects are built. Now I've placed the plugin configuration in the web module and added an execution, bound to the install phase so that I can rebuild the whole project and get it deployed to my tomcat. Obviously maven takes care of the right execution order of the modules.

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <url>http://localhost:8080/manager/text</url>
        <server>tomcatServer</server>
        <path>/webapp</path>
        <warFile>${project.basedir}/target/${project.parent.artifactId}-${project.parent.version}.war</warFile>
        <username>admin</username>
        <password>password</password>
    </configuration>
    <executions>
        <execution>
            <id>redeployafterinstall</id>
            <phase>install</phase>
            <goals>
                <goal>redeploy</goal>
            </goals>
        </execution>
    </executions>
</plugin>