44

I am using Maven 2 and I have an external Tomcat 7. I was wondering how to run Tomcat 7 from using Maven Tomcat plugin.

And does Maven Tomcat plugin in Maven 3 runs the Tomcat 7 by default.

Thanks.

informatik01
  • 16,038
  • 10
  • 74
  • 104
fresh_dev
  • 6,694
  • 23
  • 67
  • 95

2 Answers2

69

This works for me: http://tomcat.apache.org/maven-plugin-2.1/

With this plugin config:

<plugin>
  <groupId>org.apache.tomcat.maven</groupId>
  <artifactId>tomcat7-maven-plugin</artifactId>
  <version>2.1</version>
  <configuration>
    <path>/</path>
  </configuration>
</plugin>

And running with

mvn clean install tomcat7:run

(Please note that tomcat7:run, not tomcat:run.)

Plugin documentation is here: http://tomcat.apache.org/maven-plugin-2.1/tomcat7-maven-plugin/plugin-info.html

For example, the default value of additionalConfigFilesDir is ${basedir}/src/main/tomcatconf, so if you put your configs into this directory it will be used on tomcat7:run.

mvn -X tomcat7:run prints the configration, for example:

[DEBUG] (f) additionalConfigFilesDir = /workspace/webtest1/src/main/tomcatconf
[DEBUG] (f) configurationDir = /workspace/webtest1/target/tomcat
...
[DEBUG] (f) path = /webtest1
...
[DEBUG] (f) port = 8080
[DEBUG] (f) project = ...:webtest1:0.0.1-SNAPSHOT @ /workspace/webtest1/pom.xml
...
[DEBUG] (f) warSourceDirectory = /workspace/webtest1/src/main/webapp

Note that warSourceDirectory points to src (not target), so it runs as an usual dynamic web project, you could change your JSPs, HTMLs and it will visible immediately. That's why the target/tomcat/webapps folder is empty.

palacsint
  • 28,416
  • 10
  • 82
  • 109
  • whn i tried it, i got the exception: `WARNING: Unable to load class [org.codehaus.classworlds.ConfiguratorAdapter] to check against the @HandlesTypes annotation of one or more ServletContentInitializers. java.lang.ClassNotFoundException: org.codehaus.classworlds.ConfiguratorAdapter` any ideas ? – fresh_dev Oct 19 '11 at 12:43
  • i posted about the exception here: http://stackoverflow.com/questions/7821622/unable-to-load-class-org-codehaus-classworlds-configuratoradapter-to-check-aga – fresh_dev Oct 19 '11 at 12:54
  • another question is that when i tried to change the tomcat configuration directory as : ` D:\apache-tomcat-7.0.22\conf ` it doesn't work, it still applies configuration from app\target\tomcat – fresh_dev Oct 19 '11 at 13:09
  • do i have to configure the war directory, meaning the place to deploy war file to, coz the folder myapp\target\tomcat\webapps doesn't contain anything, so that when i try to open http:localhost:8080\ i got exceptions from url rewrite framework that there's no classes here – fresh_dev Oct 19 '11 at 13:57
  • well i used mvn clean and then mvn tomcat7:run and everything works fine, and i have no idea how it's working – fresh_dev Oct 19 '11 at 14:51
  • 4
    that tomcat7:run was very helpful. Had missed that fact and wondered why it still was running Tomcat 6. – philnate Oct 04 '12 at 10:46
  • 1
    Thanks mate, I wasted two days to find a proper documentation to get this plugin working. Your answer really helped – Asanka Jan 08 '14 at 13:42
  • The method started the tomcat smoothly but soon I get "xxxx.servlet cannot be cast to javax.servlet.Servlet", have no idea why. – Drake .C Jun 22 '19 at 00:06
  • @ChenLin: You should ask it as a separate question (with more context info). – palacsint Jun 22 '19 at 09:28
2

Have you tried the tomcat 7 plugin?

Erbureth
  • 3,378
  • 22
  • 39
Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • 2
    i tried it, but it gives me an exception: http://stackoverflow.com/questions/7805464/unable-to-load-class-com-sun-jmx-mbeanserver-repositorysupport-to-check-agains – fresh_dev Oct 18 '11 at 13:01