50

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?

Anish B.
  • 9,111
  • 3
  • 21
  • 41
l15a
  • 2,547
  • 5
  • 29
  • 41

9 Answers9

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

Anish B.
  • 9,111
  • 3
  • 21
  • 41
koszu28
  • 741
  • 1
  • 5
  • 2
21

I used the Maven WAR Plugin: http://maven.apache.org/plugins/maven-war-plugin/usage.html

l15a
  • 2,547
  • 5
  • 29
  • 41
  • 6
    The code to add is : in the configuration tag of the maven-war-plugin artifactId – techzen Mar 02 '11 at 18:35
  • How could one do this in the case where the webapps folder is on a remote server (e.g. could it copy over SCP?). I'm trying to do this with the maven-wagon-ssh library, and it almost works, but I can't figure out how to control which files are sent and where they go. – jacobq Jan 02 '13 at 20:34
9

This is the correct approach :

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
             <warName>${name}</warName>
             <outputDirectory>C:\Tomcat7\webapps</outputDirectory>
            </configuration>
        </plugin>

This will put a war file in C:\Tomcat7\webapps folder with the name of the maven project.

Aled Evans
  • 126
  • 1
  • 2
7

You can use http://cargo.codehaus.org/Deploying+to+a+running+container and configure it accordingly.

rkosegi
  • 14,165
  • 5
  • 50
  • 83
  • Deploying to one is easy! :) Deploying to two is hard. :( (unless you run the deploy command twice) http://stackoverflow.com/questions/732275/maven-deploy-to-multiple-tomcat-servers – cgp Apr 19 '09 at 02:07
  • @valerio, link is broken its not opening – Bhaskara Arani Dec 06 '19 at 11:33
6

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.

Bent André Solheim
  • 1,091
  • 7
  • 10
  • I don't want to run the war file. I want it placed in tomcat's webapps directory rather than having to do it manually. – l15a Dec 16 '08 at 03:45
6

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.

Clay
  • 2,932
  • 2
  • 18
  • 16
  • Can you give more information on how this is setup. I've done this in the past but google is having trouble locating example configurations. – Drew Aug 26 '10 at 20:10
  • Upranking this answer. Although many of the other ways work, this was by far the fastest for me (1.5min build vs 7min with the next fastest approach) – samspot Dec 22 '10 at 21:20
3

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>
sal
  • 23,373
  • 15
  • 66
  • 85
2

Thanks for all the above answers.

The below answer works for me. This is just a consolidated one. Nothing special!

</project>
 ........ 
   <build>
    <finalName>HelloWorld1</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId> 
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <outputDirectory>C:\Program Files (x86)\apache-tomcat-8.5.8\webapps\</outputDirectory>
            </configuration>
        </plugin>
    </plugins>
  </build>
</project>

For more information. Please refer to dependency:copy official page for more information.

GrabNewTech
  • 631
  • 6
  • 14
  • Caveat - this technique does not create the war file in the `/target` directory then copy it into the `outputDirectory`. This builds it directly in the `outputDirectory`. So if you are expecting a copy of the war file in the `target` directory, it will not be there. – pamcevoy Dec 07 '19 at 21:31
1

You can also do this with the dependency plugin

Brian Fox
  • 6,782
  • 4
  • 27
  • 31