com.tiobe.jacobe.JacobeMojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of maven-jacobe-plugin Show documentation
Show all versions of maven-jacobe-plugin Show documentation
Simple maven plugin to invoke Jacobe, a code beautifier by TIOBE (www.tiobe.com).
The newest version!
/*
* The MIT License
*
* Copyright (c) 2010 Bruno P. Kinoshita
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.tiobe.jacobe;
import hudson.util.ArgumentListBuilder;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.ExecuteWatchdog;
import org.apache.commons.exec.PumpStreamHandler;
import org.apache.commons.exec.ShutdownHookProcessDestroyer;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
/**
* Jacobe maven plugin mojo.
*
* @author Bruno P. Kinoshita
* @goal jacobe
* @since 1.0
*/
public class JacobeMojo
extends AbstractMojo
{
/**
* Log.
*/
protected static final Logger LOG = Logger.getLogger(JacobeMojo.class.getName());
/**
* Default exit value. Only to initialize a variable.
*/
private static final int DEFAULT_EXIT_VALUE = 1;
/**
* The jacobe executable.
*
* @parameter expression="${jacobe.jacobeExecutable}" default-value="jacobe.exe"
*/
private File jacobeExecutable;
/**
* An array of lists in format [=]
*
* @parameter
*/
private String[] rules;
/**
* The .cfg file that Jacobe uses to load the rules for beautification.
* If no .cfg file is specified, the default sun.cfg is used that is
* searched for in the same directory as the Jacobe executable or in a
* location specified by the JACOBECFG environment variable.
*
* @parameter default-value="sun.cfg"
*/
private File configurationFile;
/**
* The comparison threshold when comparing header comments (used with
* --prependheader and -header). The value is a percentage (between 0 and
* 1). If this attribute is not specified, a default threshold of 5%
* (0.05) is used.
*
* @parameter default-value=0.05
*/
private Float comparisonThreshold;
/**
* The file containing the header comment to insert in case the
* comparison threshold is exceeded.
*
* @parameter
*/
private File headerFile;
/**
* Specify the publication level for Javadoc comments. Should be one of
* public|protected|private. Determines for whether Javadoc comment is
* generated or completed (for classes, interfaces,
* constructors, methods and fields). The default is protected. This
* functionality is only available in the Professional Edition
*
* @parameter default-value="protected"
*/
private String javadoc;
/**
* Treat .java files as Java 1.3 sources. The default is to parse all
* input as Java 1.5 sources.
*
* @parameter default-value=false
*/
private Boolean noAssert;
/**
* Treat .java files as Java 1.4 sources (unless -noassert is
* also specified).
*
* @parameter default-value=false
*/
private Boolean noEnum;
/**
* Do not keep a copy of the original input file.
*
* @parameter default-value=false
*/
private Boolean noBackup;
/**
* Overwrite the original input file instead of creating a file with the
* .jacobe extension.
*
* @parameter default-value=false
*/
private Boolean overwrite;
/*
* Generate output files in the specified directory (also works
* recursively upto some point). The default is to generate output
* files in the same directory as the input file.
*
* @parameter
*/
// private String outputDir;
/**
* The extension for output files. By default, the .jacobe extension
* is appended to the input file.
*
* @parameter default-value="jacobe"
*/
private String outputExtension;
/**
* Directory to look for .java files.
*
* @parameter
* @required
*/
private String input;
/**
* This method calls jacobe.exe passing the parameters from the plugin's
* configuration. Then the source code is nicely formatted.
*
* @throws MojoExecutionException, MojoFailureException
*/
public void execute()
throws MojoExecutionException, MojoFailureException
{
final ArgumentListBuilder args = new ArgumentListBuilder();
// Prepare list of arguments
this.buildArgumentList( args );
final String command = args.toStringWithQuote();
LOG.log( Level.INFO, "Jacobe command: " + command );
int exitValue = DEFAULT_EXIT_VALUE;
final CommandLine commandLine = CommandLine.parse( command );
final DefaultExecutor executor = new DefaultExecutor();
ExecuteWatchdog watchDog = new ExecuteWatchdog(60000);
executor.setWatchdog(watchDog);
PumpStreamHandler streamHandler = new PumpStreamHandler();
executor.setStreamHandler(streamHandler);
ShutdownHookProcessDestroyer processDestroyer = new ShutdownHookProcessDestroyer();
executor.setProcessDestroyer(processDestroyer);
executor.setExitValue( DEFAULT_EXIT_VALUE );
try
{
exitValue = executor.execute( commandLine );
LOG.log( Level.INFO, "Jacobe process exit value: " + exitValue );
}
catch (ExecuteException ee )
{
if ( executor.isFailure(exitValue) )
{
LOG.log( Level.WARNING, ee.getMessage(), ee );
}
}
catch ( IOException ioe )
{
if ( executor.isFailure(exitValue) )
{
LOG.log( Level.WARNING, ioe.getMessage(), ioe );
}
}
finally
{
if ( executor.isFailure(exitValue) )
{
final String message = "Failed invoking jacobe executable with given arguments. Check the execution log for details.";
LOG.log( Level.SEVERE, message );
throw new MojoExecutionException( message );
}
}
}
/**
* Builds the list of arguments.
*
* @param args Argument List Builder
* @return Argument List
*/
protected void buildArgumentList(ArgumentListBuilder args)
{
args.add(jacobeExecutable);
this.addRulesArgument( args );
this.addConfigurationFileArgument( args );
this.addComparisonThresholdArgument( args );
this.addHeaderFileArgument( args );
this.addJavadocArgument( args );
this.addNoAssertArgument( args );
this.addNoEnumArgument( args );
this.addNoBackupArgument( args );
this.addOverwriteArgument( args );
//this.addOutputDirArgument( args );
this.addOutputExtensionArgument( args );
this.addInputArgument( args );
}
/* -- Methods to add arguments to argument list builder -- */
/**
* Adds rule id.
*
* @param args ArgumentListBuilder
*/
private void addRulesArgument(ArgumentListBuilder args) {
if ( rules != null && rules.length >= 0 )
{
for( String rule : rules )
{
if ( ! rule.startsWith("--") )
{
rule = "--" + rule;
}
args.add(rule);
}
}
}
/**
* Adds a configuration file.
*
* @param args ArgumentListBuilder
*/
private void addConfigurationFileArgument(ArgumentListBuilder args)
{
if ( configurationFile != null )
{
args.add("-cfg=" + configurationFile.toString() );
}
}
/**
* Adds a comparison threshold.
*
* @param args ArgumentListBuilder
*/
private void addComparisonThresholdArgument(ArgumentListBuilder args)
{
if( comparisonThreshold != null && comparisonThreshold >= 0.0)
{
args.add("-cmpth=" + comparisonThreshold.toString() );
}
}
/**
* Adds a Header File.
*
* @param args ArgumentListBuilder
*/
private void addHeaderFileArgument(ArgumentListBuilder args)
{
if ( headerFile != null )
{
args.add("-header=" + headerFile.toString() );
}
}
/**
* Adds Javadoc.
*
* @param args ArgumentListBuilder
*/
private void addJavadocArgument(ArgumentListBuilder args)
{
if ( javadoc != null )
{
args.add( "-javadoc=" + javadoc );
}
}
/**
* Adds No Assert.
*
* @param args ArgumentListBuilder
*/
private void addNoAssertArgument(ArgumentListBuilder args)
{
if ( noAssert != null && noAssert == true )
{
args.add( "-noassert" );
}
}
/**
* Adds No Enum.
*
* @param args ArgumentListBuilder
*/
private void addNoEnumArgument(ArgumentListBuilder args)
{
if ( noEnum != null && noEnum == true )
{
args.add( "-noenum" );
}
}
/**
* Adds No Backup.
*
* @param args ArgumentListBuilder
*/
private void addNoBackupArgument(ArgumentListBuilder args)
{
if ( noBackup != null && noBackup == true )
{
args.add( "-nobackup" );
}
}
/**
* Adds No Overwrite.
*
* @param args ArgumentListBuilder
*/
private void addOverwriteArgument(ArgumentListBuilder args)
{
if ( overwrite != null && overwrite == true )
{
args.add( "-overwrite" );
}
}
/*
* Adds Output Directory.
*
* @param args ArgumentListBuilder
*/
// private void addOutputDirArgument(ArgumentListBuilder args)
// {
// if ( outputDir != null )
// {
// args.add("-outdir=" + outputDir);
// }
// }
/**
* Adds Output Extension.
*
* @param args ArgumentListBuilder
*/
private void addOutputExtensionArgument(ArgumentListBuilder args)
{
if ( outputExtension != null )
{
args.add("-outext=" + outputExtension);
}
}
/**
* Adds input expression.
*
* @param args ArgumentListBuilder
*/
private void addInputArgument(ArgumentListBuilder args)
{
if ( input != null )
{
args.add( input );
}
}
}