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.List;
25  
26  import org.apache.maven.plugin.BuildPluginManager;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.MojoFailureException;
29  import org.apache.maven.plugin.descriptor.PluginDescriptor;
30  import org.apache.maven.plugins.annotations.Component;
31  import org.apache.maven.plugins.annotations.LifecyclePhase;
32  import org.apache.maven.plugins.annotations.Mojo;
33  import org.apache.maven.plugins.annotations.Parameter;
34  import org.apache.maven.shared.invoker.InvocationRequest;
35  import org.apache.maven.shared.invoker.InvocationResult;
36  import org.apache.maven.shared.invoker.Invoker;
37  import org.apache.maven.shared.invoker.MavenInvocationException;
38  
39  /**
40   * Invoker will execute an Maven instance by iterating through the given items.
41   * 
42   * @author Karl-Heinz Marbaise <a href="mailto:khmarbaise@apache.org">khmarbaise@apache.org</a>
43   */
44  @Mojo( name = "invoker", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true )
45  public class InvokerMojo
46      extends AbstractInvokerMojo
47  {
48  
49      /**
50       * The Maven BuildPluginManager component.
51       */
52      @Component
53      private BuildPluginManager pluginManager;
54  
55      @Parameter( defaultValue = "${plugin}", required = true, readonly = true )
56      private PluginDescriptor pluginDescriptor;
57  
58      @Parameter
59      private File workingDirectory;
60  
61      @Component
62      private Invoker invoker;
63  
64      /**
65       * Possibly to multithreading..
66       * 
67       * @throws MojoFailureException in case of failure.
68       */
69      // private int numberOfThreads;
70  
71      public void execute()
72          throws MojoExecutionException, MojoFailureException
73      {
74          if ( isSkip() )
75          {
76              getLog().info( "Skip by user request." );
77              return;
78          }
79  
80          if ( isNoneSet() )
81          {
82              throw new MojoExecutionException( "You have to use at least one. " + "Either items, "
83                  + "itemsWithProperties, content or folder element!" );
84          }
85  
86          if ( isMoreThanOneSet() )
87          {
88              throw new MojoExecutionException( "You can use only one element. "
89                  + "Either items, itemsWithProperties, content or folder element but not more than one of them." );
90          }
91  
92          File localRepository = new File( getMavenSession().getSettings().getLocalRepository() );
93  
94          invoker.setLocalRepositoryDirectory( localRepository );
95          // invoker.setOutputHandler(outputHandler);
96          // TODO: Check how it looks if we will use the invokerLogger?
97          // invoker.setLogger();
98  
99          // getLog().info("local repository: " + localRepository);
100         // // getLog().isDebugEnabled()
101         // getLog().info("Invoker:" + invoker);
102 
103         List<Exception> exceptions = new ArrayList<>();
104 
105         for ( ItemWithProperties item : getItemsConverted() )
106         {
107             try
108             {
109                 createLogOutput( item );
110                 InvocationResult result = mavenCall( item );
111 
112                 if ( result.getExitCode() == 0 )
113                 {
114                     getLog().info( "------ Maven call was Ok." );
115                     continue;
116                 }
117 
118                 getLog().error( "------ Maven call was NOT Ok. for iteration " + item.getName() + " ( return code: " + result.getExitCode() + " )" );
119                 if ( result.getExecutionException() != null )
120                 {
121                     getLog().error( result.getExecutionException().getMessage(),
122                                     result.getExecutionException().getCause() );
123                 }
124 
125                 String failureMessage =
126                     "Maven call failed with return code " + result.getExitCode() + " for iteration: " + item.getName();
127 
128                 if ( isFailAtEnd() )
129                 {
130                     exceptions.add( new RuntimeException( failureMessage ) );
131                 }
132                 else
133                 {
134                     throw new MojoFailureException( failureMessage );
135                 }
136 
137             }
138             catch ( MavenInvocationException e )
139             {
140                 // This will stop any iteration.
141                 getLog().error( "------ ***** Command line options are wrong:", e );
142                 throw new MojoExecutionException( "Command line options are wrong:", e );
143             }
144         }
145 
146         if ( !exceptions.isEmpty() )
147         {
148             for ( Exception exception : exceptions )
149             {
150                 getLog().error( exception );
151             }
152             throw new MojoExecutionException( "Failures during iterations." );
153         }
154     }
155 
156     private File getWorkingDirectoryAfterPlaceHolderIsReplaced( ItemWithProperties currentValue )
157     {
158         File baseDir = getWorkingDirectory();
159         if ( baseDir != null && baseDir.toString().contains( getPlaceHolder() ) )
160         {
161             baseDir = new File( baseDir.toString().replaceAll( getPlaceHolder(), currentValue.getName() ) );
162         }
163         return baseDir;
164     }
165 
166     private InvocationResult mavenCall( ItemWithProperties item )
167         throws MavenInvocationException
168     {
169         InvocationRequest request = createAndConfigureAnInvocationRequest( item );
170 
171         OutputConsumer output = new OutputConsumer( getLog() );
172         request.setOutputHandler( output );
173 
174         invoker.setWorkingDirectory( getWorkingDirectoryAfterPlaceHolderIsReplaced( item ) );
175 
176         return invoker.execute( request );
177     }
178 
179     private void createLogOutput( ItemWithProperties item )
180     {
181         StringBuilder sb = new StringBuilder( "------ " );
182         sb.append( " iterator-maven-plugin " );
183         sb.append( "( iteration: " );
184         sb.append( item.getName() );
185         sb.append( " )" );
186         getLog().info( sb.toString() );
187     }
188 
189     public void setThreads( String threads )
190     {
191         this.threads = threads;
192     }
193 
194     public File getWorkingDirectory()
195     {
196         return workingDirectory;
197     }
198 
199     public void setWorkingDirectory( File workingDirectory )
200     {
201         this.workingDirectory = workingDirectory;
202     }
203 
204 }