|
25 | 25 | package org.jenkinsci.plugins.pipeline.maven.publishers; |
26 | 26 |
|
27 | 27 | import edu.umd.cs.findbugs.annotations.NonNull; |
| 28 | +import hudson.AbortException; |
28 | 29 | import hudson.Extension; |
29 | | -import hudson.FilePath; |
30 | | -import hudson.Launcher; |
31 | | -import hudson.model.Run; |
32 | | -import hudson.model.TaskListener; |
33 | | -import hudson.plugins.jacoco.JacocoPublisher; |
34 | 30 | import java.io.IOException; |
35 | | -import java.util.ArrayList; |
36 | | -import java.util.List; |
37 | | -import java.util.logging.Level; |
38 | | -import java.util.logging.Logger; |
39 | | -import java.util.stream.Collectors; |
40 | 31 | import org.jenkinsci.Symbol; |
41 | | -import org.jenkinsci.plugins.pipeline.maven.MavenArtifact; |
42 | 32 | import org.jenkinsci.plugins.pipeline.maven.MavenPublisher; |
43 | | -import org.jenkinsci.plugins.pipeline.maven.MavenSpyLogProcessor; |
44 | 33 | import org.jenkinsci.plugins.pipeline.maven.Messages; |
45 | | -import org.jenkinsci.plugins.pipeline.maven.util.XmlUtils; |
46 | 34 | import org.jenkinsci.plugins.workflow.steps.StepContext; |
47 | 35 | import org.kohsuke.stapler.DataBoundConstructor; |
48 | 36 | import org.w3c.dom.Element; |
49 | 37 |
|
50 | 38 | /** |
51 | 39 | * @author <a href="mailto:[email protected]">Cyrille Le Clerc</a> |
| 40 | + * |
| 41 | + * @deprecated since TODO |
52 | 42 | */ |
| 43 | +@Deprecated |
53 | 44 | public class JacocoReportPublisher extends MavenPublisher { |
54 | | - private static final Logger LOGGER = Logger.getLogger(JacocoReportPublisher.class.getName()); |
55 | | - |
56 | 45 | private static final long serialVersionUID = 1L; |
57 | 46 |
|
58 | 47 | @DataBoundConstructor |
@@ -123,184 +112,12 @@ public JacocoReportPublisher() {} |
123 | 112 | @Override |
124 | 113 | public void process(@NonNull StepContext context, @NonNull Element mavenSpyLogsElt) |
125 | 114 | throws IOException, InterruptedException { |
126 | | - |
127 | | - TaskListener listener = context.get(TaskListener.class); |
128 | | - FilePath workspace = context.get(FilePath.class); |
129 | | - Run run = context.get(Run.class); |
130 | | - Launcher launcher = context.get(Launcher.class); |
131 | | - |
132 | | - List<Element> jacocoPrepareAgentEvents = XmlUtils.getExecutionEventsByPlugin( |
133 | | - mavenSpyLogsElt, "org.jacoco", "jacoco-maven-plugin", "prepare-agent", "MojoSucceeded", "MojoFailed"); |
134 | | - List<Element> jacocoPrepareAgentIntegrationEvents = XmlUtils.getExecutionEventsByPlugin( |
135 | | - mavenSpyLogsElt, |
136 | | - "org.jacoco", |
137 | | - "jacoco-maven-plugin", |
138 | | - "prepare-agent-integration", |
139 | | - "MojoSucceeded", |
140 | | - "MojoFailed"); |
141 | | - jacocoPrepareAgentEvents.addAll(jacocoPrepareAgentIntegrationEvents); // add prepare-agent-integration goals |
142 | | - |
143 | | - if (jacocoPrepareAgentEvents.isEmpty()) { |
144 | | - LOGGER.log(Level.FINE, "No org.jacoco:jacoco-maven-plugin:prepare-agent[-integration] execution found"); |
145 | | - return; |
146 | | - } |
147 | | - |
148 | | - try { |
149 | | - Class.forName("hudson.plugins.jacoco.JacocoPublisher"); |
150 | | - } catch (ClassNotFoundException e) { |
151 | | - listener.getLogger().print("[withMaven] Jenkins "); |
152 | | - listener.hyperlink("https://wiki.jenkins.io/display/JENKINS/JaCoCo+Plugin", "JaCoCo Plugin"); |
153 | | - listener.getLogger() |
154 | | - .println( |
155 | | - " not found, don't display org.jacoco:jacoco-maven-plugin:prepare-agent[-integration] results in pipeline screen."); |
156 | | - return; |
157 | | - } |
158 | | - |
159 | | - List<JacocoReportDetails> jacocoReportDetails = new ArrayList<>(); |
160 | | - |
161 | | - for (Element jacocoPrepareAgentEvent : jacocoPrepareAgentEvents) { |
162 | | - |
163 | | - Element buildElement = XmlUtils.getUniqueChildElementOrNull(jacocoPrepareAgentEvent, "project", "build"); |
164 | | - if (buildElement == null) { |
165 | | - if (LOGGER.isLoggable(Level.FINE)) |
166 | | - LOGGER.log( |
167 | | - Level.FINE, |
168 | | - "Ignore execution event with missing 'build' child:" |
169 | | - + XmlUtils.toString(jacocoPrepareAgentEvent)); |
170 | | - continue; |
171 | | - } |
172 | | - |
173 | | - Element pluginElt = XmlUtils.getUniqueChildElement(jacocoPrepareAgentEvent, "plugin"); |
174 | | - Element destFileElt = XmlUtils.getUniqueChildElementOrNull(pluginElt, "destFile"); |
175 | | - Element projectElt = XmlUtils.getUniqueChildElement(jacocoPrepareAgentEvent, "project"); |
176 | | - MavenArtifact mavenArtifact = XmlUtils.newMavenArtifact(projectElt); |
177 | | - MavenSpyLogProcessor.PluginInvocation pluginInvocation = XmlUtils.newPluginInvocation(pluginElt); |
178 | | - |
179 | | - if (destFileElt == null) { |
180 | | - listener.getLogger() |
181 | | - .println("[withMaven] No <destFile> element found for <plugin> in " |
182 | | - + XmlUtils.toString(jacocoPrepareAgentEvent)); |
183 | | - continue; |
184 | | - } |
185 | | - String destFile = destFileElt.getTextContent().trim(); |
186 | | - if (destFile.equals("${jacoco.destFile}")) { |
187 | | - destFile = "${project.build.directory}/jacoco.exec"; |
188 | | - if ("prepare-agent-integration".equals(pluginInvocation.goal)) |
189 | | - destFile = "${project.build.directory}/jacoco-it.exec"; |
190 | | - String projectBuildDirectory = XmlUtils.getProjectBuildDirectory(projectElt); |
191 | | - if (projectBuildDirectory == null || projectBuildDirectory.isEmpty()) { |
192 | | - listener.getLogger() |
193 | | - .println("[withMaven] '${project.build.directory}' found for <project> in " |
194 | | - + XmlUtils.toString(jacocoPrepareAgentEvent)); |
195 | | - continue; |
196 | | - } |
197 | | - |
198 | | - destFile = destFile.replace("${project.build.directory}", projectBuildDirectory); |
199 | | - } else if (destFile.contains("${project.build.directory}")) { |
200 | | - String projectBuildDirectory = XmlUtils.getProjectBuildDirectory(projectElt); |
201 | | - if (projectBuildDirectory == null || projectBuildDirectory.isEmpty()) { |
202 | | - listener.getLogger() |
203 | | - .println("[withMaven] '${project.build.directory}' found for <project> in " |
204 | | - + XmlUtils.toString(jacocoPrepareAgentEvent)); |
205 | | - continue; |
206 | | - } |
207 | | - destFile = destFile.replace("${project.build.directory}", projectBuildDirectory); |
208 | | - |
209 | | - } else if (destFile.contains("${basedir}")) { |
210 | | - String baseDir = projectElt.getAttribute("baseDir"); |
211 | | - if (baseDir.isEmpty()) { |
212 | | - listener.getLogger() |
213 | | - .println("[withMaven] '${basedir}' found for <project> in " |
214 | | - + XmlUtils.toString(jacocoPrepareAgentEvent)); |
215 | | - continue; |
216 | | - } |
217 | | - destFile = destFile.replace("${basedir}", baseDir); |
218 | | - } |
219 | | - |
220 | | - destFile = XmlUtils.getPathInWorkspace(destFile, workspace); |
221 | | - |
222 | | - String sourceDirectory = buildElement.getAttribute("sourceDirectory"); |
223 | | - String classesDirectory = buildElement.getAttribute("directory") + "/classes"; |
224 | | - |
225 | | - String sourceDirectoryRelativePath = XmlUtils.getPathInWorkspace(sourceDirectory, workspace); |
226 | | - String classesDirectoryRelativePath = XmlUtils.getPathInWorkspace(classesDirectory, workspace); |
227 | | - |
228 | | - String excludes = null; |
229 | | - Element excludesElt = XmlUtils.getUniqueChildElementOrNull(pluginElt, "excludes"); |
230 | | - if (excludesElt != null) { |
231 | | - List<Element> excludeElements = XmlUtils.getChildrenElements(excludesElt, "exclude"); |
232 | | - excludes = excludeElements.stream() |
233 | | - .map(e -> e.getTextContent().trim()) |
234 | | - .collect(Collectors.joining(",")); |
235 | | - } |
236 | | - |
237 | | - listener.getLogger() |
238 | | - .println("[withMaven] jacocoPublisher - Archive JaCoCo analysis results for Maven artifact " |
239 | | - + mavenArtifact.toString() + " generated by " + pluginInvocation + ": execFile: " + destFile |
240 | | - + ", sources: " + sourceDirectoryRelativePath + ", classes: " |
241 | | - + classesDirectoryRelativePath + ", excludes: " |
242 | | - + excludes); |
243 | | - jacocoReportDetails.add(new JacocoReportDetails( |
244 | | - destFile, |
245 | | - sourceDirectoryRelativePath, |
246 | | - classesDirectoryRelativePath, |
247 | | - excludes, |
248 | | - mavenArtifact.toString() + " " + pluginInvocation)); |
249 | | - } |
250 | | - |
251 | | - JacocoPublisher jacocoPublisher = new JacocoPublisher(); |
252 | | - |
253 | | - String aggregatedDestFile = |
254 | | - jacocoReportDetails.stream().map(details -> details.execFile).collect(Collectors.joining(",")); |
255 | | - String aggregatedSourceDirectory = jacocoReportDetails.stream() |
256 | | - .map(details -> details.sourceDirectory) |
257 | | - .collect(Collectors.joining(",")); |
258 | | - String aggregatedClassesDirectory = jacocoReportDetails.stream() |
259 | | - .map(details -> details.classesDirectory) |
260 | | - .collect(Collectors.joining(",")); |
261 | | - String aggregatedExclusionPattern = jacocoReportDetails.stream() |
262 | | - .map(details -> details.excludes) |
263 | | - .filter(e -> e != null && !e.isEmpty()) |
264 | | - .distinct() |
265 | | - .collect(Collectors.joining(",")); |
266 | | - |
267 | | - jacocoPublisher.setExecPattern(aggregatedDestFile); |
268 | | - jacocoPublisher.setSourcePattern(aggregatedSourceDirectory); |
269 | | - jacocoPublisher.setClassPattern(aggregatedClassesDirectory); |
270 | | - jacocoPublisher.setExclusionPattern(aggregatedExclusionPattern); |
271 | | - |
272 | | - try { |
273 | | - jacocoPublisher.perform(run, workspace, launcher, listener); |
274 | | - } catch (Exception e) { |
275 | | - listener.error("[withMaven] jacocoPublisher - exception archiving JaCoCo results for " + jacocoReportDetails |
276 | | - + ": " + e); |
277 | | - LOGGER.log(Level.WARNING, "Exception processing JaCoCo results", e); |
278 | | - throw new MavenPipelinePublisherException( |
279 | | - "jacocoPublisher", "archiving JaCoCo results for " + jacocoReportDetails, e); |
280 | | - } |
281 | | - } |
282 | | - |
283 | | - public static class JacocoReportDetails { |
284 | | - final String execFile, sourceDirectory, classesDirectory, excludes, description; |
285 | | - |
286 | | - public JacocoReportDetails( |
287 | | - String execFile, String sourceDirectory, String classesDirectory, String excludes, String description) { |
288 | | - this.execFile = execFile; |
289 | | - this.sourceDirectory = sourceDirectory; |
290 | | - this.classesDirectory = classesDirectory; |
291 | | - this.excludes = excludes; |
292 | | - this.description = description; |
293 | | - } |
294 | | - |
295 | | - @Override |
296 | | - public String toString() { |
297 | | - return "JacocoReportDetails{" + "execFile='" |
298 | | - + execFile + '\'' + ", sourceDirectory='" |
299 | | - + sourceDirectory + '\'' + ", classesDirectory='" |
300 | | - + classesDirectory + '\'' + ", excludes='" |
301 | | - + excludes + '\'' + ", description='" |
302 | | - + description + '\'' + '}'; |
303 | | - } |
| 115 | + throw new AbortException( |
| 116 | + """ |
| 117 | + The jacocoPublisher is deprecated as is the Jacoco plugin and you should not use it. |
| 118 | + Alternatively, you should rely on Coverage plugin and see the configuration required at https://plugins.jenkins.io/coverage/#plugin-content-pipeline-example. |
| 119 | + Or, if continuing to use the JaCoCo plugin for now, run it explicitly rather than expecting withMaven to run it implicitly. |
| 120 | + """); |
304 | 121 | } |
305 | 122 |
|
306 | 123 | @Symbol("jacocoPublisher") |
|
0 commit comments