您的位置:首页 > 编程语言 > Java开发

开发自己的Maven插件之七:开发jslint4java-report plugin

2012-07-04 13:26 302 查看
现在你会看到我写这系列文章的真正目的。
jslint是一个Javascript检查工具,jslint4java是一个Java开发者提供的Java项目中很方便使用jslint来检查JavaScript代码的工具。该开发者还提供了jslint4java plugin用于maven项目。
现在简单介绍一下如何使用这个插件:
在你的Java web的maven项目中,pom.xml中添加如下配置:
<plugin>
<groupId>com.googlecode.jslint4java</groupId>
<artifactId>jslint4java-maven-plugin</artifactId>
<version>2.0.2</version>
<executions>
<execution>
<id>lint</id>
<phase>process-resources</phase>
<goals>
<goal>lint</goal>
</goals>
<configuration>
<failOnError>false</failOnError>
<sourceFolders>
<sourceFolder>${basedir}/WebContent/js/app</sourceFolder>
</sourceFolders>
<options>
<undef>true</undef>
</options>
</configuration>
</execution>
</executions>
</plugin>当你执行mvn compile的时候,jslint4java plugin会检查WebContent/js/app目下的所有js文件,并且把结果显示在终端上,同时结果文件写到
target/jslint4java/目录下。
下面是我的项目中的部分扫描结果:
/home/chenshu/104/beacontower/GeoFlow/WebContent/js/app/flowModel.js:3:9: Missing 'use strict' statement.
var name;
^
/home/chenshu/104/beacontower/GeoFlow/WebContent/js/app/flowModel.js:14:9: Missing 'use strict' statement.
var name;
^
/home/chenshu/104/beacontower/GeoFlow/WebContent/js/app/flowModel.js:17:21: Combine this with the previous 'var' statement.
但是这个结果文件并没有被加入到site站点报告中,因此,我写了一个jslint4java-report plugin完成这件事情。

如何创建一个report-plugin工程和前面一样,现在仅仅看看实现代码:
package org.freebird;

/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
import java.io.File;
import org.apache.maven.reporting.AbstractMavenReport;
import org.apache.maven.reporting.MavenReportException;
import java.util.Locale;
import org.apache.maven.doxia.sink.Sink;
import org.apache.maven.doxia.siterenderer.Renderer;
import org.apache.maven.project.MavenProject;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;

/**
* Goal which touches a timestamp file.
*
* @goal my-report
*
* @phase site
*/
public class Jslint4JavaReport extends AbstractMavenReport {

/**
* Describe variable <code>resultFile</code> here.
*
* @parameter
*/
private File resultFile;

private Renderer siteRenderer;

@Override
protected Renderer getSiteRenderer() {
return siteRenderer;
}

@Override
protected String getOutputDirectory() {
return "${project.build.directory}/site/myreport";
}

private MavenProject project;

@Override
protected MavenProject getProject() {
return project;
}

/**
* Describe <code>executeReport</code> method here.
*
* @param locale a <code>Locale</code> value
* @exception MavenReportException if an error occurs
*/
@Override
protected void executeReport(Locale locale) throws MavenReportException {
Sink sink = getSink();

if(!resultFile.exists()) {
throw new MavenReportException("resultFile doesn't exit! Please check your configuration of jslint4java-report plugin");
}
if(!resultFile.canRead()) {
throw new MavenReportException("can't read resultFile for jslint4java-report plugin");
}

FileReader reader = null;
BufferedReader br = null;
try {
reader = new FileReader(resultFile);
br = new BufferedReader(reader);
String row;
while((row = br.readLine()) != null) {
sink.paragraph();
sink.text(row);
sink.paragraph();
}
} catch(Exception ex) {
throw new MavenReportException("Got exception when reading the result file in jslint4java-report plugin",ex);
} finally {
try{
if(br != null) {
br.close();
}

if(reader != null) {
reader.close();
}
}catch(IOException ex){
getLog().error(ex.getMessage(),ex);
}
}
}

/**
* Describe <code>getOutputName</code> method here.
*
* @return a <code>String</code> value
*/
public String getOutputName() {
return "jslint4java-report";
}

/**
* Describe <code>getName</code> method here.
*
* @param locale a <code>Locale</code> value
* @return a <code>String</code> value
*/
public String getName(Locale locale) {
return "jslint4java";
}

/**
* Describe <code>getDescription</code> method here.
*
* @param locale a <code>Locale</code> value
* @return a <code>String</code> value
*/
public String getDescription(Locale locale) {
return "The check result of java script codes using jslint4java";
}
}

在executeReport方法中,将target/jslint4java/report.txt文件的内容读取出来,通过sink的函数创建一个属于site站点的报表。
下一节(最后一篇)会介绍如何得到target/jslint4java/report.txt文件路径的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息