View Javadoc

1   package net_alchim31_maven_yuicompressor;
2   
3   import java.io.File;
4   import java.util.List;
5   
6   import org.apache.maven.model.Resource;
7   import org.apache.maven.plugin.AbstractMojo;
8   import org.apache.maven.plugin.MojoExecutionException;
9   import org.apache.maven.plugin.MojoFailureException;
10  import org.apache.maven.project.MavenProject;
11  import org.codehaus.plexus.util.DirectoryScanner;
12  
13  /**
14   * Common class for mojos.
15   *
16   * @author David Bernard
17   * @created 2007-08-29
18   */
19  // @SuppressWarnings("unchecked")
20  public abstract class MojoSupport extends AbstractMojo {
21      private static final String[] EMPTY_STRING_ARRAY = {};
22  
23      /**
24       * Javascript source directory. (result will be put to outputDirectory).
25       * This allow project with "src/main/js" structure.
26       *
27       * @parameter expression="${project.build.sourceDirectory}/../js"
28       */
29      private File sourceDirectory;
30  
31      /**
32       * Single directory for extra files to include in the WAR.
33       *
34       * @parameter expression="${basedir}/src/main/webapp"
35       */
36      private File warSourceDirectory;
37  
38      /**
39       * The directory where the webapp is built.
40       *
41       * @parameter expression="${project.build.directory}/${project.build.finalName}"
42       */
43      private File webappDirectory;
44  
45      /**
46       * The output directory into which to copy the resources.
47       *
48       * @parameter expression="${project.build.outputDirectory}"
49       */
50      private File outputDirectory;
51  
52      /**
53       * The list of resources we want to transfer.
54       *
55       * @parameter expression="${project.resources}"
56       */
57      private List<Resource> resources;
58  
59      /**
60       * list of additional excludes
61       *
62       * @parameter
63       */
64      private List<String> excludes;
65  
66      /**
67       * list of additional includes
68       * 
69       * @parameter
70       */
71      private List<String> includes;
72      
73      /**
74       * Excludes files from webapp directory
75       * 
76       * @parameter
77       */
78      private boolean excludeWarSourceDirectory = false;
79      
80      /**
81       * Excludes files from resources directories.
82       *
83       * @parameter default-value="false"
84       */
85      private boolean excludeResources = false;
86  
87      /**
88       * @parameter expression="${project}"
89       * @readonly
90       * @required
91       */
92      protected MavenProject project;
93  
94      /**
95       * [js only] Display possible errors in the code
96       *
97       * @parameter expression="${maven.yuicompressor.jswarn}" default-value="true"
98       */
99      protected boolean jswarn;
100 
101     /**
102      * Whether to skip execution.
103      *
104      * @parameter expression="${maven.yuicompressor.skip}" default-value="false"
105      */
106     private boolean skip;
107 
108     /**
109      * define if plugin must stop/fail on warnings.
110      *
111      * @parameter expression="${maven.yuicompressor.failOnWarning}" default-value="false"
112      */
113     protected boolean failOnWarning;
114     protected ErrorReporter4Mojo jsErrorReporter_;
115 
116     @SuppressWarnings("unchecked")
117     public void execute() throws MojoExecutionException, MojoFailureException {
118         try {
119             if (skip) {
120                 getLog().debug("run of yuicompressor-maven-plugin skipped");
121                 return;
122             }
123             if (failOnWarning) {
124                 jswarn = true;
125             }
126             jsErrorReporter_ = new ErrorReporter4Mojo(getLog(), jswarn);
127             beforeProcess();
128             processDir(sourceDirectory, outputDirectory, null, true);
129             if (!excludeResources) {
130               for (Resource resource : resources){
131                   File destRoot = outputDirectory;
132                   if (resource.getTargetPath() != null) {
133                       destRoot = new File(outputDirectory, resource.getTargetPath());
134                   }
135                   processDir(new File( resource.getDirectory() ), destRoot, resource.getExcludes(), true);
136               }
137             }
138             if (!excludeWarSourceDirectory) {
139             	processDir(warSourceDirectory, webappDirectory, null, false);
140             }
141             afterProcess();
142             getLog().info(String.format("nb warnings: %d, nb errors: %d", jsErrorReporter_.getWarningCnt(), jsErrorReporter_.getErrorCnt()));
143             if (failOnWarning && (jsErrorReporter_.getWarningCnt() > 0)) {
144                 throw new MojoFailureException("warnings on "+ this.getClass().getSimpleName() + "=> failure ! (see log)");
145             }
146         } catch (RuntimeException exc) {
147             throw exc;
148         } catch (MojoFailureException exc) {
149             throw exc;
150         } catch (MojoExecutionException exc) {
151             throw exc;
152         } catch (Exception exc) {
153             throw new MojoExecutionException("wrap: " + exc.getMessage(), exc);
154         }
155     }
156 
157     protected abstract String[] getDefaultIncludes() throws Exception;
158     protected abstract void beforeProcess() throws Exception;
159     protected abstract void afterProcess() throws Exception;
160 
161     /**
162      * Force to use defaultIncludes (ignore srcIncludes) to avoid processing resources/includes from other type than *.css or *.js
163      * @see https://github.com/davidB/yuicompressor-maven-plugin/issues/19 
164      */
165     protected void processDir(File srcRoot, File destRoot, List<String> srcExcludes, boolean destAsSource) throws Exception {
166         if ((srcRoot == null) || ( !srcRoot.exists() )) {
167             return;
168         }
169         if (destRoot == null) {
170             throw new MojoFailureException("destination directory for " + srcRoot + " is null");
171         }
172         DirectoryScanner scanner = new DirectoryScanner();
173         scanner.setBasedir(srcRoot);
174         
175         if (includes == null) {
176         	scanner.setIncludes(getDefaultIncludes());
177         } else {
178         	scanner.setIncludes(includes.toArray(new String[0]));
179         }
180         
181         if ( (srcExcludes != null) && !srcExcludes.isEmpty() ) {
182             scanner.setExcludes( srcExcludes.toArray( EMPTY_STRING_ARRAY ) );
183         }
184         if ((excludes != null) && !excludes.isEmpty()) {
185             scanner.setExcludes( excludes.toArray( EMPTY_STRING_ARRAY ) );
186         }
187         scanner.addDefaultExcludes();
188         scanner.scan();
189         for(String name :scanner.getIncludedFiles() ) {
190             SourceFile src = new SourceFile(srcRoot, destRoot, name, destAsSource);
191             jsErrorReporter_.setDefaultFileName("..." + src.toFile().getAbsolutePath().substring(project.getBasedir().getAbsolutePath().length()));
192             processFile(src);
193         }
194     }
195 
196     protected abstract void processFile(SourceFile src) throws Exception;
197 }