17

It sounds like Eclipse (Kepler) does not have a proper plugin for Tomcat 8. I want to deploy my .war into Tomcat 8 and run it by Maven pom.xml file. Can anyone provide me step-by-step guidance or any resources, please?

My POM file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Test-App</groupId>
  <artifactId>test-rest</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>test-rest Maven Webapp</name>
  <url>http://maven.apache.org</url>
     <!-- Tomcat plugin -->


  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
  <plugins>
  <plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
     <path>/${project.build.finalName}</path>
     <update>true</update>
     <url>http:// localhost:8080/manager/text</url>
     <username>tomcat</username>
     <password>tomcatuser</password>
    </configuration>
   </plugin>
   </plugins>
    <finalName>test-rest</finalName>
  </build>
</project>
bluish
  • 26,356
  • 27
  • 122
  • 180
Débora
  • 5,816
  • 28
  • 99
  • 171
  • 1
    [This post](http://kosalads.blogspot.de/2014/03/maven-deploy-war-in-tomcat-7.html) claims that the maven tomcat-7 plugin also deploys to tomcat 8. Do you also need eclipse integration? Or do you just want to be able to deploy on the command line? – John Jun 17 '14 at 19:05
  • @John. Thanks. I already tried with that. but there is an error " http://cwiki.apache.org/confluence/display/MAVEN/LifecyclePhaseNotFoundException" and not much clear for me. Integration of eclipse is not necessary. What I need is to build my web app into war, deploy into tomcat 8. If any working solution even command line is also enough for me. – Débora Jun 17 '14 at 19:18
  • Maybe it'd be best to post your pom.xml. Which command did trigger the "LifecyclePhaseNotFoundException"? – John Jun 17 '14 at 19:20
  • 2
    For the record, the tomcat7-maven-plugin is working fine for me with Tomcat 8.0.21 using the same configuration as above, excluding username/password entries - I am relying on the default credentials of 'admin' with no password. It sounds like the LifecyclePhaseNotFoundException was probably unrelated to the Tomcat maven plugin. – John Rix Apr 22 '15 at 21:58
  • @JohnRix Can you explain how you say its using Tomcat 8.0.21, since the plugin doesnt have any version of tomcat defined ? – Jess May 31 '18 at 21:38
  • Sorry @Jess, this is going back a long time! If I recall what this was about though, the Tomcat instance I was deploying into from maven was version 8.0.21. As you say, the maven plugin itself does not care what Tomcat version is at the other end of the URL you specify, as long as it quacks like a Tomcat7! – John Rix May 31 '18 at 22:38

4 Answers4

8

Can you use the cargo pluygin instead, this works for me, deploying to tomcat8 :

             <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <version>1.4.8</version>
                <configuration>
                    <container>
                        <containerId>tomcat8x</containerId>
                        <home>${env.CATALINA_HOME}</home>
                    </container>
                    <configuration>
                        <type>existing</type>
                        <home>${env.CATALINA_HOME}</home>
                    </configuration>
                    <deployables>
                        <deployable>
                            <groupId>com.yourcompany</groupId>
                            <artifactId>ROOT</artifactId>
                            <type>war</type>
                            <properties>
                                <context>${project.build.finalName}</context>
                            </properties>
                        </deployable>
                    </deployables>
                    <deployer>
                        <type>installed</type>
                    </deployer>
                </configuration>
            </plugin>
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • thanks. I tried with this. Then error: No plugin found for prefix 'tomcat8' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/../.m2/repository), central (http://repo.maven.apache.org/maven2)] -> [Help 1] To see the full stack trace of the errors, re-run Maven with the -e .... – Débora Jun 17 '14 at 19:50
  • 1
    Can you please add the usage or is the maven command for running the war same as "tomcat7:run-war-only" ? – Jess May 31 '18 at 21:32
6

Example of how to run your war with cargo & tomcat8

mvn clean verify org.codehaus.cargo:cargo-maven2-plugin:run -Dcargo.maven.containerId=tomcat8x -Dcargo.maven.containerUrl=http://repo1.maven.org/maven2/org/apache/tomcat/tomcat/8.5.9/tomcat-8.5.9.zip

If you want to add it to your pom

<plugin>
  <groupId>org.codehaus.cargo</groupId>
  <artifactId>cargo-maven2-plugin</artifactId>
  <version>${cargo.version}</version>
  <configuration>
    <container>
      <containerId>tomcat8x</containerId>
      <zipUrlInstaller>
          <url>http://repo1.maven.org/maven2/org/apache/tomcat/tomcat/8.5.9/tomcat-8.5.9.zip</url>
      </zipUrlInstaller>
    </container>
  </configuration>
</plugin>

Then to run you war (if your pom is a war)

mvn cargo:run

To run with debug (memory options not necessary)

<plugin>
  <groupId>org.codehaus.cargo</groupId>
  <artifactId>cargo-maven2-plugin</artifactId>
  <version>${cargo.version}</version>
  <configuration>
    <container>
      <containerId>tomcat8x</containerId>
      <zipUrlInstaller>
          <url>http://repo1.maven.org/maven2/org/apache/tomcat/tomcat/8.5.9/tomcat-8.5.9.zip</url>
      </zipUrlInstaller>
    </container>
    <configuration>
      <properties>
        <!-- <cargo.servlet.port>8200</cargo.servlet.port> -->
        <cargo.jvmargs>-Xmx2048m -Xms512m</cargo.jvmargs>
        <cargo.start.jvmargs>-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000</cargo.start.jvmargs>
      </properties>
    </configuration>
  </configuration>
</plugin>

Then you need to create a remote debug configuration in your IDE on port 8000 (port by default if nothing specified)

More commands here : https://codehaus-cargo.github.io/cargo/Maven2+plugin.html#Maven2plugin-TheCargoMavenpluginindetail

Ronan Quillevere
  • 3,699
  • 1
  • 29
  • 44
3

Just in case you use a separate Tomcat local server. If you use embedded tomcat, this is not applicable.

In your pom.xml, add the tomcat plugin. (You can use this for both Tomcat 7 and 8):

pom.xml

<!-- Tomcat plugin -->  
<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>    *(From maven > settings.xml)*
  <username>*yourtomcatusername*</username>  
  <password>*yourtomcatpassword*</password>   
 </configuration>   
</plugin>   

tomcat-users.xml

<tomcat-users>
    <role rolename="manager-gui"/>  
        <role rolename="manager-script"/>   
        <user username="admin" password="password" roles="manager-gui,manager-script" />  
</tomcat-users>

settings.xml (maven > conf)

<servers>  
    <server>
       <id>TomcatServer</id>
       <username>admin</username>
       <password>password</password>
    </server>
</servers>  

* deploy/re-deploy

mvn tomcat7:deploy OR mvn tomcat7:redeploy

Tried this on (Both Ubuntu and Windows 8/10):
* Jdk 7 & Tomcat 7
* Jdk 7 & Tomcat 8
* Jdk 8 & Tomcat 7
* Jdk 8 & Tomcat 8

Tested on Both Jdk 7/8 & Tomcat 7/8. (Works with Tomcat 8.5 Also, will test Tomcat 9 soon)

Note:
Tomcat manager should be running or properly setup, before you can use it with maven.

Borgy Manotoy
  • 1,960
  • 5
  • 27
  • 42
  • If it's `tomcat7-maven-plugin` how did you tested with Tomcat 8? – Robert Gabriel Oct 24 '18 at 14:28
  • I have tomcat 8 installed on both my laptops for development... one running on ubuntu and the other is windows. I upgraded my tomcat and jdk from 7 to 8 btw... and it still works even on the same plugin `tomcat7-maven-plugin`. – Borgy Manotoy Oct 24 '18 at 17:05
  • So you are not starting a Tomcat instance each time you are running it from maven? That plugin is just building the war. – Robert Gabriel Oct 25 '18 at 07:49
  • yeah, just like what I indicate in the answer... this is only for the case that you have a separate tomcat server. – Borgy Manotoy Oct 25 '18 at 13:24
  • How can tomcat-users.xml and server.xml changes be passed with command line maven deployment? – DShah Oct 16 '20 at 08:16
2

For Tomcat 8, you should use cargo-maven2-plugin, which works for both maven 2 and maven 3. When configuration cargo plugin, make sure that the value for Container Id is "tomcat8x". Do NOT omit the "x" after tomcat8.

C Wang
  • 29
  • 1