View Javadoc
1   package org.andromda.beautifier.plugin;
2   
3   /*
4    * Run org.andromda.beautifier as a maven plugin after generating sources.
5    * Beautifier (open source) is available from http://andromda.sourceforge.net/activities.html.
6    * Version 1.1.7 7/15/2007 is current.
7    * Beautifier changes all fully-qualified classname references to import statements.
8    * Normally used with AndroMDA, added the the mda/pom.xml file build plugins as a build goal.
9    */
10  
11  import de.plushnikov.doctorjim.ImportProcessor;
12  import de.plushnikov.doctorjim.javaparser.ParseException;
13  import java.io.File;
14  import java.io.FileNotFoundException;
15  import java.io.IOException;
16  import java.util.Collection;
17  import org.apache.commons.io.FileUtils;
18  import org.apache.log4j.Logger;
19  import org.apache.maven.plugin.AbstractMojo;
20  import org.apache.maven.plugin.MojoExecutionException;
21  
22  /**
23   * Runs andromda beautifier
24   * @phase process-sources
25   * @goal beautify-imports
26   * @requiresProject false
27   */
28  public class ImportBeautifierMojo
29      extends AbstractMojo
30  {
31      private static final Logger LOG = Logger.getLogger(ImportBeautifierMojo.class);
32  
33      /**
34       * Location of the directory to be recursively beautified.
35       * Defaults to all source directories for parent project (..)
36       * @parameter expression="${basedir}"
37       * @optional
38       */
39      private File inputDirectory;
40  
41      /**
42       * Location of the output directory for beautified source.
43       * Defaults to the source directory
44       * @parameter expression="${basedir}"
45       * @optional
46       */
47      private File outputDirectory;
48  
49      /*
50       * Run import beautifier utility.
51       * @parameter default-value="true"
52       * @optional
53      private boolean runBeautifier;
54       */
55  
56      /*
57       * Delegate formatting to Jalopy after beautifying imports.
58       * @parameter default-value="false"
59       * @optional
60      private boolean runJalopy;
61       */
62  
63      /**
64       * 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
65       * site from already formatted source code).
66       *
67       * @parameter expression="${beautifier.run.skip}"
68       */
69      private boolean skipProcessing = false;
70  
71      /**
72       * @see org.apache.maven.plugin.Mojo#execute()
73       */
74      public void execute()
75          throws MojoExecutionException
76      {
77          if (this.skipProcessing)
78          {
79              getLog().info("process-sources:beautify-imports skipProcessing");
80              return;
81          }
82          getLog().info("process-sources:beautify-imports on " + this.inputDirectory);
83          File file = this.inputDirectory;
84          if ( !file.exists() )
85          {
86              throw new MojoExecutionException("Beautifier format input directory does not exist: " + this.inputDirectory);
87          }
88  
89          if (this.outputDirectory==null)
90          {
91              this.outputDirectory = this.inputDirectory;
92          }
93          else
94          {
95              File outputFile = this.outputDirectory;
96              if ( !outputFile.exists() )
97              {
98                  throw new MojoExecutionException("Beautifier format output directory does not exist: "
99                          + 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 }