Skip to content

Some useful snippets and commands for Apache Maven, the project management tool for Java based applications.

Notifications You must be signed in to change notification settings

dipanzan/maven-cheatsheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 

Repository files navigation

Maven Cheat Sheet

Some useful commands and snippets for Apache Maven

Install Maven - Debian Based Systems

sudo apt install maven

See: Installing Apache Maven

Getting started

See: Maven Getting Started Guide

Create First Maven Project

mvn archetype:generate -B \
                    -DarchetypeGroupId=org.apache.maven.archetypes \
                    -DarchetypeArtifactId=maven-archetype-quickstart \
                    -DarchetypeVersion=1.4 \
                    -DgroupId=com.company \
                    -DartifactId=app-name \
                    -Dversion=1.0.0
Archetype help command
mvn archetype:help -Ddetail=true -Dgoal=generate

POM - Project Object Model

The basic POM is inherited from the super POM which is the system wide POM for any Maven project unless a parent POM is supplied. Default repository: Maven Central
See: Introduction to the POM

Basic POM

<project 
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.company</groupId>
    <artifactId>app-name</artifactId>
    <version>1.0.0</version>
    ...
Dependencies
    ...
    <dependencies>
        <dependency>
            <groupId>...</groupId>
            <artifactId>...</artifactId>
            <version>...</version>
            <scope>compile|runtime|test</scope>
        </dependency>
    </dependencies>
    ...
Plugins
    ...
    <build>
        ...
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                </plugin>
                ...
                <plugin>
                    ...
                </plugin> 
            </plugins>
        </pluginManagement>
        ...
Directories
        ...
        <directory>${project.basedir}/target</directory>
        <outputDirectory>${project.build.directory}/classes</outputDirectory>
        <finalName>${project.artifactId}-${project.version}</finalName>
        <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
        <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
        <scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
        <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
        ...
Resources
        ...
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>${project.basedir}/src/test/resources</directory>
            </testResource>
        </testResources>
        ...
    </build>
    ...
</project>

See: Project Object Model

Standard Directory Layout

app-name
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- company
    |               `-- App.java
    `-- test
        `-- java
            `-- com
                `-- company
                    `-- AppTest.java

See: Standard Directory Layout

Maven Workflow

Command Description
mvn compile Compile application sources
mvn test-compile Compile test sources
mvn test Compile test sources and run unit tests
mvn package Generate a JAR
mvn install Install JAR in local repository
mvn site Generate website
mvn clean Remove target dir with build data

Some Useful commands

Enabling Maven debug level logs
mvn clean install –X
Viewing the effective POM file
mvn help:effective-pom
Building a dependency tree
mvn dependency:tree
Viewing the dependency classpath
mvn dependency:build-classpath
Find inconsistent dependencies
mvn dependency:analyze
Copy dependencies from repository to local project directory
mvn dependency:copy-dependencies

Resources

About

Some useful snippets and commands for Apache Maven, the project management tool for Java based applications.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published