Maven/Eclipse At Home

All the cool kids are doing it… I always arrive on a project after the architect has already set things up, and I wanted to see if I could do it by myself.

  • Create a new eclipse project called “foo”.

Eclipse -> File -> New Project -> Java Project -> Next -> Project name: “foo” -> Finish

  • Download and install maven-2.0.7-bin.zip from here.
# mv maven-2.0.7-bin.zip /usr/local/
# cd /usr/local/
# unzip maven-2.0.7-bin.zip
  • Test the maven install.
# export PATH=/usr/local/maven-2.0.7/bin:$PATH
# export JAVA_HOME=/usr/local/jdk-1.5.0
# mvn --version
Maven version: 2.0.7
Java version: 1.5.0-p2
OS name: "openbsd" version: "4.0" arch: "i386"
  • Add a M2_REPO classpath variable to your Eclipse preferences.

Window -> Preferences -> Java -> Build Path -> Classpath Variables -> New -> Name: “M2_REPO”, Path: “/root/.m2/repository” -> OK -> OK.

# cd ~/
# mkdir .m2
# cd .m2
# mkdir repository
  • Create a new maven application called “my-app”.
# cd /root/workspace/foo
# mvn archetype:create \
  -DarchetypeGroupId=org.apache.maven.archetypes \
  -DgroupId=com.mycompany.app \
  -DartifactId=my-app
  • “After the archetype generation of your first project you will also notice that the following directory structure has been created:” (from getting-started)
/root/workspace/foo/my-app:

|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- mycompany
    |               `-- app
    |                   `-- App.java
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java
  • Test the new maven app.
# cd /root/workspace/foo/my-app
# mvn compile
# mvn test
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.mycompany.app.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.074 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
  • Works from command line 😉 Now I want it to work in eclipse… I thought I just had to do the following and F5/clean/bulid/whatever in eclipse, but the .project/.classpath files were all fubared.
# mvn -DdownloadSources=true eclipse:eclipse
  • So, I tried a few maven eclipse commands, plus I moved everything from /root/workspace/foo/my-app/ to /root/workspace/foo/.
# mvn eclipse:clean
# mvn eclipse:eclipse
  • I dunno… works now (green bar).
/root/workspace/foo/:

|-- pom.xml
 -- .project
 -- .classpath
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- mycompany
    |               `-- app
    |                   `-- App.java
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java
  • .classpath
<classpath>
  <classpathentry kind="src" path="src/main/java"/>
  <classpathentry kind="src" path="src/test/java" output="target/test-classes"/>
  <classpathentry kind="output" path="target/classes"/>
  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
  <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"
sourcepath="M2_REPO/junit/junit/3.8.1/junit-3.8.1-sources.jar"/>
</classpath>
  • .project
<projectDescription>
  <name>my-app</name>
  <comment/>
  <projects/>
  <buildSpec>
    <buildCommand>
      <name>org.eclipse.jdt.core.javabuilder</name>
    </buildCommand>
  </buildSpec>
  <natures>
    <nature>org.eclipse.jdt.core.javanature</nature>
  </natures>
</projectDescription>
  • pom.xml
<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>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-app</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
  • App.java
package com.mycompany.app;

public class App {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}
  • AppTest.java
package com.mycompany.app;

import junit.framework.Test;

public class AppTest extends TestCase {

    public AppTest(String testName) {
        super(testName);
    }

    public static Test suite() {
        return new TestSuite(AppTest.class);
    }

    public void testApp() {
        assertTrue(true);
    }
}

It's only fair to share...
Share on FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

Leave a Reply