I'm looking for the best way how to use Tomcat in Eclipse with Maven.
I have some experience developing Java EE applications and I'm expecting similar behavior when using Maven.
My requirements are simple:
I do not know how Tomcat + Eclipse work in detail so I assumed that Maven command
mvn eclipse:eclipse -Dwtpversion=2.0
setups everything correctly, but I've got suspicion it's not working well (or at least as expected).
When project (let's call it test) is built in maven, result is in ${project}/target/test
folder. But it seems to me that classes for Tomcat are from different folder (${project}/target/classes
?). This is IMHO wrong (correct me if I'm wrong), but maven can do other actions before building classes - code generation, resource filtering, AOP weaving and so on.
Are there some recommendations how to use it? Because performing mvn clean install
from command line and deploying the result to Tomcat doesn't sound to me as IDE (Integrated development environment). It is possible to instruct maven eclipse plugin to setup it correctly?
I also tried to use mvn tomcat:run
but here I'm completely lost, I'm getting errors I do not understand f.e.
java.lang.NoSuchMethodError: org.apache.cxf.common.classloader.ClassLoaderUtils.loadClass
and I have no idea why it's not working while using server runtime for tomcat 6 works fine from eclipse.
Is it possible to instruct Eclipse to perform let say mvn install
when I save the class? Will it work than? Will that be quick enough (rebuilding only changed things + dependencies as I'm used to from standard JSE projects)?
Actually I'm using
edit
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>
I normally just use m2eclipse Maven plugin and import the project into eclipse.
If you use mvn eclipse:eclipse
, Maven creates the project settings specific for Eclipse. This way, no Maven command is executed by Eclipse for developing your application within Eclipse. Eclipse automatically deploys your application to the embedded Tomcat as always.
If you do a test build or production build, then you use Maven (e.g. mvn package deploy
) This will create the war file and deploys it into a Nexus (or any other artifact repository, if configured). in your target/
folder, you will then find a artifactId-version.war
which you can deploy on any Tomcat you want.
Here's an article with some more hints: https://jacksonps4.me/wordpress/?p=1013
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With