Maven的常用插件

cnblogs 2024-09-02 08:09:00 阅读 96

Apache Maven

Clean

    <li>

    Apache Maven Clean Plugin

    清理编译期在如下目录内生成的文件。

    • <code>project.build.directoryli>
    • project.build.outputDirectory
    • project.build.testOutputDirectory
    • project.reporting.outputDirectory
  • Plugin Documentation

  • Usage

    在命令行中执行如下命令:

    mvn clean:clean

    或者

    mvn clean

    修改pom.xml,增加如下配置:

    <project>

    ...

    <build>

    <!-- To define the plugin version in your parent POM -->

    <pluginManagement>

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-clean-plugin</artifactId>

    <version>3.4.0</version>

    </plugin>

    ...

    </plugins>

    </pluginManagement>

    <!-- To use the plugin goals in your POM or parent POM -->

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-clean-plugin</artifactId>

    <executions>

    <execution>

    <id>auto-clean</id>

    <phase>initialize</phase>

    <goals>

    <goal>clean</goal>

    </goals>

    </execution>

    </executions>

    </plugin>

    ...

    </plugins>

    </build>

    ...

    </project>

  • Dependency Information

  • 代码仓库

Compiler

  • Apache Maven Compiler Plugin

    对于Java项目来说,源码的默认路径为src/main/java,测试代码的默认路径为src/test/java

  • Plugin Documentation

  • Usage

    在命令行中执行如下命令:

    mvn compile

    或者

    mvn test-compile

    修改pom.xml,增加如下配置:

    <project>

    ...

    <build>

    <!-- To define the plugin version in your parent POM -->

    <pluginManagement>

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-compiler-plugin</artifactId>

    <version>3.13.0</version>

    </plugin>

    ...

    </plugins>

    </pluginManagement>

    <!-- To use the plugin goals in your POM or parent POM -->

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-compiler-plugin</artifactId>

    </plugin>

    ...

    </plugins>

    </build>

    ...

    </project>

  • Compiling Sources Using A Different JDK

  • Setting the -source and -target of the Java Compiler

    修改pom.xml,增加如下配置:

    <project>

    [...]

    <properties>

    <maven.compiler.source>1.8</maven.compiler.source>

    <maven.compiler.target>1.8</maven.compiler.target>

    </properties>

    [...]

    </project>

  • Setting the --release of the Java Compiler

    使用Java 9及以后的版本编译、构建时,可以指定编译的版本号。

    修改pom.xml,增加如下配置:

    <project>

    [...]

    <properties>

    <maven.compiler.release>8</maven.compiler.release>

    </properties>

    [...]

    </project>

  • Compile Using Memory Allocation Enhancements

    修改pom.xml,增加如下配置,即使用单独的javac进程,初始内存指定为128MiB,最大内存指定为512MiB

    <project>

    [...]

    <build>

    [...]

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-compiler-plugin</artifactId>

    <configuration>

    <fork>true</fork>

    <meminitial>128m</meminitial>

    <maxmem>512m</maxmem>

    </configuration>

    </plugin>

    </plugins>

    [...]

    </build>

    [...]

    </project>

  • Pass Compiler Arguments

    修改pom.xml,增加如下配置:

    <project>

    [...]

    <build>

    [...]

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-compiler-plugin</artifactId>

    <configuration>

    <compilerArgs>

    <arg>-verbose</arg>

    <arg>-Xlint:all,-options,-path</arg>

    </compilerArgs>

    </configuration>

    </plugin>

    </plugins>

    [...]

    </build>

    [...]

    </project>

    相关参数,可以参考javac。

JAR

  • Apache Maven JAR Plugin

    构建jar文件。

  • Plugin Documentation
  • Usage

    修改pom.xml,增加如下配置:

    <project>

    ...

    <modelVersion>4.0.0</modelVersion>

    <groupId>net.jackieathome.studio</groupId>

    <artifactId>core</artifactId>

    <version>1.0-SNAPSHOT</version>

    <!-- <packaging>jar</packaging> -->

    ...

    <build>

    <!-- To define the plugin version in your parent POM -->

    <pluginManagement>

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-jar-plugin</artifactId>

    <version>3.4.2</version>

    </plugin>

    ...

    </plugins>

    </pluginManagement>

    <!-- To use the plugin goals in your POM or parent POM -->

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-jar-plugin</artifactId>

    </plugin>

    ...

    </plugins>

    </build>

    ...

    </project>

WAR

  • Apache Maven WAR Plugin

    构建war文件。

  • Plugin Documentation
  • Usage

    修改pom.xml,增加如下配置:

    <project>

    ...

    <modelVersion>4.0.0</modelVersion>

    <groupId>net.jackieathome.studio</groupId>

    <artifactId>core</artifactId>

    <version>1.0-SNAPSHOT</version>

    <packaging>war</packaging>

    ...

    <build>

    <!-- To define the plugin version in your parent POM -->

    <pluginManagement>

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-war-plugin</artifactId>

    <version>3.4.0</version>

    </plugin>

    ...

    </plugins>

    </pluginManagement>

    <!-- To use the plugin goals in your POM or parent POM -->

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-war-plugin</artifactId>

    </plugin>

    ...

    </plugins>

    </build>

    ...

    </project>

Shade

  • Apache Maven Shade Plugin

    构建uber jar文件。

  • Plugin Documentation
  • Usage

    修改pom.xml,增加如下配置:

    <project>

    ...

    <build>

    <!-- To define the plugin version in your parent POM -->

    <pluginManagement>

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-shade-plugin</artifactId>

    <version>3.6.0</version>

    </plugin>

    ...

    </plugins>

    </pluginManagement>

    <!-- To use the plugin goals in your POM or parent POM -->

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-shade-plugin</artifactId>

    <executions>

    <execution>

    <phase>package</phase>

    <goals>

    <goal>shade</goal>

    </goals>

    </execution>

    </executions>

    </plugin>

    ...

    </plugins>

    </build>

    ...

    </project>

Assembly

  • Apache Maven Assembly Plugin
  • Plugin Documentation
  • Usage

    修改pom.xml,增加如下配置:

    <project>

    ...

    <build>

    <!-- To define the plugin version in your parent POM -->

    <pluginManagement>

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-assembly-plugin</artifactId>

    <version>3.7.1</version>

    </plugin>

    ...

    </plugins>

    </pluginManagement>

    <!-- To use the plugin goals in your POM or parent POM -->

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-assembly-plugin</artifactId>

    <configuration>

    <descriptorRefs>

    <descriptorRef>jar-with-dependencies</descriptorRef>

    </descriptorRefs>

    </configuration>

    </plugin>

    ...

    </plugins>

    </build>

    ...

    </project>

Deploy

  • Apache Maven Deploy Plugin

  • Plugin Documentation

  • Usage

    修改pom.xml,增加如下配置:

    <project>

    ...

    <build>

    <!-- To define the plugin version in your parent POM -->

    <pluginManagement>

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-deploy-plugin</artifactId>

    <version>3.1.3</version>

    </plugin>

    ...

    </plugins>

    </pluginManagement>

    <!-- To use the plugin goals in your POM or parent POM -->

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-deploy-plugin</artifactId>

    </plugin>

    ...

    </plugins>

    </build>

    ...

    </project>

  • Frequently Asked Questions

  • 代码仓库

Install

  • Apache Maven Install Plugin

  • Plugin Documentation

  • Usage

    修改pom.xml,增加如下配置:

    <project>

    ...

    <build>

    <!-- To define the plugin version in your parent POM -->

    <pluginManagement>

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-install-plugin</artifactId>

    <version>3.1.3</version>

    </plugin>

    ...

    </plugins>

    </pluginManagement>

    <!-- To use the plugin goals in your POM or parent POM -->

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-install-plugin</artifactId>

    </plugin>

    ...

    </plugins>

    </build>

    ...

    </project>

Resources

  • Apache Maven Resources Plugin

    源码资源的默认路径为src/main/resources,测试代码的资源的默认路径为src/test/resources

  • Plugin Documentation

  • Usage

    修改pom.xml,增加如下配置:

    <project>

    ...

    <build>

    <!-- To define the plugin version in your parent POM -->

    <pluginManagement>

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-resources-plugin</artifactId>

    <version>3.3.1</version>

    </plugin>

    ...

    </plugins>

    </pluginManagement>

    <!-- To use the plugin goals in your POM or parent POM -->

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-resources-plugin</artifactId>

    </plugin>

    ...

    </plugins>

    </build>

    ...

    </project>

  • Specifying a character encoding scheme

    修改pom.xml,增加如下配置:

    <project ...>

    ...

    <properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    ...

    </properties>

    ..

    </project>

  • Filtering

  • Filtering Properties Files

  • Including and excluding files and directories

  • Binary filtering

Surefire

  • Maven Surefire Plugin

    执行单元测试代码。

  • Plugin Documentation
  • Usage

    修改pom.xml,增加如下配置:

    <project>

    ...

    <build>

    <!-- To define the plugin version in your parent POM -->

    <pluginManagement>

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-surefire-plugin</artifactId>

    <version>3.5.0</version>

    </plugin>

    ...

    </plugins>

    </pluginManagement>

    <!-- To use the plugin goals in your POM or parent POM -->

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-surefire-plugin</artifactId>

    </plugin>

    ...

    </plugins>

    </build>

    ...

    </project>

Failsafe

  • Maven Failsafe Plugin

    运行集成测试用例的插件。

  • Plugin Documentation
  • Usage

    修改pom.xml,增加如下配置:

    <project>

    ...

    <build>

    <!-- To define the plugin version in your parent POM -->

    <pluginManagement>

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-failsafe-plugin</artifactId>

    <version>3.5.0</version>

    </plugin>

    ...

    </plugins>

    </pluginManagement>

    <!-- To use the plugin goals in your POM or parent POM -->

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-failsafe-plugin</artifactId>

    <executions>

    <execution>

    <goals>

    <goal>integration-test</goal>

    <goal>verify</goal>

    </goals>

    </execution>

    </executions>

    </plugin>

    ...

    </plugins>

    </build>

    ...

    </project>



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。