How to use the command 'mvn' (with examples)
Apache Maven is a powerful tool used for building and managing Java-based projects. It automates the process of compilation, packaging, and deployment of software. This article will provide examples of various use cases for the mvn
command.
Use case 1: Compile a project
Code:
mvn compile
Motivation:
Compiling a project is the first step towards building a Java-based project. The mvn compile
command compiles the source code of the project and prepares it for packaging.
Explanation:
The compile
goal is responsible for compiling the project’s source code. When you run mvn compile
, it searches for the project’s source code in the specified directory (usually src/main/java
) and compiles it into bytecode.
Example output:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
Use case 2: Compile and package the code in a distributable format
Code:
mvn package
Motivation:
After compiling the project, it is common to package the compiled code into a distributable format, such as a JAR file. The mvn package
command compiles the source code and creates an executable JAR file or any other specified package format.
Explanation:
The package
goal is responsible for creating the distributable format of the project. It includes compiling the project’s source code and packaging it into a target format, such as a JAR file.
Example output:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.729 s
Use case 3: Compile and package, skipping unit tests
Code:
mvn package -DskipTests
Motivation:
During development, running unit tests can be time-consuming. By skipping unit tests, you can save time when performing compilation and packaging tasks. The -DskipTests
argument tells Maven to skip running the unit tests.
Explanation:
The -DskipTests
argument is a system property that Maven recognizes. By including this argument, Maven skips the execution of unit tests during the build process.
Example output:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.851 s
Use case 4: Install the built package in the local Maven repository
Code:
mvn install
Motivation:
Installing the built package in the local Maven repository makes it available for other projects to use. This command invokes both the compile
and package
commands.
Explanation:
The install
goal builds the project like the package
goal, but additionally installs the built package in the local Maven repository (~/.m2/repository
). This makes it available for other projects to use as a dependency.
Example output:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.542 s
Use case 5: Delete build artifacts from the target directory
Code:
mvn clean
Motivation:
Sometimes, it becomes necessary to clean up the build artifacts from the target directory. The mvn clean
command removes all generated files and directories, preparing the project for a fresh build.
Explanation:
The clean
goal removes all generated files and directories from the target directory. This includes class files, JAR files, and other build artifacts.
Example output:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.983 s
Use case 6: Clean and then invoke the package phase
Code:
mvn clean package
Motivation:
The mvn clean package
command is often used to ensure a clean build before packaging the project. It removes any existing build artifacts and then performs the package phase.
Explanation:
The clean
goal deletes all generated files and directories, and the package
goal compiles and packages the project code. This combination ensures a fresh build before creating the distributable format.
Example output:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.061 s
Use case 7: Clean and then package the code with a given build profile
Code:
mvn clean -P profile package
Motivation:
Build profiles allow you to customize the build process with different configurations for different environments. The -P
argument allows you to specify a build profile to use when building the project.
Explanation:
The -P
argument is used to activate a specific build profile specified in the pom.xml
file. By providing a profile name, Maven builds the project with the configuration defined in that profile.
Example output:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.952 s
Use case 8: Run a class with a main method
Code:
mvn exec:java -Dexec.mainClass="com.example.Main" -Dexec.args="argument1 argument2 ..."
Motivation:
The mvn exec:java
command allows you to run a Java class with a main method directly from Maven. This is useful when you want to quickly execute a specific class without building and packaging the whole project.
Explanation:
The exec:java
goal is provided by the Maven Exec Plugin. It allows you to execute a Java class with a main method directly from Maven. The -Dexec.mainClass
argument specifies the fully qualified name of the class to execute, and the -Dexec.args
argument specifies any command-line arguments to be passed to the class.
Example output:
Hello World!
Conclusion:
The mvn
command is a versatile tool for building and managing Java-based projects. It provides various goals and arguments to perform different tasks such as compiling, packaging, and running code. Understanding these use cases will enable you to use Maven effectively and efficiently in your development workflows.