Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Tomcat with maven correctly in Eclipse

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:

  • easy application building
  • easy deployment
  • if possible, hot swap

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

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

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>
like image 285
Betlista Avatar asked Mar 04 '13 12:03

Betlista


2 Answers

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

like image 77
Jay Q. Avatar answered Oct 29 '22 02:10

Jay Q.


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

like image 42
guerda Avatar answered Oct 29 '22 00:10

guerda