Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BaseMojo |
|
| 1.5263157894736843;1.526 |
1 | package org.kuali.maven.mojo; | |
2 | ||
3 | import org.apache.maven.execution.MavenSession; | |
4 | import org.apache.maven.plugin.AbstractMojo; | |
5 | import org.apache.maven.plugin.MojoExecutionException; | |
6 | import org.apache.maven.plugin.MojoFailureException; | |
7 | import org.apache.maven.project.MavenProject; | |
8 | import org.apache.maven.settings.Settings; | |
9 | import org.kuali.maven.mojo.MavenLogger; | |
10 | ||
11 | /** | |
12 | * Mojo essentials. Contains the "skip" logic that is the de facto standard for | |
13 | * maven plugins. Contains a number of maven related properties that are common | |
14 | * to most mojos. Also sets up logging so that if libraries called by a mojo | |
15 | * issue log statements to Jakarta Commons Logging or Log4j, those log messages | |
16 | * are shown in maven's output | |
17 | */ | |
18 | 0 | public abstract class BaseMojo extends AbstractMojo { |
19 | 0 | public static final String FS = System.getProperty("file.separator"); |
20 | public static final String SKIP_PACKAGING_TYPE = "pom"; | |
21 | ||
22 | /** | |
23 | * When true, redirect logging from Log4j and Jakarta Commons Logging to the | |
24 | * Maven logging system | |
25 | * | |
26 | * @parameter expression="${startMavenLogger}" default-value="true" | |
27 | */ | |
28 | private boolean startMavenLogger; | |
29 | ||
30 | /** | |
31 | * When <code>true</code>, skip the execution of this mojo | |
32 | * | |
33 | * @parameter default-value="false" | |
34 | */ | |
35 | private boolean skip; | |
36 | ||
37 | /** | |
38 | * Setting this parameter to <code>true</code> will force the execution of | |
39 | * this mojo, even if it would get skipped usually. | |
40 | * | |
41 | * @parameter expression="${forceMojoExecution}" default-value="false" | |
42 | * @required | |
43 | */ | |
44 | private boolean forceMojoExecution; | |
45 | ||
46 | /** | |
47 | * The encoding to use when reading/writing files. If not specified this | |
48 | * defaults to the platform specific encoding of whatever machine the build | |
49 | * is running on. | |
50 | * | |
51 | * @parameter expression="${encoding}" | |
52 | * default-value="${project.build.sourceEncoding}" | |
53 | */ | |
54 | private String encoding; | |
55 | ||
56 | /** | |
57 | * The Maven project this plugin runs in. | |
58 | * | |
59 | * @parameter expression="${project}" | |
60 | * @required | |
61 | * @readonly | |
62 | */ | |
63 | private MavenProject project; | |
64 | ||
65 | /** | |
66 | * @parameter expression="${settings}" | |
67 | * @required | |
68 | * @since 1.0 | |
69 | * @readonly | |
70 | */ | |
71 | private Settings settings; | |
72 | ||
73 | /** | |
74 | * @parameter default-value="${session}" | |
75 | * @required | |
76 | * @readonly | |
77 | */ | |
78 | private MavenSession mavenSession; | |
79 | ||
80 | protected void beforeExecution() throws MojoExecutionException, MojoFailureException { | |
81 | 0 | } |
82 | ||
83 | protected void afterExecution() throws MojoExecutionException, MojoFailureException { | |
84 | 0 | } |
85 | ||
86 | @Override | |
87 | public void execute() throws MojoExecutionException, MojoFailureException { | |
88 | 0 | beforeExecution(); |
89 | 0 | if (isStartMavenLogger()) { |
90 | 0 | MavenLogger.startPluginLog(this); |
91 | } | |
92 | 0 | if (skipMojo()) { |
93 | 0 | return; |
94 | } | |
95 | 0 | executeMojo(); |
96 | 0 | if (isStartMavenLogger()) { |
97 | 0 | MavenLogger.endPluginLog(this); |
98 | } | |
99 | 0 | afterExecution(); |
100 | 0 | } |
101 | ||
102 | protected abstract void executeMojo() throws MojoExecutionException, MojoFailureException; | |
103 | ||
104 | /** | |
105 | * <p> | |
106 | * Determine if the mojo execution should get skipped. | |
107 | * </p> | |
108 | * This is the case if: | |
109 | * <ul> | |
110 | * <li>{@link #skip} is <code>true</code></li> | |
111 | * <li>if the mojo gets executed on a project with packaging type 'pom' and | |
112 | * {@link #forceMojoExecution} is <code>false</code></li> | |
113 | * </ul> | |
114 | * | |
115 | * @return <code>true</code> if the mojo execution should be skipped. | |
116 | */ | |
117 | protected boolean skipMojo() { | |
118 | 0 | if (skip) { |
119 | 0 | getLog().info("Skipping execution"); |
120 | 0 | return true; |
121 | } | |
122 | ||
123 | 0 | if (!forceMojoExecution && project != null && SKIP_PACKAGING_TYPE.equals(project.getPackaging())) { |
124 | 0 | getLog().info("Skipping execution for project with packaging type '" + SKIP_PACKAGING_TYPE + "'"); |
125 | 0 | return true; |
126 | } | |
127 | ||
128 | 0 | return false; |
129 | } | |
130 | ||
131 | /** | |
132 | * Returns the maven project. | |
133 | * | |
134 | * @return The maven project where this plugin runs in. | |
135 | */ | |
136 | public MavenProject getProject() { | |
137 | 0 | return project; |
138 | } | |
139 | ||
140 | public String getEncoding() { | |
141 | 0 | return encoding; |
142 | } | |
143 | ||
144 | public void setEncoding(String encoding) { | |
145 | 0 | this.encoding = encoding; |
146 | 0 | } |
147 | ||
148 | public boolean isSkip() { | |
149 | 0 | return skip; |
150 | } | |
151 | ||
152 | public void setSkip(boolean skip) { | |
153 | 0 | this.skip = skip; |
154 | 0 | } |
155 | ||
156 | public boolean isForceMojoExecution() { | |
157 | 0 | return forceMojoExecution; |
158 | } | |
159 | ||
160 | public void setForceMojoExecution(boolean forceMojoExecution) { | |
161 | 0 | this.forceMojoExecution = forceMojoExecution; |
162 | 0 | } |
163 | ||
164 | public Settings getSettings() { | |
165 | 0 | return settings; |
166 | } | |
167 | ||
168 | public void setSettings(Settings settings) { | |
169 | 0 | this.settings = settings; |
170 | 0 | } |
171 | ||
172 | public MavenSession getMavenSession() { | |
173 | 0 | return mavenSession; |
174 | } | |
175 | ||
176 | public void setMavenSession(MavenSession mavenSession) { | |
177 | 0 | this.mavenSession = mavenSession; |
178 | 0 | } |
179 | ||
180 | public void setProject(MavenProject project) { | |
181 | 0 | this.project = project; |
182 | 0 | } |
183 | ||
184 | public boolean isStartMavenLogger() { | |
185 | 0 | return startMavenLogger; |
186 | } | |
187 | ||
188 | public void setStartMavenLogger(boolean startMavenLogger) { | |
189 | 0 | this.startMavenLogger = startMavenLogger; |
190 | 0 | } |
191 | } |