Maven Cheatsheet

kuniga.me > Docs > Maven Cheatsheet

Maven Cheatsheet

Setup

Create a project structure

mvn archetype:generate -DgroupId=com.example -DartifactId=my-java-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Template file

App.java:

public class App
{
    public static void main( String[] args )
    {
        System.out.println("Hello World!");
    }
}

Configure build

pom.xml:

<project>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>com.example.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Configure dependencies

pom.xml:

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

Operational

Compile

mvn compile

Run

java is the name provided in <goal> on the build config.

mvn exec:java