Maven template

June 09, 2014

File structure

$ tree .
.
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── mycompany
    │           └── MyCode.java
    └── test
        └── java
            └── mycompany
                └── MyTest.java

7 directories, 3 files

Configuration

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>mycompany</groupId>
    <artifactId>myproject</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Code

src/main/java/mycompany/MyCode.java:

package mycompany;

public class MyCode {

  public static int add(int x, int y) {
    return x + y;
  }

}

src/test/java/mycompany/MyTest.java:

package mycompany;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class MyTest {

    @Test
    public void testAdd() {
        int sum = MyCode.add(3,5);
        assertEquals("3 + 5 = 8", 8, sum);
    }

}