Compile the maven code line statistics plug-in and the maven code plug-in
Steps for compiling maven plug-ins
I. Create a maven-plugin Project
Create a normal maven project, but change packaging to maven-plugin, and introduce dependency maven-plugin-api. The pom file is as follows:
<?xml version="1.0" encoding="UTF-8"?><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>com.sawyer.edu</groupId> <artifactId>maven-loc-plugin</artifactId> <version>1.0-SNAPSHOT</version> <packaging>maven-plugin</packaging> <properties> <maven.version>3.0</maven.version> </properties> <dependencies> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-plugin-api</artifactId> <version>${maven.version}</version> </dependency> </dependencies></project>
2. Create a CountMojo class. Three necessary work for this class: Inherit AbstractMojo, implement the execute () method, and provide the @ goal annotation. The Code is as follows:
Package com.sawyer.edu. tlsys. plugin; import org. apache. maven. model. resource; import org. apache. maven. plugin. abstractMojo; import org. apache. maven. plugin. mojoExecutionException; import org. apache. maven. plugin. mojoFailureException; import java. io. bufferedReader; import java. io. file; import java. io. fileReader; import java. io. IOException; import java. util. arrayList; import java. util. list;/*** maven code statistics plug-in * @ au Thor sawyer * @ goal count */public class CountMojo extends actmojo {/*** default includes */private static final String [] INCLUDES_DEFAULT = {"java", "xml ", "properties"};/*** @ parameter expression = "$ {project. basedir} "* @ required * @ readonly */private File basedir;/*** @ parameter expression =" $ {project. build. sourceDirectory} "* @ required * @ readonly */private File sourceDirectory ;/*** @ Parameter expression = "$ {project. build. testSourceDirectory} "* @ required * @ readonly */private File testSourceDirectory;/*** @ parameter expression =" $ {project. build. resources} "* @ required * @ readonly */private List <Resource> resources;/*** @ parameter expression =" $ {project. build. testResources} "* @ required * @ readonly */private List <Resource> testResources;/*** file types whi Ch will be encoded for counting * @ parameter */private String [] includes;/*** execute * @ throws MojoExecutionException * @ throws MojoFailureException */public void execute () throws MojoExecutionException, MojoFailureException {if (primary des = null | primary des. length = 0) {includes = INCLUDES_DEFAULT;} try {countDir (sourceDirectory); countDir (testS OurceDirectory); for (Resource resource: resources) {countDir (new File (resource. getDirectory ();} for (Resource testResource: testResources) {countDir (new File (testResource. getDirectory () ;}} catch (Exception e) {throw new MojoExecutionException ("count failed:", e );}} /*** calculate the code line of a File in a directory * @ param dir directory * @ throws IOException File exception */private void countDir (File dir) throws IOException {if (! Dir. exists () {return ;}list <File> collected = new ArrayList <File> (); collectFiles (collected, dir); int lines = 0; for (File sourceFile: collected) {lines + = countLine (sourceFile);} String path = dir. getAbsolutePath (). substring (basedir. getAbsolutePath (). length (); getLog (). info (path + ":" + lines + "lines of code in" + collected. size () + "files ");} /*** recursively retrieve the file list * @ param collected File List * @ param File file */private void collectFiles (list <file> collected, file File) {if (File. isFile () {for (String include: includes) {if (file. getName (). endsWith (". "+ include) {collected. add (file); break ;}}else {for (File sub: file. listFiles () {collectFiles (collected, sub );}}} /*** number of lines reading the file * @ param File file object * @ return line * @ throws IOException file operation exception */private int countLine (File file) throws IOException {BufferedReader reader = new BufferedReader (new FileReader (file); int line = 0; try {while (reader. ready () {reader. readLine (); line ++ ;}} finally {reader. close () ;}return line ;}}CountMojo code
Here, we need to pay attention to the @ goal annotation. This annotation is the goal of this class. After the goal is defined, we can configure this plug-in goal in the project.
The Code also contains fields such as basedir, sourceDirectory, and testSourceDirectory. They all use @ parameter annotation, And the keyword expression indicates that these fields are read from system properties.
Second, the "des" field in the code is used to provide users with this configuration point. Its type is a String array, and the @ parameter is used to indicate that users can configure this field in pom, the pom configuration when using this plug-in is as follows:
<plugin> <groupId>com.iflytek.edu</groupId> <artifactId>maven-loc-plugin</artifactId> <version>1.0-SNAPSHOT</version> <configuration> <includes> <include>java</include> <include>sql</include> </includes> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>count</goal> </goals> </execution> </executions></plugin>
According to the above pom configuration, we can see that the incuudes field identifies the file suffix to be counted. phase indicates that the plug-in works in the compile stage. When using this plug-in, we can see the following output information:
[INFO] --- maven-loc-plugin:1.0-SNAPSHOT:count (default) @ ZX-jobmonitor-webapp ---[INFO] \src\main\java: 778 lines of code in 6files[INFO] \src\test\java: 0 lines of code in 0files[INFO] \src\main\resources: 0 lines of code in 0files
Use the mvn clean install command to build and install the plug-in project to the local repository, and follow the above pom configuration, you can use it to count the project code.
3. Error Handling and logs
The CountMojo class above inherits from AbstratctMojo. The trace will find that this abstract class implements the Mojo-like interface, and the execute () method is defined in this interface.
void execute() throws MojoExecutionException, MojoFailureException;
This method can throw two types of exceptions,
If it is a MojoFailureException, it indicates that an expected error has been found. For example, the unit test plug-in will throw this exception when detecting a test failure.
If the exception is MojoExcutionException, unexpected exceptions are found, such as IOException in the above Code.
Iv. Test
You can directly integrate the plug-in the project, or test the plug-in using the command line in the project directory. The command is as follows:
mvn com.sawyer.edu:maven-loc-plugin:1.0-SNAPSHOT:count