View Javadoc
1   package com.soebes.maven.plugins.iterator;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Properties;
28  
29  import org.apache.maven.plugins.annotations.Parameter;
30  import org.apache.maven.project.MavenProject;
31  import org.apache.maven.shared.invoker.DefaultInvocationRequest;
32  import org.apache.maven.shared.invoker.InvocationRequest;
33  
34  /**
35   * @author Karl-Heinz Marbaise <a href="mailto:khmarbaise@apache.org">khmarbaise@apache.org</a>
36   */
37  public abstract class AbstractInvokerMojo
38      extends AbstractIteratorMojo
39  {
40  
41      /**
42       * The list of goals which will be called during each invocation of Maven. TODO: Change the name, cause we are
43       * calling life cycle phases instead of goal but we can if we like things like site:site ?
44       */
45      @Parameter
46      private List<String> goals = Collections.singletonList( "clean" );
47  
48      /**
49       * This will set <code>--batch-mode</code>
50       */
51      @Parameter( defaultValue = "false" )
52      private boolean interactive;
53  
54      /**
55       * This will <code>--also-make</code>
56       */
57      @Parameter( defaultValue = "false" )
58      private boolean alsoMake;
59  
60      /**
61       * This will activate <code>--also-make-dependents</code>
62       */
63      @Parameter( defaultValue = "false" )
64      private boolean alsoMakeDependents;
65  
66      /**
67       * Sets the path to the base directory of the POM for the Maven invocation. If {@link #getPomFile()} does not return
68       * <code>null</code>, this setting only affects the working directory for the Maven invocation. Here you can use the
69       * placeholder like: EXAMPLE
70       */
71      @Parameter
72      private File baseDirectory;
73  
74      /**
75       * <code>-Ddebug=true</code>
76       */
77      @Parameter( defaultValue = "false" )
78      private boolean debug;
79  
80      /**
81       * The valid values are {@link InvocationRequest#REACTOR_FAIL_AT_END}, {@link InvocationRequest#REACTOR_FAIL_FAST},
82       * {@link InvocationRequest#REACTOR_FAIL_NEVER}.
83       * 
84       * @see {@link InvocationRequest#REACTOR_FAIL_AT_END}, {@link InvocationRequest#REACTOR_FAIL_FAST},
85       *      {@link InvocationRequest#REACTOR_FAIL_NEVER}
86       * @FIXME: Check if we need to create a setter which checks the valid values
87       */
88      @Parameter( defaultValue = InvocationRequest.REACTOR_FAIL_FAST )
89      private String failureBehaviour;
90  
91      /**
92       * Sets the checksum mode of the Maven invocation. Equivalent of {@code -c} or {@code --lax-checksums}, {@code -C}
93       * or {@code --strict-checksums}
94       * 
95       * @param globalChecksumPolicy The checksum mode, must be one of {@link InvocationRequest#CHECKSUM_POLICY_WARN} and
96       *            {@link InvocationRequest#CHECKSUM_POLICY_FAIL}.
97       */
98      @Parameter( defaultValue = InvocationRequest.CHECKSUM_POLICY_WARN )
99      private String globalChecksumPolicy;
100 
101     /**
102      * The path to the global settings for the Maven invocation or <code>null</code> to load the global settings from
103      * the default location.
104      */
105     @Parameter
106     private File globalSettingsFile;
107 
108     /**
109      * If <code>null</code> the default location from <code>settings.xml</code> will be used.
110      */
111     @Parameter
112     private File localRepositoryDirectory;
113 
114     /**
115      * If <code>null</code>.
116      */
117     @Parameter
118     private String mavenOpts;
119 
120     /**
121      * If true <code>--no-plugin-updates</code> will be used.
122      */
123     @Parameter
124     private boolean nonPluginUpdates;
125 
126     /**
127      * <code>true</code> Maven will be executed in off-line mode ( <code>--offline</code>).
128      */
129     @Parameter
130     private boolean offline;
131 
132     /**
133      * The list of profiles.
134      */
135     @Parameter
136     private List<String> profiles;
137 
138     /**
139      * Sets the reactor project list. Equivalent of {@code -pl} or {@code --projects} projects the reactor project list
140      */
141     @Parameter
142     private List<String> projects;
143 
144     /**
145      * Sets the system properties for the Maven invocation, may be <code>null</code> if not set.
146      */
147     @Parameter
148     private Properties properties;
149 
150     /**
151      * Sets the recursion behavior of a reactor invocation. <em>Inverse</em> equivalent of {@code -N} and
152      * {@code --non-recursive}
153      */
154     @Parameter( defaultValue = "false" )
155     private boolean recursive;
156 
157     /**
158      * Specify the reactor project to resume from.
159      */
160     @Parameter
161     private String resumeFrom;
162 
163     /**
164      * Indicates whether the environment variables of the current process should be propagated to the Maven invocation.
165      * By default, the current environment variables are inherited by the new Maven invocation.
166      */
167     @Parameter( defaultValue = "true" )
168     private boolean shellEnvironmentInherited;
169 
170     /**
171      * Gets the exception output mode of the Maven invocation. By default, Maven will not print stack traces of build
172      * exceptions.
173      */
174     @Parameter( defaultValue = "false" )
175     private boolean showErrors;
176 
177     /**
178      * The show version behaviour {@code -V} option.
179      */
180     @Parameter( defaultValue = "false" )
181     private boolean showVersion;
182 
183     /**
184      * {@code -T} option of Maven.
185      */
186     @Parameter
187     protected String threads;
188 
189     /**
190      * Set the path to the custom toolchains file
191      */
192     @Parameter
193     private File toolchains;
194 
195     /**
196      * Indicates whether Maven should enforce an update check for plugins and snapshots. By default, no update check is
197      * performed {@code --update-snapshots}.
198      */
199     @Parameter( defaultValue = "false" )
200     private boolean updateSnapshots;
201 
202     /**
203      * Sets the path to the user settings for the Maven invocation.
204      */
205     @Parameter
206     private File userSettings;
207         
208     private File getBaseDirectoryAfterPlaceHolderIsReplaced( String currentValue )
209     {
210         File baseDir = getBaseDirectory();
211         if ( baseDir != null && baseDir.toString().contains( getPlaceHolder() ) )
212         {
213             baseDir = new File( baseDir.toString().replaceAll( getPlaceHolder(), currentValue ) );
214         }
215         return baseDir;
216     }
217 
218     private List<String> replacePlaceholderInElements( String currentValue, List<String> goals )
219     {
220         List<String> result = new ArrayList<String>();
221         for ( String string : goals )
222         {
223             if ( string.contains( getPlaceHolder() ) )
224             {
225                 result.add( string.replaceAll( getPlaceHolder(), currentValue ) );
226             }
227             else
228             {
229                 result.add( string );
230             }
231         }
232         return result;
233     }
234 
235     private List<String> getGoalsAfterPlaceHolderIsReplaced( String currentValue )
236     {
237         List<String> goals = getGoals();
238         if ( goals == null )
239         {
240             return null;
241         }
242 
243         List<String> result = replacePlaceholderInElements( currentValue, goals );
244         return result;
245     }
246 
247     private List<String> getProfilesAfterPlaceHolderIsReplaced( String currentValue )
248     {
249         List<String> profiles = getProfiles();
250         if ( profiles == null )
251         {
252             return null;
253         }
254 
255         List<String> result = replacePlaceholderInElements( currentValue, profiles );
256         return result;
257     }
258 
259     private List<String> getProjectsAfterPlaceHolderIsReplaced( String currentValue )
260     {
261         List<String> projects = getProjects();
262         if ( projects == null )
263         {
264             return null;
265         }
266 
267         List<String> result = replacePlaceholderInElements( currentValue, projects );
268         return result;
269     }
270 
271     protected InvocationRequest createAndConfigureAnInvocationRequest( ItemWithProperties currentValue )
272     {
273         InvocationRequest request = new DefaultInvocationRequest();
274 
275         request.setAlsoMake( isAlsoMake() );
276         request.setAlsoMakeDependents( isAlsoMakeDependents() );
277         request.setDebug( isDebug() );
278         request.setFailureBehavior( getFailureBehaviour() );
279         request.setGlobalChecksumPolicy( getGlobalChecksumPolicy() );
280         request.setGlobalSettingsFile( getGlobalSettingsFile() );
281         request.setInteractive( isInteractive() );
282 
283         request.setLocalRepositoryDirectory( getLocalRepositoryDirectory() );
284         request.setMavenOpts( getMavenOpts() );
285         request.setNonPluginUpdates( isNonPluginUpdates() );
286         request.setOffline( isOffline() );
287 
288 //        request.setProperties( properties )
289 //        ;
290         // @TODO: Think about it.
291         // request.setPomFile(pomFile);
292         // @TODO: Think about it.
293         // request.setPomFileName(pomFilename);
294 
295         // The following parameter do make sense to use a placeholder
296         // base directory
297         // cd @item@
298         // mvn clean package
299         request.setBaseDirectory( getBaseDirectoryAfterPlaceHolderIsReplaced( currentValue.getName() ) );
300         // goals:
301         // mvn plugin-name:@item@
302         //
303         request.setGoals( getGoalsAfterPlaceHolderIsReplaced( currentValue.getName() ) );
304         // Profiles:
305         // mvn -Pxyz-@item@ clean package
306         // mvn -P@item@
307         request.setProfiles( getProfilesAfterPlaceHolderIsReplaced( currentValue.getName() ) );
308         // Projects:
309         // mvn -pl xyz-@item@ clean package
310         request.setProjects( getProjectsAfterPlaceHolderIsReplaced( currentValue.getName() ) );
311 
312         Properties props = getMergedProperties(currentValue );
313         request.setProperties( props );
314 
315         request.setRecursive( isRecursive() );
316         request.setResumeFrom( getResumeFrom() );
317         request.setShellEnvironmentInherited( isShellEnvironmentInherited() );
318         request.setShowErrors( isShowErrors() );
319         request.setShowVersion( isShowVersion() );
320         request.setThreads( getThreads() );
321         request.setToolchainsFile( getToolchains() );
322         request.setUpdateSnapshots( isUpdateSnapshots() );
323         request.setUserSettingsFile( getUserSettings() );
324         
325         return request;
326     }
327 
328     private Properties getMergedProperties( ItemWithProperties item ) 
329     {
330         Properties props = new Properties();
331         if ( getMavenProject().getProperties() != null ) 
332         {
333             props.putAll( getMavenProject().getProperties() );
334         }
335         
336         if ( getProperties() != null ) 
337         {
338             props.putAll( getProperties() );
339         }
340         
341         if ( item.getProperties() != null ) 
342         {
343             props.putAll( item.getProperties() );
344         }
345         
346         for ( Map.Entry<Object, Object> property : props.entrySet() ) 
347         {
348             String key = (String) property.getKey();
349             String systemPropertyValue = System.getProperty( key );
350 
351             if ( systemPropertyValue != null ) 
352             {
353                 props.put( key, systemPropertyValue );
354             }
355         }
356         
357         return props;
358     }
359 
360     public List<String> getGoals()
361     {
362         return goals;
363     }
364 
365     public boolean isInteractive()
366     {
367         return interactive;
368     }
369 
370     public void setInteractive( boolean interactive )
371     {
372         this.interactive = interactive;
373     }
374 
375     public boolean isAlsoMake()
376     {
377         return alsoMake;
378     }
379 
380     public void setAlsoMake( boolean alsoMake )
381     {
382         this.alsoMake = alsoMake;
383     }
384 
385     public boolean isAlsoMakeDependents()
386     {
387         return alsoMakeDependents;
388     }
389 
390     public void setAlsoMakeDependents( boolean alsoMakeDependents )
391     {
392         this.alsoMakeDependents = alsoMakeDependents;
393     }
394 
395     public File getBaseDirectory()
396     {
397         return baseDirectory;
398     }
399 
400     public void setBaseDirectory( File baseDirectory )
401     {
402         this.baseDirectory = baseDirectory;
403     }
404 
405     public boolean isDebug()
406     {
407         return debug;
408     }
409 
410     public void setDebug( boolean debug )
411     {
412         this.debug = debug;
413     }
414 
415     public String getFailureBehaviour()
416     {
417         return failureBehaviour;
418     }
419 
420     public void setFailureBehaviour( String failureBehaviour )
421     {
422         this.failureBehaviour = failureBehaviour;
423     }
424 
425     public String getGlobalChecksumPolicy()
426     {
427         return globalChecksumPolicy;
428     }
429 
430     public void setGlobalChecksumPolicy( String globalChecksumPolicy )
431     {
432         this.globalChecksumPolicy = globalChecksumPolicy;
433     }
434 
435     public File getGlobalSettingsFile()
436     {
437         return globalSettingsFile;
438     }
439 
440     public void setGlobalSettingsFile( File globalSettingsFile )
441     {
442         this.globalSettingsFile = globalSettingsFile;
443     }
444 
445     public File getLocalRepositoryDirectory()
446     {
447         return localRepositoryDirectory;
448     }
449 
450     public void setLocalRepositoryDirectory( File localRepositoryDirectory )
451     {
452         this.localRepositoryDirectory = localRepositoryDirectory;
453     }
454 
455     public String getMavenOpts()
456     {
457         return mavenOpts;
458     }
459 
460     public void setMavenOpts( String mavenOpts )
461     {
462         this.mavenOpts = mavenOpts;
463     }
464 
465     public boolean isNonPluginUpdates()
466     {
467         return nonPluginUpdates;
468     }
469 
470     public void setNonPluginUpdates( boolean nonPluginUpdates )
471     {
472         this.nonPluginUpdates = nonPluginUpdates;
473     }
474 
475     public boolean isOffline()
476     {
477         return offline;
478     }
479 
480     public void setOffline( boolean offline )
481     {
482         this.offline = offline;
483     }
484 
485     public List<String> getProfiles()
486     {
487         return profiles;
488     }
489 
490     public void setProfiles( List<String> profiles )
491     {
492         this.profiles = profiles;
493     }
494 
495     public List<String> getProjects()
496     {
497         return projects;
498     }
499 
500     public void setProjects( List<String> projects )
501     {
502         this.projects = projects;
503     }
504 
505     public Properties getProperties()
506     {
507         return properties;
508     }
509 
510     public void setProperties( Properties properties )
511     {
512         this.properties = properties;
513     }
514 
515     public boolean isRecursive()
516     {
517         return recursive;
518     }
519 
520     public void setRecursive( boolean recursive )
521     {
522         this.recursive = recursive;
523     }
524 
525     public String getResumeFrom()
526     {
527         return resumeFrom;
528     }
529 
530     public void setResumeFrom( String resumeFrom )
531     {
532         this.resumeFrom = resumeFrom;
533     }
534 
535     public boolean isShellEnvironmentInherited()
536     {
537         return shellEnvironmentInherited;
538     }
539 
540     public void setShellEnvironmentInherited( boolean shellEnvironmentInherited )
541     {
542         this.shellEnvironmentInherited = shellEnvironmentInherited;
543     }
544 
545     public boolean isShowErrors()
546     {
547         return showErrors;
548     }
549 
550     public void setShowErrors( boolean showErrors )
551     {
552         this.showErrors = showErrors;
553     }
554 
555     public boolean isShowVersion()
556     {
557         return showVersion;
558     }
559 
560     public void setShowVersion( boolean showVersion )
561     {
562         this.showVersion = showVersion;
563     }
564 
565     public String getThreads()
566     {
567         return threads;
568     }
569 
570     public File getToolchains()
571     {
572         return toolchains;
573     }
574 
575     public void setToolchains( File toolchains )
576     {
577         this.toolchains = toolchains;
578     }
579 
580     public boolean isUpdateSnapshots()
581     {
582         return updateSnapshots;
583     }
584 
585     public void setUpdateSnapshots( boolean updateSnapshots )
586     {
587         this.updateSnapshots = updateSnapshots;
588     }
589 
590     public File getUserSettings()
591     {
592         return userSettings;
593     }
594 
595     public void setUserSettings( File userSettings )
596     {
597         this.userSettings = userSettings;
598     }
599 
600     public void setGoals( List<String> goals )
601     {
602         this.goals = goals;
603     }
604 
605 }