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.isReportEmpty;
21 import static org.openecomp.sdc.onboarding.pmd.PMDHelperUtils.readCurrentPMDState;
22 import static org.openecomp.sdc.onboarding.pmd.PMDHelperUtils.writeCurrentPMDState;
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 org.apache.maven.execution.MavenSession;
32 import org.apache.maven.plugin.AbstractMojo;
33 import org.apache.maven.plugin.MojoExecutionException;
34 import org.apache.maven.plugin.MojoFailureException;
35 import org.apache.maven.plugins.annotations.LifecyclePhase;
36 import org.apache.maven.plugins.annotations.Mojo;
37 import org.apache.maven.plugins.annotations.Parameter;
38 import org.apache.maven.plugins.annotations.ResolutionScope;
39 import org.apache.maven.project.MavenProject;
41 @Mojo(name = "post-verify-helper", threadSafe = true, defaultPhase = LifecyclePhase.VERIFY,
42 requiresDependencyResolution = ResolutionScope.NONE)
43 public class VerifyHelperMojo extends AbstractMojo {
45 private static final String SKIP_PMD = "skipPMD";
47 @Parameter(defaultValue = "${project}", readonly = true)
48 private MavenProject project;
49 @Parameter(defaultValue = "${project.artifact.groupId}:${project.artifact.artifactId}")
50 private String moduleCoordinates;
51 @Parameter(defaultValue = "${session}")
52 private MavenSession session;
54 private File pmdTargetLocation;
56 private File pmdReportFile;
58 private File pmdStateFile;
60 private String pmdCurrentStateFilePath;
62 private String excludePackaging;
64 private Boolean validatePMDReport = Boolean.FALSE;
66 private String persistingModuleCoordinates;
68 private File pmdFailureReportLocation;
70 private File compiledFilesList;
72 private File compiledTestFilesList;
74 private static File pmdCurrentStateFile;
76 public void execute() throws MojoExecutionException, MojoFailureException {
77 if (project.getPackaging().equals(excludePackaging)) {
81 warnDataIssuesIfAny();
83 if (Boolean.FALSE.equals(Boolean.valueOf(project.getProperties().getProperty(SKIP_PMD))) && !isReportEmpty(
85 Map<String, List<Violation>> data = readCurrentPMDState(pmdCurrentStateFile);
86 Map<String, List<Violation>> cv = readCurrentModulePMDReport();
88 boolean error = false;
89 if (!PMDState.getHistoricState().isEmpty() && !PMDHelperUtils
90 .evaluateCodeQuality(PMDState.getHistoricState(), cv,
91 pmdFailureReportLocation, getLog())) {
93 if (validatePMDReport) {
94 throw new MojoFailureException(
95 "PMD Failures encountered. Build halted. For details refer " + pmdFailureReportLocation
99 "\u001B[31m\u001B[1m Code Quality concerns raised by Quality Management System. For details refer "
100 + pmdFailureReportLocation.getAbsolutePath()
101 + " and address them before committing this code in Version Control System. \u001B[0m");
104 String moduleChecksum = project.getProperties().getProperty("mainChecksum") + ":" + project.getProperties()
107 data = reinitializeIfNeeded(!error, data);
109 Map<String, Object> checksumStore = HashMap.class.cast(data);
110 if (!moduleChecksum.equals(checksumStore.get(moduleCoordinates))) {
111 checksumStore.put(moduleCoordinates, moduleChecksum);
112 writeCurrentPMDState(pmdCurrentStateFile, data);
115 if (Boolean.FALSE.equals(Boolean.valueOf(project.getProperties().getProperty(SKIP_PMD)))) {
116 if (isReportEmpty(pmdReportFile)) {
117 HashMap data = HashMap.class.cast(readCurrentPMDState(pmdCurrentStateFile));
118 data.put(moduleCoordinates,
119 project.getProperties().getProperty("mainChecksum") + ":" + project.getProperties().getProperty(
121 writeCurrentPMDState(pmdCurrentStateFile, data);
123 pmdReportFile.delete();
125 removeProcessFiles();
129 private void removeProcessFiles() {
130 if (moduleCoordinates.equals(persistingModuleCoordinates) && pmdStateFile.exists()) {
131 pmdStateFile.delete();
133 if (pmdTargetLocation.exists()) {
134 pmdTargetLocation.delete();
138 private void init() {
139 if (pmdCurrentStateFile == null) {
140 setPmdCurrentStateFile(
141 getStateFile(pmdCurrentStateFilePath.substring(0, pmdCurrentStateFilePath.indexOf('/')), project,
142 pmdCurrentStateFilePath));
144 pmdReportFile.getParentFile().mkdirs();
148 private static void setPmdCurrentStateFile(File file) {
149 pmdCurrentStateFile = file;
150 pmdCurrentStateFile.getParentFile().mkdirs();
153 private Map<String, List<Violation>> readCurrentModulePMDReport() {
155 PMDState.reset(compiledFilesList, compiledTestFilesList, moduleCoordinates);
156 if (pmdReportFile.exists()) {
157 List<String> lines = Files.readAllLines(pmdReportFile.toPath());
159 for (String line : lines) {
160 PMDState.addViolation(line, moduleCoordinates);
163 } catch (IOException ioe) {
164 throw new UncheckedIOException(ioe);
166 return PMDState.getState();
169 private void warnDataIssuesIfAny() {
170 if (PMDState.getHistoricState() != null && PMDState.getHistoricState().isEmpty()) {
171 getLog().error("PMD Check is skipped. problem while loading data.");
175 private Map<String, List<Violation>> reinitializeIfNeeded(boolean required, Map<String, List<Violation>> orig) {
177 return readCurrentPMDState(pmdCurrentStateFile);