1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
46 @Parameter(property = "buildplan.tasks", defaultValue = "deploy")
47 private String[] tasks;
48
49
50 @Parameter(property = "buildplan.outputFile")
51 private File outputFile;
52
53
54 @Parameter(property = "buildplan.appendOutput", defaultValue = "false")
55 private boolean appendOutput;
56
57
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 }