View Javadoc
1   /*
2    * Copyright (C) 2012 Jean-Christophe Gay (contact@jeanchristophegay.com)
3    *
4    * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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.codehaus.mojo.buildplan;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import org.apache.maven.execution.MavenSession;
21  import org.apache.maven.lifecycle.DefaultLifecycles;
22  import org.apache.maven.lifecycle.LifecycleExecutor;
23  import org.apache.maven.lifecycle.MavenExecutionPlan;
24  import org.apache.maven.plugin.AbstractMojo;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugin.MojoFailureException;
27  import org.apache.maven.plugins.annotations.Component;
28  import org.apache.maven.plugins.annotations.Parameter;
29  import org.apache.maven.project.MavenProject;
30  
31  public abstract class AbstractLifecycleMojo extends AbstractMojo {
32  
33      @Component(role = DefaultLifecycles.class)
34      protected DefaultLifecycles defaultLifecycles;
35  
36      @Parameter(defaultValue = "${session}", readonly = true)
37      private MavenSession session;
38  
39      @Parameter(defaultValue = "${project}", readonly = true)
40      private MavenProject project;
41  
42      @Component
43      private LifecycleExecutor lifecycleExecutor;
44  
45      /** Allow to specify which tasks will be used to calculate execution plan. */
46      @Parameter(property = "buildplan.tasks", defaultValue = "deploy")
47      private String[] tasks;
48  
49      /** Allow to specify an output file to bypass console output */
50      @Parameter(property = "buildplan.outputFile")
51      private File outputFile;
52  
53      /** Allow to specify appending to the output file */
54      @Parameter(property = "buildplan.appendOutput", defaultValue = "false")
55      private boolean appendOutput;
56  
57      /** Flag to easily skip all checks */
58      @Parameter(property = "buildplan.skip", defaultValue = "false")
59      private boolean skip;
60  
61      protected MavenExecutionPlan calculateExecutionPlan() throws MojoFailureException {
62          try {
63              return lifecycleExecutor.calculateExecutionPlan(session, tasks);
64          } catch (Exception e) {
65              throw new MojoFailureException(
66                      String.format("Cannot calculate Maven execution plan, caused by: %s", e.getMessage()), e);
67          }
68      }
69  
70      protected void handleOutput(final String output) {
71          String outputWithTitle = "Build Plan for " + project.getName() + ": " + output;
72          if (outputFile == null) {
73              getLog().info(outputWithTitle);
74          } else {
75              try {
76                  SynchronizedFileReporter.write(outputWithTitle, outputFile, appendOutput);
77                  getLog().info("Wrote build plan output to: " + outputFile);
78              } catch (IOException e) {
79                  getLog().warn("Unable to write to output file: " + outputFile, e);
80              }
81          }
82      }
83  
84      @Override
85      public final void execute() throws MojoExecutionException, MojoFailureException {
86          if (skip) {
87              getLog().info("Skipping build plan execution.");
88              return;
89          }
90          executeInternal();
91      }
92  
93      protected abstract void executeInternal() throws MojoFailureException;
94  }