View Javadoc

1   package org.apache.maven.plugin.reactor;
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.util.List;
23  
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.logging.Log;
26  import org.apache.maven.shared.invoker.CommandLineConfigurationException;
27  import org.apache.maven.shared.invoker.DefaultInvocationRequest;
28  import org.apache.maven.shared.invoker.InvocationRequest;
29  import org.apache.maven.shared.invoker.InvocationResult;
30  import org.apache.maven.shared.invoker.Invoker;
31  import org.apache.maven.shared.invoker.MavenCommandLineBuilder;
32  
33  /** Simplified wrapper for Maven invoker
34   * 
35   * @author <a href="mailto:dfabulich@apache.org">Dan Fabulich</a>
36   *
37   */
38  public class SimpleInvoker
39  {
40      /** Runs a "mvn --reactor" build with the specified includes
41       * 
42       * @param reactorIncludes the list of include patterns for --reactor
43       * @param goalList the list of goals (you can also pass other flags in here; they're just command-line args)
44       * @param invoker the Maven Invoker (let Maven provide to you as a component)
45       * @param printOnly if true, don't actually run anything, just log a message
46       * @param log logger
47       * @throws InvokerExecutionException if build fails for any reason
48       */
49      void runReactor( String[] reactorIncludes, List goalList, Invoker invoker, boolean printOnly, Log log )
50          throws InvokerExecutionException
51      {
52          InvocationRequest request = new DefaultInvocationRequest();
53          request.activateReactor( reactorIncludes, null/* excludes */);
54          request.setGoals( goalList );
55          request.setRecursive( false );
56          try
57          {
58              log.info( "Executing: " + new MavenCommandLineBuilder().build( request ) );
59          }
60          catch ( CommandLineConfigurationException e )
61          {
62              throw new InvokerExecutionException( "Failed to display command line", e );
63          }
64  
65          if ( !printOnly )
66          {
67              try
68              {
69                  InvocationResult result = invoker.execute( request );
70                  if ( result.getExecutionException() != null )
71                      throw result.getExecutionException();
72                  if ( result.getExitCode() != 0 )
73                      throw new InvokerExecutionException( "Exit code was " + result.getExitCode() );
74              }
75              catch ( Exception e )
76              {
77                  throw new InvokerExecutionException( "Maven build failed: " + e.getLocalizedMessage(), e );
78              }
79          }
80      }
81      
82      class InvokerExecutionException extends MojoExecutionException {
83  
84          private static final long serialVersionUID = 1L;
85  
86          public InvokerExecutionException( Object source, String shortMessage, String longMessage )
87          {
88              super( source, shortMessage, longMessage );
89          }
90  
91          public InvokerExecutionException( String message, Exception cause )
92          {
93              super( message, cause );
94          }
95  
96          public InvokerExecutionException( String message, Throwable cause )
97          {
98              super( message, cause );
99          }
100 
101         public InvokerExecutionException( String message )
102         {
103             super( message );
104         }
105         
106     }
107 }