Maven的常用外掛

jackieathome發表於2024-09-01

Apache Maven

Clean

  • Apache Maven Clean Plugin
    清理編譯期在如下目錄內生成的檔案。

    • project.build.directory
    • 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>
    

相關文章