[JAVA] Frequently used Maven command collection

I have organized a collection of Maven commands that I often use for work.

Verification environment

・ MacOS Sierra -Java version 1.8.0_111 -Apache Maven 3.3.9

Command collection

Maven installation

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

$ tar xzvf apache-maven-3.3.9-bin.tar 
$ vi ~/.bash_profile

bash_profile


export M3_HOME=/Users/local/apache-maven-3.3.9
M3=$M3_HOME/bin
export PATH=$M3:$PATH

Confirmation after installation

$ mvn --version

Creating a template project.

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

Or you can create a new project interactively with mvn archetype: generate.

$ mvn archetype:generate

Choose 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-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
Choose a number: 6: 
Define value for property 'groupId': : com.myapp
Define value for property 'artifactId': : myapp
Define value for property 'version':  1.0-SNAPSHOT: : 
Define value for property 'package':  com.myapp: : 
Confirm properties configuration:
groupId: com.myapp
artifactId: myapp
version: 1.0-SNAPSHOT
package: com.myapp
 Y: : 

Project verification

Do not compile and check the project for errors.

$ mvn validate

compile

Compile the project

$ mvn compile

When specifying the JDK version at compile time or the 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 the test

Run Junit test class

$ mvn test

Run tests only for specific classes.

$ mvn test -Dtest=BlogicTest

Only execute tests for specific classes and specific methods.

$ mvn test -Dtest= BlogicTest#blogicTest1

Use wildcards to batch execute the target test class.

$ mvn test -Dtest=Blogic*Test

Package generation

Generate artifacts such as JAR and WAR. Executing the package also executes the valiate, compile, test, and package phases.

$ mvn package

Skip the execution of the test code and generate the JAR.

$ mvn package -DskipTests

Skip compiling test code and running tests.

$ mvn package -Dmaven.test.skip

Installation

Install the JAR in your local repository.

$ mvn install

Install the third party JAR in your 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

Skip compiling test code and running tests.

$ mvn install -Dmaven.test.skip

Deploy

Register the package in the remote repository. If you execute deploy, all the previous phases will be executed.

$ mvn deploy

Clean Delete the target directory.

$ mvn clean

It is common to execute it after package and install.

$ mvn clean package
$ mvn clean install

Information display

Display the dependencies of the project in a tree.

$ mvn dependency:tree

Display pom file information, including default settings.

$ mvn help:effective-pom

Display information in the configuration file, including default settings.

$ mvn help:effective-settings

Generate build.xml for ANT

Generate build.xml for ant using maven-ant-plugin.

$ 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

Generating a project for Eclipse

.project and .classpath are generated using maven-eclipse-plugin and can be imported by Eclipse.

$ mvn eclipse:eclipse

Library output

Use dependency-maven-plugin to copy dependent libraries to a specific directory.

$ mvn dependency:copy-dependencies -DoutputDirectory=lib

Assembly output

Create an executable Jar (assembly) from a pre-built project using maven-assembly-plugin.

$ mvn assembly:assembly -DdescriptorId=bin

Javadoc Generate Javadoc.

$ mvn javadoc:javadoc

Library source

Output the source of the dependent library.

$ mvn dependency:sources

Recommended Posts

Frequently used Maven command collection
Frequently used docker-compose command
A collection of RSpecs that I used frequently
Frequently used Java generics
Docker Frequently used commands
A collection of commands that were frequently used on heroku
IntelliJ Frequently Used Operation Notes
Frequently used processes in SpreadSheet
rails console Frequently used operations
git flow Frequently used commands Memo
Frequently used methods in Active Record
Summary of frequently used Docker commands
Create command line app with maven
[Docker] Other frequently used (probably) Docker Instructions
Create a Maven project with a command
Introduction to Docker (1) Frequently used commands