mvn common commands - unix1998/technical_notes GitHub Wiki
To add a Maven plugin to your project, you need to include it in the <plugins>
section of your pom.xml
file.
Here is an example of how to add the Maven Surefire Plugin, which is used for running unit tests:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<!-- Plugin-specific configuration here -->
</configuration>
</plugin>
</plugins>
</build>
</project>
When you run a Maven build command, such as mvn clean install
, Maven will:
-
Read the
pom.xml
file: Maven parses thepom.xml
file and identifies all dependencies and plugins specified. - Download the plugins: If the plugins are not already present in your local repository, Maven will automatically download them from the configured repositories (e.g., Maven Central).
-
Install and configure the plugins: Maven installs and configures the plugins as specified in the
pom.xml
.
-
mvn clean
: Cleans the project by removing thetarget
directory. -
mvn compile
: Compiles the source code of the project. -
mvn test
: Runs the tests using a testing framework like JUnit or TestNG. -
mvn package
: Packages the compiled code into a distributable format (e.g., JAR, WAR). -
mvn install
: Installs the package into the local repository, which can be used as a dependency in other projects locally. -
mvn deploy
: Deploys the package to a remote repository for sharing with other developers and projects.
You can include multiple plugins in the <plugins>
section. Here’s an example that includes both the Surefire and Compiler plugins:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
</plugins>
</build>
</project>
By adding plugins to the <plugins>
section of your pom.xml
file, Maven will automatically handle the installation and configuration of those plugins during the build process. This allows you to easily manage and use various plugins for different build tasks in your Maven projects.