How to use Tomcat with maven in Eclipse

I am looking for the best way to use Tomcat in Eclipse with Maven.

I have experience developing Java EE applications, and I expect similar behavior when using Maven.

My requirements are simple:

  • easy application creation
  • easy deployment
  • hot swap if possible

I don’t know how Tomcat + Eclipse works in detail, so I assumed that the Maven team

mvn eclipse:eclipse -Dwtpversion=2.0 

configures everything correctly, but I have a suspicion that it does not work well (or at least as expected).

When a project (let it call a test) is built into maven, the result is in the ${project}/target/test folder. But it seems to me that the classes for Tomcat are different folders ( ${project}/target/classes ?). This is a wrong IMHO (correct me if I am mistaken), but maven can perform other actions before creating classes - code generation, resource filtering, AOP weaving, etc.

Are there any recommendations for its use? Since running mvn clean install from the command line and deploying the result to Tomcat doesn't sound like an IDE (Integrated development environment) to me. Can I configure the maven eclipse plugin for proper configuration?

I also tried using mvn tomcat:run , but here I am completely lost, I get errors that I do not understand. fe

 java.lang.NoSuchMethodError: org.apache.cxf.common.classloader.ClassLoaderUtils.loadClass 

and I have no idea why it does not work when using server runtime for tomcat 6 works fine from eclipse.

Can I instruct Eclipse to run let say mvn install when I save the class? Will it work than? Will it be fast enough (restoring only changed things + dependencies, since I'm used to standard JSE projects)?

I actually use

  • maven 3.0.4
  • java 1.6
  • eclipse 4.2.1 ( STS 3.2.0.M2)
  • tomcat 6 (we are currently developing Tomcat 6, but I think it is the same for 7)

change

some relevant parts from pom.xml

 <project ...> <modelVersion>4.0.0</modelVersion> <groupId>...</groupId> <artifactId>...</artifactId> <version>...</version> <packaging>war</packaging> <properties> <spring.version>3.1.1.RELEASE</spring.version> <!-- WSDL -> Java (start) --> <cxf.version>2.7.3</cxf.version> <cxf-codegen-plugin.version>2.6.1</cxf-codegen-plugin.version> <cxf.sourceRoot>src/main/generated</cxf.sourceRoot> <!-- WSDL -> Java (end) --> <junit.version>4.11</junit.version> <maven.build.timestamp.format>yyyy-MM-dd HH:mm</maven.build.timestamp.format> <project.build.date>${maven.build.timestamp}</project.build.date> </properties> <dependencies> ... </dependencies> <build> <resources> <resource> <!-- filtering used to replace variables from in property files --> <filtering>true</filtering> <directory>${basedir}/src/main/java</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> <resource> <filtering>false</filtering> <directory>${basedir}/src/main/resources</directory> </resource> </resources> <plugins> <!-- add src/main/generated for maven --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.7</version> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source>src/main/generated</source> </sources> </configuration> </execution> </executions> </plugin> <!-- WSDL -> Java (start) --> <plugin> <!-- !!! READ !!! --> <!-- mvn cxf-codegen-plugin:wsdl2java NOT working, comment phase and run "mvn clean install -DskipTests") --> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin</artifactId> <version>${cxf-codegen-plugin.version}</version> <executions> ... </executions> </plugin> </plugins> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <version>2.5.1</version> <inherited>true</inherited> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <version>2.1.1</version> <artifactId>maven-war-plugin</artifactId> <configuration> <warName>test</warName> </configuration> </plugin> <!-- resources in UTF-8 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.5</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- JUnit --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.8</version> <configuration> <redirectTestOutputToFile>true</redirectTestOutputToFile> </configuration> </plugin> <plugin> <artifactId>maven-eclipse-plugin</artifactId> <configuration> <wtpversion>1.5</wtpversion> </configuration> </plugin> </plugins> </pluginManagement> </build> </project> 
+8
java eclipse maven tomcat
source share
2 answers

Usually I just use the m2eclipse Maven plugin and import the project into eclipse.

+4
source share

If you use mvn eclipse:eclipse , Maven creates Eclipse-specific project settings. Therefore, Eclipse does not execute the Maven command to develop your application in Eclipse. Eclipse automatically deploys your application in the integrated Tomcat, as always.

If you are building or assembling tests, you are using Maven (e.g. mvn package deploy ). This will create a war file and deploy it to Nexus (or any other artifact repository, if configured). in your target/ folder, you will find artifactId-version.war , which can be deployed to any Tomcat you want.

Here is an article with a few tips: https://jacksonps4.me/wordpress/?p=1013

+2
source share

All Articles