Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I deploy multiple wars using the tomcat plugin in maven?

Tags:

maven-2

tomcat

I have two wars which I deploy in two maven projects using tomcat plugin. I want to do this in one step and be able to deploy more than one war in a single maven project. How can I do this? any suggestions?

like image 661
nagl Avatar asked Jul 27 '09 17:07

nagl


People also ask

What is Tomcat Maven plugin?

The Tomcat Maven Plugin provides goals to manipulate WAR projects within the Apache Tomcat servlet container. Or to run your war project with an embedded Apache Tomcat. The run goals give you the opportunity to quickly develop your application without needing to install a standalone Tomcat instance.


1 Answers

I'm not in a position to test this, but there are two approaches I can think of. Either may work for you.

Option 1:

In one of the projects you can define the configuration for the tomcat plugin. In the snippet below there are two executions defined, both bound to the pre-integration-test phase (this might not be the best phase to do this, but it seems a good starting point as the war will have been packaged). Each execution will deploy the war defined in its configuration's warFile property.

<project>
  ...
  <build>
    ...
    <plugins>
      ...
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>tomcat-maven-plugin</artifactId>
        <version>1.0-beta-1</version>
        <executions>
          <execution>
            <id>deploy1</id>
            <phase>pre-integration-test</phase>
            <configuration>
              <warFile>path/to/my/warFile1.war</warFile>
            </configuration>
            <goals>
              <goal>deploy</goal>
            </goals>
          </execution>
          <execution>
            <id>deploy2</id>
            <phase>pre-integration-test</phase>
            <configuration>
              <warFile>path/to/my/warFile2.war</warFile>
            </configuration>
            <goals>
              <goal>deploy</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      ...
    </plugins>
    ...
  </build>
  ...
</project>

Option 2: This is probably the better approach. Define one execution in each war (you can omit the warFile element as the default can be used). You can then define a third project with a modules declaration referencing each war project. When the parent is built both wars will be be built and the wars deployed.

The modules declaration for the third project:

<modules>
  <module>relative/path/to/war1</module>
  <module>relative/path/to/war2</module>
<modules>

And the config for each war project:

<project>
  ...
  <build>
    ...
    <plugins>
      ...
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>tomcat-maven-plugin</artifactId>
        <version>1.0-beta-1</version>
        <executions>
          <execution>
            <id>deploy</id>
            <phase>pre-integration-test</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      ...
    </plugins>
    ...
  </build>
  ...
</project>

Maven has some properties you can use to avoid absolute paths.

${maven.repo.local} will resolve to the local repository
${project.basedir} is the project directory (the root of the project)
${project.build.directory} is the build directory (default is "target")
${project.build.outputDirectory} is the directory where compilation output goes (default is "target/classes")
like image 117
Rich Seller Avatar answered Oct 18 '22 12:10

Rich Seller