2 * Copyright © 2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on a "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.
17 package org.openecomp.sdc.onboarding.pmd;
19 import static org.openecomp.sdc.onboarding.pmd.PMDHelperUtils.getStateFile;
20 import static org.openecomp.sdc.onboarding.pmd.PMDHelperUtils.readCurrentPMDState;
21 import static org.openecomp.sdc.onboarding.pmd.PMDHelperUtils.writeCurrentPMDState;
22 import static org.openecomp.sdc.onboarding.pmd.PMDHelperUtils.isReportEmpty;
25 import java.io.IOException;
26 import java.io.UncheckedIOException;
27 import java.nio.file.Files;
28 import java.util.HashMap;
29 import java.util.List;
31 import java.util.stream.Collectors;
32 import org.apache.maven.execution.MavenSession;
33 import org.apache.maven.plugin.AbstractMojo;
34 import org.apache.maven.plugin.MojoExecutionException;
35 import org.apache.maven.plugin.MojoFailureException;
36 import org.apache.maven.plugins.annotations.LifecyclePhase;
37 import org.apache.maven.plugins.annotations.Mojo;
38 import org.apache.maven.plugins.annotations.Parameter;
39 import org.apache.maven.plugins.annotations.ResolutionScope;
40 import org.apache.maven.project.MavenProject;
42 @Mojo(name = "post-verify-helper", threadSafe = true, defaultPhase = LifecyclePhase.VERIFY,
43 requiresDependencyResolution = ResolutionScope.NONE)
44 public class VerifyHelperMojo extends AbstractMojo {
46 private static final String SKIP_PMD = "skipPMD";
48 @Parameter(defaultValue = "${project}", readonly = true)
49 private MavenProject project;
50 @Parameter(defaultValue = "${project.artifact.groupId}:${project.artifact.artifactId}")
51 private String moduleCoordinates;
52 @Parameter(defaultValue = "${session}")
53 private MavenSession session;
55 private File pmdTargetLocation;
57 private File pmdReportFile;
59 private File pmdStateFile;
61 private String pmdCurrentStateFilePath;
63 private String excludePackaging;
65 private Boolean validatePMDReport = Boolean.FALSE;
67 private String persistingModuleCoordinates;
69 private File pmdFailureReportLocation;
71 private File compiledFilesList;
73 private File compiledTestFilesList;
75 private static File pmdCurrentStateFile;
77 public void execute() throws MojoExecutionException, MojoFailureException {
78 if (project.getPackaging().equals(excludePackaging)) {
81 if (moduleCoordinates.equals(persistingModuleCoordinates)) {
82 if (pmdStateFile.exists()) {
83 pmdStateFile.delete();
86 if (pmdCurrentStateFile == null) {
88 getStateFile(pmdCurrentStateFilePath.substring(0, pmdCurrentStateFilePath.indexOf('/')), project,
89 pmdCurrentStateFilePath);
90 pmdCurrentStateFile.getParentFile().mkdirs();
91 pmdReportFile.getParentFile().mkdirs();
93 if (PMDState.getHistoricState() != null && PMDState.getHistoricState().isEmpty()) {
94 getLog().error("PMD Check is skipped. problem while loading data.");
96 if (Boolean.FALSE.equals(Boolean.valueOf(project.getProperties().getProperty(SKIP_PMD))) && !isReportEmpty(pmdReportFile)) {
97 Map<String, List<Violation>> data = readCurrentPMDState(pmdCurrentStateFile);
98 Map<String, List<Violation>> cv = readCurrentModulePMDReport();
100 if (!PMDState.getHistoricState().isEmpty() && !PMDHelperUtils
101 .evaluateCodeQuality(PMDState.getHistoricState(), cv,
102 pmdFailureReportLocation, getLog())) {
103 if (validatePMDReport) {
104 throw new MojoFailureException(
105 "PMD Failures encountered. Build halted. For details refer " + pmdFailureReportLocation
109 "\u001B[31m\u001B[1m Code Quality concerns raised by Quality Management System. For details refer "
110 + pmdFailureReportLocation.getAbsolutePath()
111 + " and address them before committing this code in Version Control System. \u001B[0m");
114 Map<String, Object> checksumStore = HashMap.class.cast(data);
115 checksumStore.put(moduleCoordinates,
116 project.getProperties().getProperty("mainChecksum") + ":" + project.getProperties()
117 .getProperty("testChecksum"));
118 writeCurrentPMDState(pmdCurrentStateFile, data);
120 if (Boolean.FALSE.equals(Boolean.valueOf(project.getProperties().getProperty(SKIP_PMD)))) {
121 if (isReportEmpty(pmdReportFile)){
122 HashMap data = HashMap.class.cast(readCurrentPMDState(pmdCurrentStateFile));
123 data.put(moduleCoordinates,
124 project.getProperties().getProperty("mainChecksum") + ":" + project.getProperties()
125 .getProperty("testChecksum"));
126 writeCurrentPMDState(pmdCurrentStateFile, data);
128 pmdReportFile.delete();
130 if (pmdTargetLocation.exists()) {
131 pmdTargetLocation.delete();
136 private Map<String, List<Violation>> readCurrentModulePMDReport() {
138 PMDState.reset(compiledFilesList, compiledTestFilesList, moduleCoordinates);
139 if (pmdReportFile.exists()) {
140 boolean isFirst = true;
141 for (String line : Files.lines(pmdReportFile.toPath()).collect(Collectors.toList())) {
145 PMDState.addViolation(line, moduleCoordinates);
149 } catch (IOException ioe) {
150 throw new UncheckedIOException(ioe);
152 return PMDState.getState();