001package org.andromda.beautifier.plugin; 002 003/* 004 * Run org.andromda.beautifier as a maven plugin after generating sources. 005 * Beautifier (open source) is available from http://andromda.sourceforge.net/activities.html. 006 * Version 1.1.7 7/15/2007 is current. 007 * Beautifier changes all fully-qualified classname references to import statements. 008 * Normally used with AndroMDA, added the the mda/pom.xml file build plugins as a build goal. 009 */ 010 011import de.plushnikov.doctorjim.ImportProcessor; 012import de.plushnikov.doctorjim.javaparser.ParseException; 013import java.io.File; 014import java.io.FileNotFoundException; 015import java.io.IOException; 016import java.util.Collection; 017import org.apache.commons.io.FileUtils; 018import org.apache.log4j.Logger; 019import org.apache.maven.plugin.AbstractMojo; 020import org.apache.maven.plugin.MojoExecutionException; 021 022/** 023 * Runs andromda beautifier 024 * @phase process-sources 025 * @goal beautify-imports 026 * @requiresProject false 027 */ 028public class ImportBeautifierMojo 029 extends AbstractMojo 030{ 031 private static final Logger LOG = Logger.getLogger(ImportBeautifierMojo.class); 032 033 /** 034 * Location of the directory to be recursively beautified. 035 * Defaults to all source directories for parent project (..) 036 * @parameter expression="${basedir}" 037 * @optional 038 */ 039 private File inputDirectory; 040 041 /** 042 * Location of the output directory for beautified source. 043 * Defaults to the source directory 044 * @parameter expression="${basedir}" 045 * @optional 046 */ 047 private File outputDirectory; 048 049 /* 050 * Run import beautifier utility. 051 * @parameter default-value="true" 052 * @optional 053 private boolean runBeautifier; 054 */ 055 056 /* 057 * Delegate formatting to Jalopy after beautifying imports. 058 * @parameter default-value="false" 059 * @optional 060 private boolean runJalopy; 061 */ 062 063 /** 064 * Whether or not processing should be skipped (this is if you just want to force Beautifier not to run on your code, i.e. if generating 065 * site from already formatted source code). 066 * 067 * @parameter expression="${beautifier.run.skip}" 068 */ 069 private boolean skipProcessing = false; 070 071 /** 072 * @see org.apache.maven.plugin.Mojo#execute() 073 */ 074 public void execute() 075 throws MojoExecutionException 076 { 077 if (this.skipProcessing) 078 { 079 getLog().info("process-sources:beautify-imports skipProcessing"); 080 return; 081 } 082 getLog().info("process-sources:beautify-imports on " + this.inputDirectory); 083 File file = this.inputDirectory; 084 if ( !file.exists() ) 085 { 086 throw new MojoExecutionException("Beautifier format input directory does not exist: " + this.inputDirectory); 087 } 088 089 if (this.outputDirectory==null) 090 { 091 this.outputDirectory = this.inputDirectory; 092 } 093 else 094 { 095 File outputFile = this.outputDirectory; 096 if ( !outputFile.exists() ) 097 { 098 throw new MojoExecutionException("Beautifier format output directory does not exist: " 099 + this.outputDirectory); 100 } 101 } 102 String directoryString = null; 103 104 try 105 { 106 directoryString = file.getCanonicalPath(); 107 String[] extensions = {"java"}; 108 Collection<File> files = FileUtils.listFiles(file, extensions, true); 109 ImportProcessor processor = new ImportProcessor(); 110 for (File formatFile : files) 111 { 112 try 113 { 114 LOG.info("Beautifying imports on " + formatFile.getPath()); 115 String output = processor.organizeImports(FileUtils.readFileToString(formatFile)); 116 FileUtils.writeStringToFile(formatFile, output); 117 } 118 catch (ParseException e) 119 { 120 // Don't allow a single file error to kill the whole process. 121 LOG.error("Beautifier error on " + formatFile.getCanonicalPath() + ": " + e.getMessage()); 122 } 123 } 124 } 125 catch ( FileNotFoundException e ) 126 { 127 throw new MojoExecutionException( "FileNotFound creating beautifier output: " + directoryString, e ); 128 } 129 catch ( IOException e ) 130 { 131 throw new MojoExecutionException( "Error creating beautifier output: " + directoryString, e ); 132 } 133 } 134}