A minimal Maven pom.xml to start with


When one create a default Maven application using an archetype, it is rather incomplete by default. The following pom was created using maven-archetype-quickstart:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>shz</groupId>
  <artifactId>foo</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>foo</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

I need to more start with. Indeed, it is necessary to configure a few more things in order to start developing. In this post, I will look at a minimal pom.xml project file for a jar.

Setting the dependencies

I most of the time use JUnit and Mockito to unit test my code, therefore, I add the following dependencies to the pom.xml:

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-all</artifactId>
      <version>1.8.5</version>
      <scope>test</scope>
    </dependency>
</dependencies>

Working around the maven-eclipse-plugin bug

I develop in Eclipse. If I don’t specify which version of the maven-eclipse-plugin to use, I run into the following error:

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building aagile 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-eclipse-plugin:2.8:eclipse (default-cli) @ aagile >>>
[INFO]
[INFO] <<< maven-eclipse-plugin:2.8:eclipse (default-cli) @ aagile <<<
[INFO]
[INFO] --- maven-eclipse-plugin:2.8:eclipse (default-cli) @ aagile ---
[INFO] Using Eclipse Workspace: null
[INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER
[INFO] Resource directory's path matches an existing source directory. Resources will be merged with the source directory src/main/java
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.387s
[INFO] Finished at: Sat Nov 20 20:55:45 CET 2010
[INFO] Final Memory: 10M/309M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-eclipse-plugin:2.8:eclipse (default-cli) on project aagile: Request to merge when 'filtering' is not identical. Original=source src/main/java: output=null, include=[**/*.java], exclude=[], test=false, filtering=false, merging with=resource src/main/java: output=target/classes, include=[**/*.xml|**/*.properties], exclude=[**/*.java], test=false, filtering=true -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

To fix it, I simply fix the version of the plugin to use as follows:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-eclipse-plugin</artifactId>
      <version>2.6</version>
      <configuration>
        <ajdtVersion>1.6</ajdtVersion>
      </configuration>
    </plugin>
  </plugins>
</build>

Specify the target version of Java

I want to compile my project against Java 6 so that I can use the features of the language. I do that by specifying the target in the maven-compiler-plugin:

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

The final pom.xml looks like

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>shz</groupId>
  <artifactId>foo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-all</artifactId>
      <version>1.8.5</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <ajdtVersion>1.6</ajdtVersion>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

References

Configuring Java on Kubuntu 10.10


2010/12/25 Update: Added the removal of the OpenJDK and added the installation of the browser plugin

Today, I had to, again, scratch my Kubuntu install and install it over again. I wonder what I did wrong this time :s

Anyway, what’s important is that, by default, Kubuntu 10.10 ships with OpenJDK 6. When I develop, I prefer to use Sun JDK. This post details the steps necessary to configure the Sun JDK on a Kubuntu/Ubuntu Maverick.

If you find any mistake, please tell me so that I can fix it quickly.

1. Add the necessary repository

Sun JDK is not available in the package repositories available out of the box. The packages are available from the Canonical Partner Repository that can be added with the following commands:

sudo add-apt-repository "deb http://archive.canonical.com/ maverick partner"

It is necessary to reload the configuration afterwards using:

sudo apt-get update

2. Install the necessary packages

Once the package repositories are configured, I can now install the JDK packages with:

sudo apt-get install sun-java6-jdk sun-java6-source sun-java6-jre sun-java6-plugin sun-java6-fonts

You can see that I also install the browser plugin as well as the JRE, which the plugin requires.

3. Set Sun JDK as the default JVM

If I execute:

java -version

That yields on my computer:

version "1.6.0_20"
OpenJDK Runtime Environment (IcedTea6 1.9.1) (6b20-1.9.1-1ubuntu3)
OpenJDK 64-Bit Server VM (build 17.0-b16, mixed mode)

I can change the default JVM by executing:

sudo update-alternatives --config java

This command yields:

There are 2 choices for the alternative java (providing /usr/bin/java).
Selection    Path                                      Priority   Status
------------------------------------------------------------
*0           /usr/lib/jvm/java-6-openjdk/jre/bin/java   1061      auto mode
1            /usr/lib/jvm/java-6-openjdk/jre/bin/java   1061      manual mode
2            /usr/lib/jvm/java-6-sun/jre/bin/java       63        manual mode

Press enter to keep the current choice[*], or type selection number:

After I entered 2, the following message appeared:

update-alternatives: using /usr/lib/jvm/java-6-sun/jre/bin/java to provide /usr/bin/java (java) in manual mode.

If I execute again:

java -version

It now yields:

java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
Java HotSpot(TM) 64-Bit Server VM (build 17.1-b03, mixed mode)

Voilà, Sun JDK 6 is the default on my system now.

4. Removing OpenJDK

If you like, you can also completely remove OpenJDK from your computer by running:

sudo apt-get remove openjdk-6-jre openjdk-6-jre-headless openjdk-6-jre-lib

If you now run

sudo update-alternatives --config java

it yields:

There are 1 choices for the alternative java (providing /usr/bin/java).
Selection    Path                                  Priority   Status
------------------------------------------------------------
0            /usr/lib/jvm/java-6-sun/jre/bin/java   63        auto mode*
1            /usr/lib/jvm/java-6-sun/jre/bin/java   63        manual mode

Press enter to keep the current choice[*], or type selection number:

5. Testing your browser plugin

If you want to test that the Java plugin is installed properly, you can surf to http://javatester.org/version.html. You will see something akin to:

6. Set JAVA_HOME

I can now set the JAVA_HOME environment variable to allow applications to find where my JDK is installed. I add the following lines in my .bashrc:

export JAVA_HOME=/usr/lib/jvm/java-6-sun
export PATH=$JAVA_HOME/bin:$PATH
%d bloggers like this: