Frequently used Maven commands

Command Collection

Installation of Maven

Unzip the tar file and append M3_HOME to bash_profile to add it to PATH.

$ takes xzvf apache-maven-3.3.9-bin.tar  $ vi ~ / .bash_profile
bash_profile
export M3_HOME=/Users/local/apache-maven-3.3.9M3=$M3_HOME/binexport PATH=$M3:$PATH

Confirmation after installation

$ mvn --version

Create model project.

$ mvn -B archetype:generate \ -DarchetypeGroupId=org.apache.maven.archetypes \ -DgroupId=com.myapp \ -DartifactId=myapp

You mvn archetype:generatecan create a new project interactively with or.

$ mvn archetype:generateChoose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 923: Choose org.apache.maven.archetypes:maven-archetype-quickstart version: 1: 1.0-alpha-12: 1.0-alpha-23: 1.0-alpha-34: 1.0-alpha-45: 1.06: 1.1Choose a number: 6: Define value for property 'groupId': : com.myappDefine value for property 'artifactId': : myappDefine value for property 'version':  1.0-SNAPSHOT: : Define value for property 'package':  com.myapp: : Confirm properties configuration:groupId: com.myappartifactId: myappversion: 1.0-SNAPSHOTpackage: com.myapp Y: : 

Project validation

Do not compile, only check project for errors.

$ mvn validate

compile

Compile the project

$ mvn compile


When specifying JDK version at compile time or JVM option, add the following information to pom.xml.

pom.xml
<project>  ...  <build>    <plugins>      <plugin>          <groupId>org.apache.maven.plugins</groupId>          <artifactId>maven-compiler-plugin</artifactId>          <version>3.3</version>          <configuration>              <source>1.8</source>              <target>1.8</target>              <encoding>UTF-8</encoding>              <compilerArgs>                  <arg>-g</arg>                  <arg>-Xlint</arg>              </compilerArgs>          </configuration>      </plugin>    </plugins>  </build></project>


Run test

Running the JUnit test class

$ mvn test

Run test only for specific classes.

$ mvn test -Dtest=BlogicTest

Run test only for a specific class, specific method.

$ mvn test -Dtest= BlogicTest#blogicTest1

Execute target test classes collectively using wildcards.

$ mvn test -Dtest=Blogic*Test

Package generation

Generate deliverables such as JAR and WAR. 
Execution of package also executes phases of validate, compile, test, package.

$ mvn package

Skipping execution of test code and generating JAR.

$ mvn package -DskipTests

Compile test code and skip execution of the test.

$ mvn package -Dmaven.test.skip

Installation

Install the JAR in the local repository.

$ mvn install

Install the third-party JAR in the local repository.

mvn install:install-file -Dfile=target/foo.jar -DgroupId=com.foo -DartifactId=foo \-Dversion=1.0 -Dpackaging=jar -DgeneratePom=true

Skip execution of test code.

$ mvn install -DskipTests

Compile test code and skip execution of the test.

$ mvn install -Dmaven.test.skip

Deploy

Register the package in the remote repository. 
If you run deploy, all previous phases are executed.

$ mvn deploy

Clean

Delete the target directory.

$ mvn clean

It is common to execute it before package and install.

$ mvn clean package$ mvn clean install

Information display

Display the dependency that the project has in a tree.

$ mvn dependency:tree

Display information of pom file including default setting.

$ mvn help:effective-pom

Display information of the setting file including default setting.

$ mvn help:effective-settings

Generate build.xml for ANT

maven-ant-pluginTo generate build.xml for ant.

$ mvn ant:ant$ tree.├── build.xml├── maven-build.properties├── maven-build.xml├── pom.xml└── src    ├── main    │   └── java    │ └── com    │           └── myapp    │               └── App.java    └── test        └── java            └── com                └── myapp                    └── AppTest.java

Generate project for Eclipse

maven-eclipse-pluginUtilizing,.project .classpathit is generated, it becomes possible to import in Eclipse.

$ mvn eclipse:eclipse

Library output

dependency-maven-pluginAnd copy dependent libraries to a specific directory.

$ mvn dependency:copy-dependencies -DoutputDirectory=lib

Output of assembly

maven-assembly-pluginTo create executable Jar (assembly) from the already built project.

$ mvn assembly:assembly -DdescriptorId=bin

Javadoc

Generate Javadoc.

$ mvn javadoc:javadoc

Library source

Output the source of the dependent library.

$ mvn dependency:sources