Maven插件开发: 实现java类个数统计

  • Post author:
  • Post category:java


1. 设置packageing

<packaging>maven-plugin</packaging>

2.添加依赖

<dependencies>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-plugin-api</artifactId>
        <version>3.5.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugin-tools</groupId>
        <artifactId>maven-plugin-annotations</artifactId>
        <version>3.5</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

3.插件开发

/**
 * Created by xingyuchao on 2017-06-03.
 * 统计以特定格式文件总数
 */
@Mojo(name = "filecount",defaultPhase = LifecyclePhase.PACKAGE)
public class FileCountPlugin extends AbstractMojo{


    private static List<String> fileList = new ArrayList<String>();

    //获取文件路径
    @Parameter
    private String basedir;

    //定义文件后缀   如果要传递 Args的参数需要这样传:mvn clean -Dsuffix=.xml  
    @Parameter(property = "suffix",defaultValue = ".java")
    private String defaultFileSuffix;

    //定义文件作为参数传递的方式,可不传递   
    @Parameter(property = "args")
    private String fileSuffix;

    public void execute() throws MojoExecutionException, MojoFailureException {
        List<String> fileList = scanFile(basedir);
        System.out.println("FilePath:"+basedir);
        System.out.println("fileSuffix:"+fileSuffix);
        System.out.println("defaultFileSuffix:"+defaultFileSuffix);
        System.out.println("FileTotal:"+fileList.size());
    }


    /**
     * 递归统计文件,并将符合条件的文件放入集合中
     * @param filePath
     * @return
     */
    private List<String> scanFile(String filePath) {
        File dir = new File(filePath);
        // 递归查找到所有的class文件
        for (File file : dir.listFiles()) {
            if (file.isDirectory()) {
                scanFile(file.getAbsolutePath());
            } else {
                //以运行参数方式传递作为有限判断条件
                if(fileSuffix != null && !"".equals(fileSuffix)){
                    if(file.getName().endsWith(fileSuffix)){
                        fileList.add(file.getName());
                    }
                }else if(defaultFileSuffix != null && !"".equals(defaultFileSuffix)){
                    //如果优先条件没有传,则以xml中配置为标准
                    if(file.getName().endsWith(defaultFileSuffix)){
                        fileList.add(file.getName());
                    }
                }
            }
        }
        return fileList;
    }
}

4.插件使用

<build>
    <plugins>
        <plugin>
            <groupId>com.zto</groupId>
            <artifactId>zto-plugin</artifactId>
            <version>1.8-SNAPSHOT</version>
            <configuration>
                <basedir>${basedir}</basedir>
            </configuration>
            <executions>
                <execution>
                    <!-- phase表示运行在哪个阶段 -->
                    <phase>clean</phase>
                    <goals>
                        <!--goal标识运行哪个命令,可以这样认为-->
                        <goal>filecount</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

5. 结果示例


git地址:



https://gitee.com/yuchao521/zto-plugin.git




版权声明:本文为yuchao2015原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。