我的Dojo中有一个Mojo(如何编写Maven插件)

https://blog.csdn.net/Luck_ZZ/article/details/105458573?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-0&spm=1001.2101.3001.4242

 https://blog.csdn.net/dnc8371/article/details/106703597

http://c.biancheng.net/view/4720.html

我一直忙于在工作中使用Maven的腋窝。 对于很多开发人员,我会听到:“那又怎样。” 不同之处在于,我通常在无法直接访问Internet的环境中工作。 因此,当我说我经常使用Maven时,这意味着某些事情。

依赖地狱

公平地说,我在示例中一直在随意使用Maven。 我发现下载依赖项并避免“依赖关系地狱”更为方便。 我必须为正在使用的库下载库的情况。 例如,必须下载Hamcrest才能使用JUnit。 在家里,放入对JUnit的依赖关系,而Maven为我下载Hamcrest,因为它是JUnit的依赖关系。 如果存在Hamcrest的依赖关系,Maven也会下载该依赖关系。 在工作时,我需要研究JUnit具有哪些依赖关系,然后研究这些依赖关系具有哪些依赖关系。 由于这种情况,我避免使用库。

形势变化

更改是因为我在工作中使用Spring Roo。 Roo使用Maven来管理它需要合并的Spring依赖项。 由于此更改,我在开发网络上设置了Nexus服务器,并开始了将依赖项从Internet转移到开发网络的过程。 这使我了解了Maven。

我对Maven的了解

在阅读了《 Maven入门》和《 Maven Build Customization》两本书之后,我对Maven以及如何创建本文的主题有了一个很好的了解。 我可以继续学习我所学到的东西,但是我将继续专注于学习Maven插件所需的知识。 我确实假设一个人看到了一个pom文件并从现在开始运行了一些Maven构建。 如果还没有,请购买我读过的书,或者先去http://maven.apache.org 。

Maven插件丰富

Maven基于插件架构。 在Maven中执行任何操作的都是插件。 从诸如编译之类的核心功能到创建网站。 可以想象,每个插件都有某些共同点。

Maven是面向包,生命周期,阶段和目标的

Maven以将某种内容构建到某种打包项目(例如jar文件)中而闻名。 显而易见,这是pom文件的第一行。 可能不知道的是,有一系列“阶段”或“生命周期”恰巧完成了构建程序包的过程(请参阅我在其中所做的事情)。 实际上,这些阶段之一被称为“打包”。 生命周期中的默认阶段列表如下:

  1. 验证
  2. 产生源
  3. 过程源
  4. 产生资源
  5. 流程资源
  6. 编译
  7. 过程类
  8. 生成测试源
  9. 流程测试源
  10. 生成测试资源
  11. 流程测试资源
  12. 测试编译
  13. 过程测试类
  14. 测试
  15. 准备包装
  16. 包
  17. 整合前测试
  18. 整合测试
  19. 整合后测试
  20. 校验
  21. 安装
  22. 部署

Maven构建中正在进行很多工作! 所有这些都由某种插件运行。 每个插件都是由可以设置为在生命周期的特定阶段运行的目标组成的。 例如,将maven-jar-plugin的jar目标设置为在打包阶段运行。

制作插件

现在,您已经对构建过程进行了更深入的了解,是时候解释创建Maven插件所需的内容了。

插件充满了Mojos

什么是魔力? Mojo是Maven普通的Old Java Objects的缩写。 它是Maven识别的插件的最小单位。 所有插件均由mojos制成。 每个mojo与一个目标相关联。 因此,要使一个插件具有多个目标,就需要多个mojo。 令人遗憾的是,我将显示的示例只有一个mojo,但是该示例还将显示测试插件的最佳实践。

最佳做法是唯一允许的做法

看看我在标题中与Dojo交易相关的工作吗? 如果有兴趣,编写插件涉及命名约定,单元测试和集成测试。 命名约定是最重要的

  1. 您不会破坏Apache商标
  2. 其他人知道一个人做了一个插件。

名称中有什么?

Apache插件的命名约定为maven- <title> -plugin。 例如,jar插件名为maven-jar-plugin。 对于其他所有人,命名约定为<title> -maven-plugin。 例如,我创建的示例名为hinter-maven-plugin。 发表本文时使用的另一个示例是Spring Boot的插件,它名为spring-boot-maven-plugin。 Spring Boot的源代码在这里 。 我分叉了它,所以我可以细读和滥用代码。 我的叉子可以在这里找到。 如果您想一起滥用它,请在完成您特定的滥用行为后,将我的副本分叉并发送给我请求请求。 无论如何,如果使用Apache的命名约定,那就是商标侵权。 你被警告了。

单元测试

自动化的单元和集成测试也很重要。 单元测试所遵循的目录模式与普通单元测试略有不同,因此请耐心等待。

对插件进行单元测试时的目录结构为

unit_test_plugin

请注意,所有测试目录都组织在测试目录下。 一个正在制作的是将使用该插件的项目的一个小版本。 测试资源目录下是一个单元目录,后跟子目录中单元的名称。 目标是一次测试一个Mojo。 由于我的示例只有一个mojo,因此我仅设置了一个测试。 除了目录设置以外,还有其他区别,但示例部分将进行介绍。

整合测试

我发现该测试将使您最大程度地了解一个人的特定插件及其工作方式。 目的是测试某种情况,就像它是实际项目构建的一部分一样。 当我指的是实际项目构建时,我的意思是甚至有一个仅用于集成构建的临时存储库。 在阅读了有关如何设置测试的信息之后,我大量借鉴了spring-boot-maven-plugin的集成测试设置和mini pom文件。 好的,我将一些文件复制到示例代码中。 只是通知一个人Spring Boot做得对。 只是为了安全起见,克隆是只读的,或者为了安全起见请分叉其代码。 目录结构如下所示。

it_test_plugin

集成测试不在test目录下,而是位于it目录下src目录下。 我本可以进行更多的集成测试,但到目前为止已经足够了。

例

该示例插件的灵感来自于我心不在and,需要提醒我所做的一切。 我曾想创建一个洗狗提醒插件,但是我决定使用一个普通的提醒插件,因为这样我就可以用它提醒我需要做的任何事情。

Pom文件

  1.  
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.  
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3.  
    <modelVersion>4.0.0</modelVersion>
  4.  
    <groupId>com.darylmathison</groupId>
  5.  
    <artifactId>reminder-maven-plugin</artifactId>
  6.  
    <packaging>maven-plugin</packaging>
  7.  
    <version>1.0-SNAPSHOT</version>
  8.  
    <name>reminder-maven-plugin Maven Mojo</name>
  9.  
    <url>http://maven.apache.org</url>
  10.  
     
  11.  
    <properties>
  12.  
    <mavenVersion>3.2.1</mavenVersion>
  13.  
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14.  
    <maven.compiler.source>1.8</maven.compiler.source>
  15.  
    <maven.compiler.target>1.8</maven.compiler.target>
  16.  
    </properties>
  17.  
     
  18.  
    <dependencies>
  19.  
    <!-- Maven dependencies -->
  20.  
    <dependency>
  21.  
    <groupId>org.apache.maven</groupId>
  22.  
    <artifactId>maven-plugin-api</artifactId>
  23.  
    <version>${mavenVersion}</version>
  24.  
    </dependency>
  25.  
    <dependency>
  26.  
    <groupId>org.apache.maven</groupId>
  27.  
    <artifactId>maven-core</artifactId>
  28.  
    <version>${mavenVersion}</version>
  29.  
    </dependency>
  30.  
    <dependency>
  31.  
    <groupId>org.apache.maven.plugin-tools</groupId>
  32.  
    <artifactId>maven-plugin-annotations</artifactId>
  33.  
    <version>3.2</version>
  34.  
    <scope>provided</scope>
  35.  
    </dependency>
  36.  
    <dependency>
  37.  
    <groupId>org.apache.maven</groupId>
  38.  
    <artifactId>maven-compat</artifactId>
  39.  
    <version>3.2.1</version>
  40.  
    <scope>test</scope>
  41.  
    </dependency>
  42.  
    <dependency>
  43.  
    <groupId>org.apache.maven.plugin-testing</groupId>
  44.  
    <artifactId>maven-plugin-testing-harness</artifactId>
  45.  
    <version>3.1.0</version>
  46.  
    <scope>test</scope>
  47.  
    </dependency>
  48.  
    <dependency>
  49.  
    <groupId>junit</groupId>
  50.  
    <artifactId>junit</artifactId>
  51.  
    <version>3.8.1</version>
  52.  
    <scope>test</scope>
  53.  
    </dependency>
  54.  
    </dependencies>
  55.  
    <build>
  56.  
    <pluginManagement>
  57.  
    <plugins>
  58.  
    <plugin>
  59.  
    <groupId>org.apache.maven.plugins</groupId>
  60.  
    <artifactId>maven-compiler-plugin</artifactId>
  61.  
    <configuration>
  62.  
    <source>1.8</source>
  63.  
    <target>1.8</target>
  64.  
    </configuration>
  65.  
    </plugin>
  66.  
    <plugin>
  67.  
    <groupId>org.apache.maven.plugins</groupId>
  68.  
    <artifactId>maven-plugin-plugin</artifactId>
  69.  
    <version>3.2</version>
  70.  
    <executions>
  71.  
    <execution>
  72.  
    <id>mojo-descriptor</id>
  73.  
    <goals>
  74.  
    <goal>descriptor</goal>
  75.  
    </goals>
  76.  
    </execution>
  77.  
    </executions>
  78.  
    <configuration>
  79.  
    <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
  80.  
    </configuration>
  81.  
    </plugin>
  82.  
    </plugins>
  83.  
    </pluginManagement>
  84.  
    </build>
  85.  
    <profiles>
  86.  
    <profile>
  87.  
    <id>run-its</id>
  88.  
    <build>
  89.  
    <plugins>
  90.  
    <plugin>
  91.  
    <groupId>org.apache.maven.plugins</groupId>
  92.  
    <artifactId>maven-invoker-plugin</artifactId>
  93.  
    <version>1.7</version>
  94.  
    <configuration>
  95.  
    <debug>true</debug>
  96.  
    <cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
  97.  
    <cloneClean>true</cloneClean>
  98.  
    <pomIncludes>
  99.  
    <pomInclude>*/pom.xml</pomInclude>
  100.  
    </pomIncludes>
  101.  
    <addTestClassPath>true</addTestClassPath>
  102.  
    <postBuildHookScript>verify</postBuildHookScript>
  103.  
    <localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
  104.  
    <settingsFile>src/it/settings.xml</settingsFile>
  105.  
    <goals>
  106.  
    <goal>clean</goal>
  107.  
    <goal>compile</goal>
  108.  
    <goal>package</goal>
  109.  
    </goals>
  110.  
    </configuration>
  111.  
    <executions>
  112.  
    <execution>
  113.  
    <id>integration-test</id>
  114.  
    <goals>
  115.  
    <goal>install</goal>
  116.  
    <goal>run</goal>
  117.  
    </goals>
  118.  
    </execution>
  119.  
    </executions>
  120.  
    </plugin>
  121.  
    </plugins>
  122.  
    </build>
  123.  
    </profile>
  124.  
    </profiles>
  125.  
    </project>

可以看到,构建一个插件需要很多插件和依赖项。 这里有一个注释依赖项。 这是Junit的版本。 该版本必须为3.8.1。 这是因为Maven扩展了TestCase类,以使其更易于进行单元测试。 很快就会看到。 注意两个插件,一个是maven-plugin-plugin,另一个是maven-invoker-plugin。 maven-plugin-plugin可以自动为插件创建帮助目标。 maven-invoker-plugin用于集成测试。 它的功能是运行Maven项目,如果一个人正在测试pom中运行它就很方便。

提醒Mojo.java

  1.  
    package com.darylmathison;
  2.  
     
  3.  
    import org.apache.maven.plugin.AbstractMojo;
  4.  
    import org.apache.maven.plugin.MojoExecutionException;
  5.  
    import org.apache.maven.plugins.annotations.LifecyclePhase;
  6.  
    import org.apache.maven.plugins.annotations.Mojo;
  7.  
    import org.apache.maven.plugins.annotations.Parameter;
  8.  
     
  9.  
    import java.io.File;
  10.  
    import java.io.FileReader;
  11.  
    import java.io.FileWriter;
  12.  
    import java.io.IOException;
  13.  
    import java.time.LocalDateTime;
  14.  
    import java.time.format.DateTimeFormatter;
  15.  
    import java.time.temporal.ChronoUnit;
  16.  
     
  17.  
    @Mojo(name = "remind",
  18.  
    defaultPhase = LifecyclePhase.PACKAGE,
  19.  
    requiresOnline = false, requiresProject = true,
  20.  
    threadSafe = false)
  21.  
    public class ReminderMojo extends AbstractMojo {
  22.  
     
  23.  
    @Parameter(property = "basedir", required = true)
  24.  
    protected File basedir;
  25.  
     
  26.  
    @Parameter(property = "message", required = true)
  27.  
    protected String message;
  28.  
     
  29.  
    @Parameter(property = "numOfWeeks", defaultValue = "6", required = true)
  30.  
    protected int numOfWeeks;
  31.  
     
  32.  
    public void execute() throws MojoExecutionException {
  33.  
     
  34.  
    File timestampFile = new File(basedir, "timestamp.txt");
  35.  
    getLog().debug("basedir is " + basedir.getName());
  36.  
    if(!timestampFile.exists()) {
  37.  
    basedir.mkdirs();
  38.  
    getLog().info(message);
  39.  
    timestamp(timestampFile);
  40.  
    } else {
  41.  
    LocalDateTime date = readTimestamp(timestampFile);
  42.  
    date.plus(numOfWeeks, ChronoUnit.WEEKS);
  43.  
    if(date.isBefore(LocalDateTime.now())) {
  44.  
    getLog().info(message);
  45.  
    timestamp(timestampFile);
  46.  
    }
  47.  
    }
  48.  
    }
  49.  
     
  50.  
    private void timestamp(File file) throws MojoExecutionException {
  51.  
    try(FileWriter w = new FileWriter(file)) {
  52.  
    LocalDateTime localDateTime = LocalDateTime.now();
  53.  
    w.write(localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
  54.  
    } catch (IOException e) {
  55.  
    throw new MojoExecutionException("Error creating file " + file, e);
  56.  
    }
  57.  
    }
  58.  
     
  59.  
    private LocalDateTime readTimestamp(File file) throws MojoExecutionException {
  60.  
    try(FileReader r = new FileReader(file)) {
  61.  
    char[] buffer = new char[1024];
  62.  
    int len = r.read(buffer);
  63.  
    LocalDateTime date = LocalDateTime.parse(String.valueOf(buffer, 0, len));
  64.  
    return date;
  65.  
    } catch(IOException ioe) {
  66.  
    throw new MojoExecutionException("Error reading file " + file, ioe);
  67.  
    }
  68.  
    }
  69.  
    }

这是插件中唯一的Mojo,并且可以找到,它非常简单,但是显示了Mojo api提供的一些很酷的功能。 类注释定义目标的名称为“ remind”,并且它不是线程安全的。 它还定义了默认阶段为打包阶段。 我要提到的最后一件事是任何成员变量都可以成为参数。 这成为目标插件的参数。

提醒MojoTest

  1.  
    package com.darylmathison;
  2.  
     
  3.  
    import org.apache.maven.plugin.testing.AbstractMojoTestCase;
  4.  
     
  5.  
    import java.io.File;
  6.  
     
  7.  
    /**
  8.  
    * Created by Daryl on 3/31/2015.
  9.  
    */
  10.  
    public class ReminderMojoTest extends AbstractMojoTestCase {
  11.  
     
  12.  
    @Override
  13.  
    protected void setUp() throws Exception {
  14.  
    super.setUp();
  15.  
    }
  16.  
     
  17.  
    @Override
  18.  
    protected void tearDown() throws Exception {
  19.  
    super.tearDown();
  20.  
    }
  21.  
     
  22.  
    public void testJustMessage() throws Exception {
  23.  
    File pom = getTestFile("src/test/resources/unit/reminder-mojo/pom.xml");
  24.  
    assertNotNull(pom);
  25.  
    assertTrue(pom.exists());
  26.  
    ReminderMojo myMojo = (ReminderMojo) lookupMojo("remind", pom);
  27.  
    assertNotNull(myMojo);
  28.  
    myMojo.execute();
  29.  
    }
  30.  
    }

这是mojo的基本单元测试用例。 测试类扩展AbstractMojoTestCase以获得诸如getTestFile和lookupMojo之类的功能。 以下是测试pom文件。

单元测试Pom文件

  1.  
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.  
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3.  
    <modelVersion>4.0.0</modelVersion>
  4.  
    <groupId>com.darylmathison.test</groupId>
  5.  
    <artifactId>reminder-maven-plugin-test-reminder</artifactId>
  6.  
    <packaging>jar</packaging>
  7.  
    <version>1.0-SNAPSHOT</version>
  8.  
    <name>reminder-maven-plugin Maven Mojo</name>
  9.  
     
  10.  
    <dependencies>
  11.  
    <dependency>
  12.  
    <groupId>junit</groupId>
  13.  
    <artifactId>junit</artifactId>
  14.  
    <version>3.8.1</version>
  15.  
    <scope>test</scope>
  16.  
    </dependency>
  17.  
    </dependencies>
  18.  
    <build>
  19.  
    <plugins>
  20.  
    <plugin>
  21.  
    <groupId>com.darylmathison</groupId>
  22.  
    <artifactId>reminder-maven-plugin</artifactId>
  23.  
    <version>1.0-SNAPSHOT</version>
  24.  
    <configuration>
  25.  
    <basedir>target/test-classes/unit/reminder-mojo</basedir>
  26.  
    <message>Wash the doggies</message>
  27.  
    </configuration>
  28.  
    </plugin>
  29.  
    </plugins>
  30.  
    </build>
  31.  
    </project>

只是定义插件的主pom文件的迷你版本。

整合测试

这是值得的,因为它实际上是Maven项目中的一个单独的Maven项目。 本练习的主要重点是查看插件将执行的操作,而不是其他操作。 示例应用程序很简单,就可以在其中构建Maven项目。 要注意的另一件事是,pom文件使用一些过滤来匹配groupId,artifactId和主插件pom的版本。

Pom文件

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.  
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.  
    <modelVersion>4.0.0</modelVersion>
  5.  
    <groupId>com.darylmathison.it</groupId>
  6.  
    <artifactId>new-timestamp</artifactId>
  7.  
    <version>0.0.1.BUILD-SNAPSHOT</version>
  8.  
    <properties>
  9.  
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  10.  
    </properties>
  11.  
    <build>
  12.  
    <plugins>
  13.  
    <plugin>
  14.  
    <groupId>@project.groupId@</groupId>
  15.  
    <artifactId>@project.artifactId@</artifactId>
  16.  
    <version>@project.version@</version>
  17.  
    <executions>
  18.  
    <execution>
  19.  
    <id>blah</id>
  20.  
    <goals>
  21.  
    <goal>remind</goal>
  22.  
    </goals>
  23.  
    </execution>
  24.  
    </executions>
  25.  
    <configuration>
  26.  
    <message>Wash the doggies</message>
  27.  
    </configuration>
  28.  
    </plugin>
  29.  
    </plugins>
  30.  
    </build>
  31.  
    </project>

SampleApp

  1.  
    package java.test;
  2.  
     
  3.  
    /**
  4.  
    * Created by Daryl on 4/5/2015.
  5.  
    */
  6.  
    public class SampleApp {
  7.  
    public static void Main(String[] args) {
  8.  
    System.out.println("out");
  9.  
    }
  10.  
    }

验证

  1.  
    System.out.println(basedir);
  2.  
    def file = new File(basedir, "timestamp.txt");
  3.  
    return file.exists();

验证脚本是为了确保插件能够完成其预期的工作。 它只是检查timestamp.txt文件是否存在,因为该插件在找不到时间戳文件时会创建一个。 Maven检查验证脚本的输出是对还是错。

结论

哇! 我在这篇文章中介绍了很多内容。 我去举了一个如何创建一个Maven插件的例子。 我还展示了如何使用最佳实践来测试该插件。 我得到了两本书之间的信息,以及一个正在进行的开源项目的实例。 示例代码在GitHub上托管这里 。 这代表了我的新示例主页中的第一个示例

posted on 2021-06-20 06:46  四海骄阳  阅读(279)  评论(0)    收藏  举报

导航