How to copy .war to Tomcat's webapps directory using Maven?

Viewed 59573

Is there anything I can add to pom.xml that will copy the generated .war file from the target directory to my Tomcat's webapps directory?

9 Answers
<build>
   <plugins>
     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.1</version>
        <configuration>
           <outputDirectory><!-- Tomcat webapps location--></outputDirectory>
           <!-- Example of Tomcat webapps location :D:\tomcat\webapps\ -->
        </configuration>
      </plugin>
    </plugins>
</build>

Once you have added it to your pom.xml, you can copy the WAR file by calling mvn package or mvn war:war.

You could also have a look at the jetty plugin. Just type "mvn jetty:run-war" and jetty should run your war-file.

Edit: Jetty is a light weight servlet container suitable for development and testing. It's also lightning fast to start.

Alternatively, you could have tomcat look in your target directory and deploy directly from there.

In your context.xml or server.xml's Context element:

<Context path="" docBase="/path/to/target/exploded">
...
</Context>

Then you can use the war:exploded goal to create your exploded war.

Not ideal, but if you have a really strange app server setup, you could always use an antrun task set to execute when the packaging is run

<build>
    ....
    <plugins>
       <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <executions>
            <execution>
              <phase>package</phase>
              <configuration>
                <tasks>
                  <!-- Ant copy tasks go here -->
                </tasks>
              </configuration>
              <goals>
                <goal>run</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
     </plugins>
  </build>
Related