Maven and JAXB

Add this dependency to your pom.xml:

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.2.5-b10</version>
</dependency>

If you have your jaxb.index file in your src/main/java source tree (probably should be in src/main/resources instead, unless you’d rather see it alongside your source), then you’ll also need a file pattern include in your build section:

<resources>
<!-- include jaxb index files -->
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.index</include>
        </includes>
    </resource>
</resources>

Maven: Changing the Java target version

Maven defaults to Java 1.5 by default. You can change this via settings on the maven compiler plugin:

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

Alternatively, you can specify the source and target Java verison using these properties:

    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

Spring STS, Eclipse WTP, and building web apps with Maven

I get Maven. I get Eclipse & WTP. I want to use Maven for my dependency management, and I want to use Eclipse for it’s built in server suppport, for publishing, stopping and starting servers etc.

Several months back I took a look at M2Eclipse and it seemed it didn’t coexist well with WTP’s approach to web app directory structures. I’ve just taken another look and I got it working, but it took a few false starts and trial and error to find what works for me. In trying various things I’d either end up with a WTP style project which Maven wouldn’t build, or a Maven like directory structure which Eclipse wouldn’t build or deploy.

I’m pretty sure there’s many ways to do this, and I’m also sure Spring Tool Suite should be able to deploy a web project out of the box to your tc server without too much trouble, but it took a while to find what works.

One approach that does work but is probably not the best, is to add the tomcat-maven-plugin plugin to your pom.xml, define your Tomcat manager app userid and password in your maven settings.xml, and then deploy using the tomcat:deploy goal – this does work, but I’m pretty sure this is not the way you’re supposed to do things:

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>tomcat-maven-plugin</artifactId>
      <configuration>
      <url>http://localhost:8080/manager/html</url>
      <server>tomcat</server>
      <path>/TestWebApp</path>
      </configuration>
    </plugin>
  </plugins>
</build>

Add to settings.xml, in the servers section:

<server>
  <id>tomcat</id>
  <username>manager</username>
  <password>yourpassword</password>
</server>

The way I think it’s supposed to work with the M2Eclipse plugin and STS, is you start with a web app project created via the M2Eclipse plugins archetype support, which sets up the project config exactly to work with WTP and deploy seamlessly to tc or other WTP supported servers:

File … New… Other… Maven… Maven Project… New

Set project location then Next. From the archetype list, select ‘maven-archetype-webapp’ ‘. Fill in the maven details, then Finish. That’s it.

When you select Run As… Run on Server, it will build automatically using your pom.xml and deploy to the server. Awesome. It’s pretty easy when you know how 🙂

One catch I noticed – the maven-archetype-webapp creates a web.xml which is for Servlet 2.3:

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

This caused me a couple of hours trying to work out why my JSTL tags and EL were not working when deploying to Tomcat 7. Changing the web.xml to 2.5 fixed my jstl issues. Couple of hours of trial and error and plenty of Googling, ready to roll.

2.5 web.xml:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">