国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

編寫(xiě)maven代碼行統(tǒng)計(jì)插件

2019-11-15 00:16:27
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
編寫(xiě)maven代碼行統(tǒng)計(jì)插件

編寫(xiě)maven插件的步驟

  1. 創(chuàng)建一個(gè)maven-plugin項(xiàng)目:插件本身也是maven項(xiàng)目,只是它的packaging是maven-plugin。
  2. 為插件編寫(xiě)目標(biāo):每個(gè)插件必須包含一個(gè)或多個(gè)目標(biāo),maven稱(chēng)之為Mojo。編寫(xiě)插件時(shí)必須提供一個(gè)或多個(gè)繼承自AbstractMojo的類(lèi)。
  3. 為目標(biāo)提供配置點(diǎn):大部分maven插件以及其目標(biāo)都是可配置的,因此在編寫(xiě)Mojo的時(shí)候需要注意提供可配置的參數(shù)。
  4. 編寫(xiě)代碼,實(shí)現(xiàn)目標(biāo)。
  5. 錯(cuò)誤處理以及日志,為客戶(hù)提供足夠的信息。
  6. 測(cè)試插件

一:創(chuàng)建maven-plugin項(xiàng)目

創(chuàng)建一個(gè)普通的maven項(xiàng)目,只是packaging改為maven-plugin,同時(shí)引入依賴(lài)maven-plugin-api。pom文件如下:

<?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>

二:創(chuàng)建一個(gè)CountMojo類(lèi)。該類(lèi)所必要的三項(xiàng)工作:繼承AbstractMojo、實(shí)現(xiàn)execute()方法、提供@goal標(biāo)注。代碼如下:

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 代碼統(tǒng)計(jì)插件 * @author sawyer * @goal count */public class CountMojo extends AbstractMojo {    /**     * 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 which will be included for counting     * @parameter     */    private String[] includes;    /**     * execute     * @throws MojoExecutionException MojoExecutionException     * @throws MojoFailureException MojoFailureException     */    public void execute() throws MojoExecutionException, MojoFailureException {        if(includes == null || includes.length == 0){            includes = INCLUDES_DEFAULT;        }        try{            countDir(sourceDirectory);            countDir(testSourceDirectory);            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);        }    }    /**     * 統(tǒng)計(jì)某個(gè)目錄下文件的代碼行     * @param dir 目錄     * @throws IOException 文件異常     */    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");    }    /**     * 遞歸獲取文件列表     * @param collected 文件列表list     * @param 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);            }        }    }    /**     * 讀取文件的行數(shù)     * @param file 文件對(duì)象     * @return line     * @throws IOException 文件操作異常     */    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代碼

這里要關(guān)注的是@goal標(biāo)注,這個(gè)標(biāo)注就是這個(gè)類(lèi)的目標(biāo),定義了目標(biāo)之后,我們才可以在項(xiàng)目中配置該插件目標(biāo)。

代碼中還包含了basedir、sourceDirectory、testSourceDirectory等字段,它們都使用了@parameter標(biāo)注,同時(shí)關(guān)鍵字expression表示從系統(tǒng)屬性中讀取這幾個(gè)字段值。

其次,代碼中的includes字段就是用來(lái)為用戶(hù)提供該配置點(diǎn)的,它的類(lèi)型為String數(shù)組,并且使用了@parameter參數(shù)表示用戶(hù)可以自己在pom中配置該字段,使用該插件時(shí)的pom配置如下:

<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>

根據(jù)上面的pom配置,可以看到incuudes字段標(biāo)識(shí)需要統(tǒng)計(jì)的文件后綴,phase表示該插件在compile階段工作,使用該插件的時(shí)候可以看到如下輸出信息:

[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

使用mvn clean install命令將該插件項(xiàng)目構(gòu)建并安裝到本地倉(cāng)庫(kù)后,并按照上面的pom配置,就可以使用它統(tǒng)計(jì)項(xiàng)目的代碼了。

三:錯(cuò)誤處理和日志

上面的CountMojo類(lèi)繼承自AbstratctMojo,跟蹤會(huì)發(fā)現(xiàn)該抽象類(lèi)實(shí)現(xiàn)了類(lèi)Mojo接口,execute()方法就是在這個(gè)接口中定義的。

void execute()        throws MojoExecutionException, MojoFailureException;

這個(gè)方法可以拋出兩種異常,

如果是MojoFailureException異常,則表示為發(fā)現(xiàn)了預(yù)期的錯(cuò)誤,例如單元測(cè)試插件在發(fā)現(xiàn)測(cè)試失敗時(shí)就會(huì)拋出該異常。

如果是MojoExcutionException異常,則表示發(fā)現(xiàn)了未預(yù)期的異常,例如上述代碼中的IOException等。

四:測(cè)試

可在項(xiàng)目中直接集成該插件,也可以在項(xiàng)目目錄下用命令行來(lái)測(cè)試該插件,命令如下:

mvn com.sawyer.edu:maven-loc-plugin:1.0-SNAPSHOT:count


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 水富县| 陆丰市| 石渠县| 永靖县| 沧源| 崇文区| 兴和县| 阜平县| 连云港市| 宁海县| 满城县| 霞浦县| 五莲县| 崇阳县| 陇川县| 天门市| 勃利县| 安泽县| 丰镇市| 甘谷县| 吴堡县| 增城市| 深泽县| 许昌县| 吴忠市| 南汇区| 鄂州市| 阿拉尔市| 诸城市| 富源县| 尼勒克县| 阿合奇县| 余干县| 临沭县| 乐清市| 岫岩| 浪卡子县| 宣恩县| 丹巴县| 河东区| 秭归县|