Maven 插件編寫記錄

很想嘗試一下Maven插件編寫,之前試過許多的文檔,總沒能成功,也參考了《Maven實(shí)戰(zhàn)》上的內(nèi)容,那上面的內(nèi)容有點(diǎn)早,現(xiàn)在生成代碼中g(shù)oal都已被標(biāo)注為廢棄

/**
 * Goal which touches a timestamp file.
 *
 * @deprecated Don't use!
 */

今天總算是靜下心來弄好了,從早上4點(diǎn)過開始,到現(xiàn)在整理好這篇文檔,弄好之后發(fā)現(xiàn)也不難,可能就是沒有指導(dǎo)獨(dú)自摸索的過程比較痛苦,在此記錄一下。如果這篇文檔能幫助到其它人,就不枉費(fèi)這次分享

說明:

本機(jī)使用jdk11,在編譯插件時(shí)報(bào)錯(cuò),后來降級(jí)為jdk8后正常,估計(jì)是在適配jdk11方面還沒有處理好,這也是踩過的坑

1、使用mvn創(chuàng)建插件項(xiàng)目

mvn archetype:generate

此處會(huì)提示有10種類型可選擇,插件應(yīng)該是第3項(xiàng)maven-archetype-plugin,如果需要加入過濾,則可以filter參數(shù),如下語句

mvn archetype:generate -Dfilter=archetype-plugin

也可以直接使用命令行指定項(xiàng)目類型

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-plugin

需要注意的是,直接用命令行指定項(xiàng)目類型后,默認(rèn)創(chuàng)建的版本為1.0版本,會(huì)提示project created from Old (1.x) Archetype

創(chuàng)建工程的其他信息如下

Confirm properties configuration:
groupId: zhouf.plugin
artifactId: first-maven-plugin
version: 1.0-SNAPSHOT
package: zhouf.plugin
 Y: :

生成如下pom.xml模板

<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>zhouf.plugin</groupId>
  <artifactId>first-maven-plugin</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>maven-plugin</packaging>

  <name>first-maven-plugin Maven Plugin</name>

  <!-- FIXME change it to the project's website -->
  <url>http://maven.apache.org</url>

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

  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-plugin-api</artifactId>
      <version>2.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.plugin-tools</groupId>
      <artifactId>maven-plugin-annotations</artifactId>
      <version>3.2</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.codehaus.plexus</groupId>
      <artifactId>plexus-utils</artifactId>
      <version>3.0.8</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-plugin-plugin</artifactId>
        <version>3.2</version>
        <configuration>
          <goalPrefix>first-maven-plugin</goalPrefix>
          <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
        </configuration>
        <executions>
          <execution>
            <id>mojo-descriptor</id>
            <goals>
              <goal>descriptor</goal>
            </goals>
          </execution>
          <execution>
            <id>help-goal</id>
            <goals>
              <goal>helpmojo</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <profiles>
    <profile>
      <id>run-its</id>
      <build>

        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-invoker-plugin</artifactId>
            <version>1.7</version>
            <configuration>
              <debug>true</debug>
              <cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
              <pomIncludes>
                <pomInclude>*/pom.xml</pomInclude>
              </pomIncludes>
              <postBuildHookScript>verify</postBuildHookScript>
              <localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
              <settingsFile>src/it/settings.xml</settingsFile>
              <goals>
                <goal>clean</goal>
                <goal>test-compile</goal>
              </goals>
            </configuration>
            <executions>
              <execution>
                <id>integration-test</id>
                <goals>
                  <goal>install</goal>
                  <goal>integration-test</goal>
                  <goal>verify</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>

      </build>
    </profile>
  </profiles>
</project>

在生成模板里,有些組件的版本選擇比較早,可以手動(dòng)調(diào)整為新的版本

<dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-plugin-api</artifactId>
    <version>3.8.2</version>
</dependency>
<dependency>
    <groupId>org.apache.maven.plugin-tools</groupId>
    <artifactId>maven-plugin-annotations</artifactId>
    <version>3.6.0</version>
    <scope>provided</scope>
</dependency>

...

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-plugin-plugin</artifactId>
            <version>3.6.1</version>

2、執(zhí)行編譯

mvn clean compile

執(zhí)行后會(huì)出現(xiàn)如下錯(cuò)誤

 -source 1.5 中不支持 try-with-resources
  (請使用 -source 7 或更高版本以啟用 try-with-resources)

修改properties,加入編譯版本為8

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
</properties>

修改后再次編譯

mvn clean compile

編譯通過后,執(zhí)行install

mvn install

也可以用一條語句進(jìn)行

mvn clean install

執(zhí)行成功

如果不希望輸出[INFO]消息,可以使用-q參數(shù)
mvn clean install -q

3、運(yùn)行

運(yùn)行命令格式如下

mvn groupId:artifactId:version:goal

調(diào)用命令如下

mvn zhouf.plugin:first-maven-plugin:1.0-SNAPSHOT:touch

其中version可省

mvn zhouf.plugin:first-maven-plugin:touch

執(zhí)行完后,會(huì)有target\touch.txt文件生成

配置簡略調(diào)用

如果需要調(diào)用時(shí)使用更為簡單的方法,可配置${user.home}/.m2/settings.xml文件,加入pluginGroup

<pluginGroups>
    <!-- pluginGroup
        | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
    <pluginGroup>zhouf.plugin</pluginGroup>
</pluginGroups>

如果不想修改這個(gè)文件,也可以將項(xiàng)目<groupId>設(shè)置為[org.apache.maven.plugins, org.codehaus.mojo]中的一個(gè),系統(tǒng)會(huì)默認(rèn)加載上面兩個(gè)group,但不推薦這樣做

修改項(xiàng)目中的pom.xml文件,將<goalPrefix>設(shè)置為調(diào)用前綴

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-plugin-plugin</artifactId>
        <version>3.4</version>
        <configuration>
          <goalPrefix>first</goalPrefix>
          <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
        </configuration>

然后就可以使用如下命令調(diào)用此插件了

mvn clean install
mvn first:touch

編譯后,會(huì)在本地倉庫中生成

└─zhouf
    └─plugin
        │  maven-metadata-local.xml
        │  resolver-status.properties
        │
        └─first-maven-plugin
            │  maven-metadata-local.xml
            │  resolver-status.properties
            │
            └─1.0-SNAPSHOT
                    first-maven-plugin-1.0-SNAPSHOT.jar
                    first-maven-plugin-1.0-SNAPSHOT.pom
                    maven-metadata-local.xml
                    _remote.repositories

其中plugin\maven-metadata-local.xml文件中生成有訪問前綴,前綴<prefix>可以多個(gè),指向調(diào)用的<artifactId>

<?xml version="1.0" encoding="UTF-8"?>
<metadata>
  <plugins>
    <plugin>
      <name>first-maven-plugin Maven Plugin</name>
      <prefix>first-maven-plugin</prefix>
      <artifactId>first-maven-plugin</artifactId>
    </plugin>
    <plugin>
      <name>first-maven-plugin Maven Plugin</name>
      <prefix>first</prefix>
      <artifactId>first-maven-plugin</artifactId>
    </plugin>
  </plugins>
</metadata>

4、調(diào)用測試

pom.xml文件中有一段<profile>

<profiles>
    <profile>
      <id>run-its</id>
      <build>

        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-invoker-plugin</artifactId>
            <version>1.7</version>
            <configuration>
              <debug>true</debug>
              <cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
              <pomIncludes>
                <pomInclude>*/pom.xml</pomInclude>
              </pomIncludes>
              <postBuildHookScript>verify</postBuildHookScript>
              <localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
              <settingsFile>src/it/settings.xml</settingsFile>
              <goals>
                <goal>clean</goal>
                <goal>test-compile</goal>
              </goals>
            </configuration>
            <executions>
              <execution>
                <id>integration-test</id>
                <goals>
                  <goal>install</goal>
                  <goal>integration-test</goal>
                  <goal>verify</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>

      </build>
    </profile>
</profiles>

執(zhí)行

mvn verify -Prun-its

時(shí)報(bào)如下錯(cuò)誤

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-invoker-plugin:1.7:verify (integration-test) on project second-maven-plugin: 1 build failed. See console output above for details. -> [Help 1]

將版本由1.7改為3.2.1

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
<version>3.2.1</version>

驗(yàn)證通過,使用如下命令做集成測試

mvn integration-test -Prun-its

成功輸出

[INFO] Building: simple-it\pom.xml
[INFO] run post-build script verify.groovy
[INFO]           simple-it\pom.xml ................................ SUCCESS (4.2 s)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

5、小結(jié)

  • 注意jdk版本
  • 修改相關(guān)組件版本
<dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-plugin-api</artifactId>
      <version>3.8.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.plugin-tools</groupId>
      <artifactId>maven-plugin-annotations</artifactId>
      <version>3.6.0</version>
      <scope>provided</scope>
    </dependency>
    ...
</dependencies>



<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-plugin-plugin</artifactId>
        <version>3.6.1</version>
        ...
      </plugin>
    </plugins>
  </build>
  <profiles>
    <profile>
      <id>run-its</id>
      <build>

        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-invoker-plugin</artifactId>
            <version>3.2.1</version>
            ...
          </plugin>
        </plugins>

      </build>
    </profile>
  </profiles>

附:開發(fā)Mojo補(bǔ)充

在編寫mojo類時(shí),可使用如下變量

變量 含義 默認(rèn)值
${project.build.sourceDirectory} 項(xiàng)目的主源碼目錄 src/main/java/.
${project.build.testSourceDirectory} 項(xiàng)目的測試源碼目錄 /src/test/java/.
${project.build.directory} 項(xiàng)目構(gòu)建輸出目錄 target/.
${project.build.outputDirectory} 項(xiàng)目主代碼編譯輸出目錄 target/classes/.
${project.build.testOutputDirectory} 項(xiàng)目測試代碼編譯輸出目錄 target/testclasses/.
${project.groupId} 項(xiàng)目的groupId
${project.artifactId} 項(xiàng)目的artifactId.
${project.version} 項(xiàng)目的version,同${version}等價(jià)
${project.build.finalName} 項(xiàng)目打包輸出文件的名稱 ${project.artifactId}${project.version}.

Mojo類代碼參考

@Mojo( name = "info", defaultPhase = LifecyclePhase.PROCESS_SOURCES )
public class FirstMojo
    extends AbstractMojo
{
    @Parameter(defaultValue = "${project.basedir}")
    private File baseDir;

    @Parameter(defaultValue = "${project.build.sourceDirectory}")
    private File sourceDirectory;

    @Parameter(defaultValue = "${project.build.testSourceDirectory}")
    private File testSourceDirectory;

    @Parameter(defaultValue = "${project.build.directory}")
    private File directory;

    @Parameter(defaultValue = "${project.build.outputDirectory}")
    private File outputDirectory;

    @Parameter(defaultValue = "${project.build.testOutputDirectory}")
    private File testOutputDirectory;

    @Parameter(defaultValue = "${project.build.finalName}")
    private String finalName;

    @Parameter(defaultValue = "${project.groupId}")
    private String groupId;

    @Parameter(defaultValue = "${project.artifactId}")
    private String artifactId;

    @Parameter(defaultValue = "${project.version}")
    private String version;

    public void execute()
        throws MojoExecutionException
    {
        getLog().info( "------------------------------------------------------------------------" );
        getLog().info(String.format(">>> %-25s %s", "baseDir",baseDir.getAbsolutePath()));
        getLog().info(String.format(">>> %-25s %s", "sourceDirectory",sourceDirectory.getAbsolutePath()));
        getLog().info(String.format(">>> %-25s %s", "testSourceDirectory",testSourceDirectory.getAbsolutePath()));
        getLog().info(String.format(">>> %-25s %s", "directory",directory.getAbsolutePath()));
        getLog().info(String.format(">>> %-25s %s", "outputDirectory",outputDirectory.getAbsolutePath()));
        getLog().info(String.format(">>> %-25s %s", "testOutputDirectory",testOutputDirectory.getAbsolutePath()));
        getLog().info(String.format(">>> %-25s %s", "finalName",finalName));
        getLog().info(String.format(">>> %-25s %s", "groupId",groupId));
        getLog().info(String.format(">>> %-25s %s", "artifactId",artifactId));
        getLog().info(String.format(">>> %-25s %s", "version",version));
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Maven作為一個(gè)優(yōu)秀的項(xiàng)目管理工具,其插件機(jī)制為其功能擴(kuò)展提供了非常大的便利性。Maven本身提供了很多的插件。...
    Soclever閱讀 6,443評論 0 9
  • 簡介 概述 Maven 是一個(gè)項(xiàng)目管理和整合工具 Maven 為開發(fā)者提供了一套完整的構(gòu)建生命周期框架 Maven...
    閩越布衣閱讀 4,535評論 6 39
  • 前言 寫本篇文章主要源于看了一下yapi接口信息采集代碼,此工具就是實(shí)現(xiàn)了一個(gè)maven插件,然后發(fā)現(xiàn)自己對如何開...
    未城居士閱讀 894評論 0 0
  • 1、插件 Maven本質(zhì)上是一個(gè)插件框架,它的核心并不執(zhí)行任何具體的構(gòu)建任務(wù),所有這些任務(wù)都交給插件來完成,像編譯...
    冰河winner閱讀 5,608評論 0 2
  • Maven編譯代碼的相關(guān)命令 第一、main目錄下的主代碼編寫完畢后,使用Maven進(jìn)行編譯,在項(xiàng)目根目錄下運(yùn)行命...
    加油小杜閱讀 1,441評論 0 2

友情鏈接更多精彩內(nèi)容