Hints for Mavenization of Java Web Applications

I discovered that it is not that easy to transform a web application from a native eclipse/netbeans project to a maven project.

Here are some hints which can make your life easier:

  1. To create the project structure for the web application use this command:
    mvn archetype:create -DgroupId=de.mycompany.app123 -DartifactId=mywebapp -DarchetypeArtifactId=maven-archetype-webapp
    Be sure you replaced the 2.3 header with
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    this was helpful for fixing this issue:
    "The selected Java EE version does not support selected JavaServer Faces version" + restart netbeans!
  2. To use jdk 1.6 do
    <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.0.1</version>
    <configuration>
    <source>1.6</source>
    <target>1.6</target>

    <encoding>UTF-8</encoding>
    </configuration>
    </plugin>
    Maybe for you it is necessary to use tomcat 5.5 for myfaces 1.1. Therefor you probably have to change the <source> element to 1.5 … Here you can determine the class format version
  3. use
    <repositories>
    <repository>
    <id>java.net</id>
    <name>java.net</name>
    <url>http://download.java.net/maven/2/</url&gt;
    </repository>
    </repositories>
    for the missing dependency in hibernate org.hibernate:hibernate:jar:3.2.1.ga: javax.transaction:jta:jar:1.0.1B
  4. All libraries which you need at compile time but not on the server (like the following) should get an scope=provided
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
    </dependency>
  5. All libraries which you need for your tests (e.g. junit or hsqldb) should get an scope=test
    <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.1.102</version>
    <scope>test</scope>
    </dependency>
  6. If your tests are temporarly broken or if you want to speed up testing do:
    <plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.4.3</version>
    <configuration>
    <skip>true</skip>
    </configuration>
    </plugin>
    (See this post for a better solution.)
  7. To invoke the tomcat plugin (‘mvn tomcat:run’) the following code can be necessary
    <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>tomcat-maven-plugin</artifactId>
    <configuration>
    <url>http://localhost:8080/webapp/</url&gt;
    </configuration>
    </plugin>
    This plugin uses tomcat5.5, to use tomcat 6.0 compile it for yourself or run the project from within your IDE with maven plugins.
  8. To invoke the jetty plugin (‘mvn jetty:run’) the following code can be necessary
    <plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>maven-jetty-plugin</artifactId>
    <version>6.1.10</version>
    </plugin>
  9. You get an exception like the following in NetBeans??
    [ERROR]Runtime Exception thrown during execution
    [ERROR]null

    Solution 1 -> use external maven (command line)
    Solution 2 -> try to recompile the project from command line
    Solution 3 -> try to recompile another maven project + restart the IDE (works only sometimes)

  10. if you want to mavenize jar files -you can use this
  11. to set up you own maven repository you can try this tutorial
  12. And last but not least: Be Warned!

Read the following hints if you are an IDE-guy

  • Install the NetBeans plugin. Installation of this is easy -> Options->Plugins->Install Maven Plugin. To use this plugin simply open the pom.xml directly.
  • Installation of the EclipsePlugin:
    • install tomcat 6.0 server (>6.0.16)
    • you will need WTP >2.0.2
    • add http://m2eclipse.sonatype.org/update/ to your update pages; I didn’t tried q4e
    • and install m2eclipse
    • select the necessary packages (scm and mylyn didn’t worked for me …)
    • if not already done add a jdk as default virtual machine to eclipse
    • specify the same java exe in the eclipse.ini file:
      -vm
      C:\Program Files\Java\jdk1.6.0_07\jre\bin\javaw.exe
    • restart eclipse

Some people want to avoid the stuff related to IDEs – they can!

  • Install maven2
    • Under debian you can simply ‘apt-get install maven’.
    • Under windows you have to add the bin folder of maven to the PATH variable.
  • Type the following in the root directory
    1. mvn install
    2. mvn tomcat:run
  • Then go to http://localhost:8080/yourapp/ with you favorite firefox ;-)

Now, my two questions to the readers are:

  • How can change the default tomcat-users.xml under target/tomcat? (for the command ‘mvn tomcat:run’)
    If I would require the user to login (security-constraints in the web.xml) I cannot login …
  • How can I use a master (or parent) module with web applications? I need an example! I already have one for a desktop application.
About these ads