View Javadoc

1   /**
2    * Copyright 2004-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.maven.common;
17  
18  import java.io.File;
19  import java.util.ArrayList;
20  import java.util.Hashtable;
21  import java.util.List;
22  import java.util.Properties;
23  import java.util.Set;
24  
25  import org.apache.tools.ant.BuildException;
26  import org.apache.tools.ant.Task;
27  
28  /**
29   * Invoke mvn from Ant
30   *
31   */
32  public class MvnTask extends Task implements MvnContext {
33      private static final String FS = File.separator;
34  
35      MvnExecutor executor = new MvnExecutor();
36  
37      @Override
38      public Properties getProjectProperties() {
39          Hashtable<?, ?> hashTable = getProject().getProperties();
40          Properties properties = new Properties();
41          Set<?> keys = hashTable.keySet();
42          for (Object key : keys) {
43              Object value = hashTable.get(key);
44              // Hashtable can't have null key's or value's so we can safely call toString() without checking
45              properties.setProperty(key.toString(), value.toString());
46          }
47          return properties;
48      }
49  
50      /**
51       * The working directory where the task makes a local copy of the pom (if a pom is supplied)
52       */
53      private File workingDir;
54  
55      /**
56       * The base directory for the new mvn invocation. By default the same as workingDir
57       */
58      private File basedir;
59  
60      /**
61       * The Maven executable. By default, the executable to use is located via the ${maven.home} system property. This
62       * causes the new mvn invocation to mirror what is currently executing (same version, etc). You can override this
63       * behavior by supplying your own executable
64       */
65      private String executable;
66  
67      /**
68       * The pom to supply to the new mvn invocation. This can be a file or any url Spring resource loading can understand
69       */
70      private String pom;
71  
72      /**
73       * POM's to invoke. If supplied, a new Maven invocation is generated using the same args for each pom
74       */
75      private List<String> poms;
76  
77      /**
78       * If true, the pom will be filtered using properties from the current project before being invoked
79       */
80      private boolean filterPom = false;
81  
82      /**
83       * Arguments to supply to the new mvn invocation eg "clean install"
84       */
85      private List<String> args = new ArrayList<String>();
86  
87      /**
88       * List of properties from the current project to propagate to the new mvn invocation
89       */
90      private List<String> properties = new ArrayList<String>();
91  
92      /**
93       * If supplied, only these properties are used when filtering the pom
94       */
95      private List<String> filterProperties = new ArrayList<String>();
96  
97      /**
98       * If true, the current environment is passed to the new mvn invocation
99       */
100     private boolean addEnvironment = false;
101 
102     /**
103      * If true, the environment variable MAVEN_OPTS (if set) is passed to the new mvn invocation
104      */
105     private boolean addMavenOpts = true;
106 
107     /**
108      * If true, any temp pom copied to <code>basedir</code> will be deleted when the task execution is complete
109      */
110     private boolean deleteTempPom = true;
111 
112     /**
113      * If true, logging output is reduced to a minimum
114      */
115     private boolean quiet = true;
116 
117     /**
118      * If true, no logging output is generated.
119      */
120     private boolean silent = true;
121 
122     /**
123      * If true, the Ant build will fail if the new mvn invocation returns a non-zero exit value, otherwise the Ant build
124      * will continue
125      */
126     private boolean failOnError = true;
127 
128     public void addConfiguredArg(Arg arg) {
129         args.add(arg.getValue());
130     }
131 
132     public void addConfiguredProperty(Property property) {
133         properties.add(property.getKey());
134     }
135 
136     protected void configure() {
137         if (workingDir == null) {
138             String filename = getProject().getBaseDir().getAbsolutePath() + FS + "target" + FS + "mvn";
139             workingDir = new File(filename);
140         }
141         if (basedir == null) {
142             basedir = workingDir;
143         }
144     }
145 
146     @Override
147     public void execute() throws BuildException {
148         try {
149             configure();
150             executor.execute(this);
151         } catch (Exception e) {
152             throw new BuildException("Error invoking mvn", e);
153         }
154     }
155 
156     /*
157      * (non-Javadoc)
158      *
159      * @see org.kuali.maven.common.MvnContext2#getWorkingDir()
160      */
161     @Override
162     public File getWorkingDir() {
163         return workingDir;
164     }
165 
166     /*
167      * (non-Javadoc)
168      *
169      * @see org.kuali.maven.common.MvnContext2#setWorkingDir(java.io.File)
170      */
171     @Override
172     public void setWorkingDir(File workingDir) {
173         this.workingDir = workingDir;
174     }
175 
176     /*
177      * (non-Javadoc)
178      *
179      * @see org.kuali.maven.common.MvnContext2#getBasedir()
180      */
181     @Override
182     public File getBasedir() {
183         return basedir;
184     }
185 
186     /*
187      * (non-Javadoc)
188      *
189      * @see org.kuali.maven.common.MvnContext2#setBasedir(java.io.File)
190      */
191     @Override
192     public void setBasedir(File basedir) {
193         this.basedir = basedir;
194     }
195 
196     /*
197      * (non-Javadoc)
198      *
199      * @see org.kuali.maven.common.MvnContext2#getExecutable()
200      */
201     @Override
202     public String getExecutable() {
203         return executable;
204     }
205 
206     /*
207      * (non-Javadoc)
208      *
209      * @see org.kuali.maven.common.MvnContext2#setExecutable(java.lang.String)
210      */
211     @Override
212     public void setExecutable(String executable) {
213         this.executable = executable;
214     }
215 
216     /*
217      * (non-Javadoc)
218      *
219      * @see org.kuali.maven.common.MvnContext2#getPom()
220      */
221     @Override
222     public String getPom() {
223         return pom;
224     }
225 
226     /*
227      * (non-Javadoc)
228      *
229      * @see org.kuali.maven.common.MvnContext2#setPom(java.lang.String)
230      */
231     @Override
232     public void setPom(String pom) {
233         this.pom = pom;
234     }
235 
236     /*
237      * (non-Javadoc)
238      *
239      * @see org.kuali.maven.common.MvnContext2#isFilterPom()
240      */
241     @Override
242     public boolean isFilterPom() {
243         return filterPom;
244     }
245 
246     /*
247      * (non-Javadoc)
248      *
249      * @see org.kuali.maven.common.MvnContext2#setFilterPom(boolean)
250      */
251     @Override
252     public void setFilterPom(boolean filterPom) {
253         this.filterPom = filterPom;
254     }
255 
256     /*
257      * (non-Javadoc)
258      *
259      * @see org.kuali.maven.common.MvnContext2#getArgs()
260      */
261     @Override
262     public List<String> getArgs() {
263         return args;
264     }
265 
266     /*
267      * (non-Javadoc)
268      *
269      * @see org.kuali.maven.common.MvnContext2#setArgs(java.util.List)
270      */
271     @Override
272     public void setArgs(List<String> args) {
273         this.args = args;
274     }
275 
276     /*
277      * (non-Javadoc)
278      *
279      * @see org.kuali.maven.common.MvnContext2#getProperties()
280      */
281     @Override
282     public List<String> getProperties() {
283         return properties;
284     }
285 
286     /*
287      * (non-Javadoc)
288      *
289      * @see org.kuali.maven.common.MvnContext2#setProperties(java.util.List)
290      */
291     @Override
292     public void setProperties(List<String> properties) {
293         this.properties = properties;
294     }
295 
296     /*
297      * (non-Javadoc)
298      *
299      * @see org.kuali.maven.common.MvnContext2#isAddEnvironment()
300      */
301     @Override
302     public boolean isAddEnvironment() {
303         return addEnvironment;
304     }
305 
306     /*
307      * (non-Javadoc)
308      *
309      * @see org.kuali.maven.common.MvnContext2#setAddEnvironment(boolean)
310      */
311     @Override
312     public void setAddEnvironment(boolean addEnvironment) {
313         this.addEnvironment = addEnvironment;
314     }
315 
316     /*
317      * (non-Javadoc)
318      *
319      * @see org.kuali.maven.common.MvnContext2#isAddMavenOpts()
320      */
321     @Override
322     public boolean isAddMavenOpts() {
323         return addMavenOpts;
324     }
325 
326     /*
327      * (non-Javadoc)
328      *
329      * @see org.kuali.maven.common.MvnContext2#setAddMavenOpts(boolean)
330      */
331     @Override
332     public void setAddMavenOpts(boolean addMavenOpts) {
333         this.addMavenOpts = addMavenOpts;
334     }
335 
336     /*
337      * (non-Javadoc)
338      *
339      * @see org.kuali.maven.common.MvnContext2#isFailOnError()
340      */
341     @Override
342     public boolean isFailOnError() {
343         return failOnError;
344     }
345 
346     /*
347      * (non-Javadoc)
348      *
349      * @see org.kuali.maven.common.MvnContext2#setFailOnError(boolean)
350      */
351     @Override
352     public void setFailOnError(boolean failOnError) {
353         this.failOnError = failOnError;
354     }
355 
356     @Override
357     public boolean isDeleteTempPom() {
358         return deleteTempPom;
359     }
360 
361     public void setDeleteTempPom(boolean deleteTempPom) {
362         this.deleteTempPom = deleteTempPom;
363     }
364 
365     @Override
366     public List<String> getPoms() {
367         return poms;
368     }
369 
370     @Override
371     public void setPoms(List<String> poms) {
372         this.poms = poms;
373     }
374 
375     @Override
376     public boolean isQuiet() {
377         return quiet;
378     }
379 
380     @Override
381     public void setQuiet(boolean quiet) {
382         this.quiet = quiet;
383     }
384 
385     @Override
386     public boolean isSilent() {
387         return silent;
388     }
389 
390     @Override
391     public void setSilent(boolean silent) {
392         this.silent = silent;
393     }
394 
395     @Override
396     public List<String> getFilterProperties() {
397         return filterProperties;
398     }
399 
400     @Override
401     public void setFilterProperties(List<String> filterProperties) {
402         this.filterProperties = filterProperties;
403     }
404 
405 }