Maven编写插件统计项目中一共有多少了.java文件

Maven编写插件统计项目中一共有多少了.java文件

特别说明:本文参考了:无杨人生 作者的博客

需求:编写一个maven插件,安装到仓库里面,可以在另一个工程里面引用这个插件,
让这个插件能工统计当前项目中一共有多少个java文件.

1.编写maven插件:

新建maven工程(普通的maven工程)
由于要变成maven插件,所以打包方式要改变

<packaging>maven-plugin</packaging>

并且需要导入maven插件编写需要的依赖:

<dependencies>
        <!--  这个依赖是因为插件了需要继承AbstractMojo  -->
        <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>

可以设置编码格式:

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

pom配置完成

2.编写插件类,实现统计功能:

在项目中建立自己的类: 本人写的是 CountMojo

整个类的代码:

package com.thp;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Mojo(name="findCountTotal",requiresProject = false, defaultPhase = LifecyclePhase.PACKAGE)
public class CountMojo extends AbstractMojo {


    // 这个集合石用来放文件的
    private static List<String> fileList = new ArrayList<String>();

    private int allLines = 0;


    @Parameter(property = "currentBaseDir",defaultValue = "User/pathHome")
    private String currentBaseDir;  // 这个参数是引入本插件的工程传进来的文件夹

    @Parameter(property = "suffix",defaultValue = ".java")
    private String suffix;  // 这个参数是引入本插件的工程传入进来的文件类型 (例如.java)




    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        List<String> fileList = scanFile(currentBaseDir);
        System.out.println("FilePath:" + currentBaseDir);
        System.out.println("FileSuffix:" + suffix);
        System.out.println("FileTotal:"+fileList.size());
        System.out.println("allLines:"+allLines);
    }

    /**
     * 递归统计文件,将所有符合条件的文件放入集合中
     */
    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(file.getName().endsWith(suffix)) {
                    fileList.add(file.getName());  // 符合条件 添加到集合中
                    allLines+=countLines(file);    // 统计行数
                }
            }
        }
        return fileList;
    }
    private int countLines(File file) {
        int lines = 0;
        try {
            // 转换成高级流  可以按行读
            BufferedReader reader =  new BufferedReader(new FileReader(file));
            while(reader.ready()) {
                reader.readLine();
                lines++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return lines;
    }
}

在写完之后 要安装到本地仓库,会在deploy到远程仓库,让别的人可以引用


3.在别的项目中引入插件:

​在项目中的pom文件中印务插件

  <build>
       <plugins>
           <plugin>
               <groupId>com.thp</groupId>
               <artifactId>codeRownums-plugin</artifactId>
               <version>1.0-SNAPSHOT</version>
               <configuration>
                   <!--  basedir  :maven的系统变量 -->
                   <currentBaseDir>${basedir}</currentBaseDir>
                   <suffix>.java</suffix>
               </configuration>
               <executions>
                   <execution>
                       <!--  在clean这个phrase执行 -->
                       <phase>clean</phase>
                       <goals>
                           <goal>findCountTotal</goal>
                       </goals>
                   </execution>
               </executions>
           </plugin>
       </plugins>
  </build>

直接在当前项目中运行mvn clean就能运行这个插件

或者:不在pom文件里面进行挂载
直接运行命令:

mvn com.thp.asset-cache-control:1.0-SNAPSHOT:findCountTotal -DcurrentBaseDir=D:\workspace\workspace-idea\asset-cache-control -Dsuffix=.java

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_38200548/article/details/81775207