maven插件的开发

2021-01-28  本文已影响0人  河神

如果你想要了解某个事物,你必须要成为其中的一员。想要理解maven插件,我们就尝试来开发一个maven插件

官方文档位置:http://maven.apache.org/guides/introduction/introduction-to-plugins.html

什么是mavne插件

Maven 实际上是一个依赖插件执行的框架,每个任务实际上是由插件完成。换句话说,插件是执行大部分实际操作的地方,插件用来:创建jar文件,创建war文件,编译代码,单元测试代码,创建项目文档等等操作。您可以想到在项目上执行的几乎所有操作均作为Maven插件实现。

插件是Maven的核心功能,它允许在多个项目中重用通用的构建逻辑。他们通过在项目pom文件的上下文中执行“操作”(即创建WAR文件或编译单元测试)来实现操作,插件同时也是可以定义参数等等。

插件的命名规范

开发

1. 创建一个Maven工程

       <!--plugin插件api-->
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>2.0</version>
        </dependency>

        <!--加入此包就可以使用@Mojo注解-->
        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>3.1</version>
        </dependency>
       <!--用户获取运行插件的Maven工程信息-->
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-project</artifactId>
            <version>3.0-alpha-2</version>
        </dependency>

2. HelloWord插件(Mojo)编写

@Mojo(name = "hello",requiresDependencyResolution = ResolutionScope.COMPILE)
public class HelloMojo extends AbstractMojo {


    /**
     * The greeting to display.
     */
    @Parameter( property = "greeting", defaultValue = "Hello World!" )
    private String greeting;

    @Parameter( defaultValue = "${project}")
    private MavenProject mavenProject;

    public void execute() throws MojoExecutionException, MojoFailureException {
        Class<? extends HelloMojo> aClass = getClass();

        System.out.println(aClass);


        getLog().info("<==============河神说: Hello Mojo start==============>"+greeting);

        List dependencies = mavenProject.getDependencies();
        getLog().info("[dependencies]");
        for (Object dependency : dependencies) {
            getLog().info(dependency.toString());
        }
        getLog().info("[basedir]");
        File basedir = mavenProject.getBasedir();

        List<File> files = searchFiles(basedir, "java");

        for (File file : files) {
            long l = file.lastModified();
            Date date = new Date(l);

            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = dateFormat.format(date);
            getLog().info(file.getName()+"[lastModified]"+format);
        }


        getLog().info("<==============河神说: Hello Mojo end==============>"+greeting);


    }

    public static List<File> searchFiles(File folder, final String keyword) {
        List<File> result = new ArrayList<File>();
        if (folder.isFile()){
            result.add(folder);
        }

        File[] subFolders = folder.listFiles(new FileFilter() {
            public boolean accept(File file) {
                if (file.isDirectory()) {
                    return true;
                }
                return file.getName().toLowerCase().endsWith(keyword);
            }
        });

        if (subFolders != null) {
            for (File file : subFolders) {
                if (file.isFile()) {
                    // 如果是文件则将文件添加到结果列表中
                    result.add(file);
                } else {
                    // 如果是文件夹,则递归调用本方法,然后把所有的文件加到结果列表中
                    result.addAll(searchFiles(file, keyword));
                }
            }
        }

        return result;
    }
}
    <build>
        <plugins>
            <plugin>
                <groupId>com.heshen</groupId>
                <artifactId>api-check-plugin</artifactId>
                <version>1.0-SNAPSHOT</version>
                <configuration>
                    <greeting>设置的参数</greeting>
                </configuration>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>hello</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

3. 直接运行

<pluginGroups>
  <pluginGroup>  groupID </ pluginGroup>
</ pluginGroups>

就可以直接使用 mvnDebug goal 来执行


桃花坞里桃花庵,桃花庵下桃花仙;
桃花仙人种桃树,又摘桃花卖酒钱。


上一篇 下一篇

猜你喜欢

热点阅读