April 15, 2022

Maven

Top Commands

  • Creating a maven standalone project
mvn archetype:generate -DgroupId=org.cloudhadoop -DartifactId=myproject

  • Creating a web standalone project
mvn archetype:generate   -DarchetypeGroupId=org.apache.maven.archetypes    
-DarchetypeArtifactId=maven-archetype-webapp   -DgroupId=com.cloudhadoop    
-DartifactId=mywebapp

  • To clean a java project with maven
mvn clean

  • Compile maven project
mvn compile

  • To run the maven by skipping unit and integration tests
mvn clean install -Dmaven.test.skip=true or   
mvn clean install -DskipTests=true  

or with a package goal
```markup
mvn package –DskipTests or  
mvn package -Dmaven.test.skip

  • Debug Maven
mvn -X
mvn dependency:tree
mvn dependency:resolve
mvn dependency:resolve -Dartifact=groupId:artifactId:version

  • POM
mvn help:effective-pom

Super POM - Simplest POM - Effective POM

Super POM is

location:

Simplest POM is

location:

Effective POM is a mixture of Super POM and Simplest POM. It's better to say that the Effective POM is actual POM that your project uses.

mvn help:effective-pom

location:

Maven dependencies and transitive dependencies

Dependencies are just dependencies that you literally use, including in your Simplest POM.

Transitive dependencies are hidden from you. Because you use them using explicit dependencies.

mvn dependency:resolve // link

Goal that resolves the project dependencies from the repository. When using this goal while running on Java 9 the module names will be visible as well.

mvn dependency:tree // link

Displays the dependency tree for this project. Multiple formats are supported: text (by default), but also DOT, GraphML, and TGF.

mvn dependency:analyze // link

Analyzes the dependencies of this project and determines which are: used and declared; used and undeclared; unused and declared.

How to exclude transitive dependency?

FE: Dependency org.apache.velocity includes transitive dependency oro.

To exclude you have to write <exclusions> tags in your Simplest POM:

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <exclusions>
            <exclusion>
                <groupId>oro</groupId>
                <artifactId>oro</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

Plugins

Official Apache documentation.

Maven Build Lifecycles

Official Apache documentation.

Maven Packaging Types / JAR - WAR - EAR - ...

  • msi
  • rpm
  • tar
  • tar.bz2
  • tar.gz
  • tbz
  • zip

IndelliJ Idea Hot Keys for Maven

Show Dependencies... // ⇧⌘U //

Show Dependencies Popup // ⌥⌘U //

Load Maven changes // ⇧⌘I // Ctrl+Shift+O