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 org.apache.maven.execution.MavenSession;
20 import org.apache.maven.plugin.AbstractMojo;
21 import org.apache.maven.plugin.MojoExecutionException;
22 import org.apache.maven.plugin.MojoFailureException;
23 import org.apache.maven.plugins.annotations.LifecyclePhase;
24 import org.apache.maven.plugins.annotations.Mojo;
25 import org.apache.maven.plugins.annotations.Parameter;
26 import org.apache.maven.plugins.annotations.ResolutionScope;
27 import org.apache.maven.project.MavenProject;
30 import java.io.IOException;
31 import java.io.UncheckedIOException;
32 import java.nio.file.Files;
33 import java.util.HashMap;
34 import java.util.List;
37 import static org.openecomp.sdc.onboarding.pmd.PMDHelperUtils.*;
39 @Mojo(name = "post-verify-helper", threadSafe = true, defaultPhase = LifecyclePhase.VERIFY,
40 requiresDependencyResolution = ResolutionScope.NONE)
41 public class VerifyHelperMojo extends AbstractMojo {
43 private static final String SKIP_PMD = "skipPMD";
45 @Parameter(defaultValue = "${project}", readonly = true)
46 private MavenProject project;
47 @Parameter(defaultValue = "${project.artifact.groupId}:${project.artifact.artifactId}")
48 private String moduleCoordinates;
49 @Parameter(defaultValue = "${session}")
50 private MavenSession session;
52 private File pmdTargetLocation;
54 private File pmdReportFile;
56 private File pmdStateFile;
58 private String pmdCurrentStateFilePath;
60 private String excludePackaging;
62 private Boolean validatePMDReport = Boolean.FALSE;
64 private String persistingModuleCoordinates;
66 private File pmdFailureReportLocation;
68 private File compiledFilesList;
70 private File compiledTestFilesList;
72 private static File pmdCurrentStateFile;
74 public void execute() throws MojoExecutionException, MojoFailureException {
75 if (project.getPackaging().equals(excludePackaging)) {
79 warnDataIssuesIfAny();
81 if (Boolean.FALSE.equals(Boolean.valueOf(project.getProperties().getProperty(SKIP_PMD))) && !isReportEmpty(
83 Map<String, List<Violation>> data = readCurrentPMDState(pmdCurrentStateFile);
84 Map<String, List<Violation>> cv = readCurrentModulePMDReport();
86 boolean error = false;
87 if (!PMDState.getHistoricState().isEmpty() && !PMDHelperUtils
88 .evaluateCodeQuality(PMDState.getHistoricState(), cv,
89 pmdFailureReportLocation, getLog())) {
91 if (validatePMDReport) {
92 throw new MojoFailureException(
93 "PMD Failures encountered. Build halted. For details refer " + pmdFailureReportLocation
97 "\u001B[31m\u001B[1m Code Quality concerns raised by Quality Management System. For details refer "
98 + pmdFailureReportLocation.getAbsolutePath()
99 + " and address them before committing this code in Version Control System. \u001B[0m");
102 String moduleChecksum = project.getProperties().getProperty("mainChecksum") + ":" + project.getProperties()
105 data = reinitializeIfNeeded(!error, data);
107 Map<String, Object> checksumStore = HashMap.class.cast(data);
108 if (!moduleChecksum.equals(checksumStore.get(moduleCoordinates))) {
109 checksumStore.put(moduleCoordinates, moduleChecksum);
110 writeCurrentPMDState(pmdCurrentStateFile, data);
113 if (Boolean.FALSE.equals(Boolean.valueOf(project.getProperties().getProperty(SKIP_PMD)))) {
114 if (isReportEmpty(pmdReportFile)) {
115 HashMap data = HashMap.class.cast(readCurrentPMDState(pmdCurrentStateFile));
116 data.put(moduleCoordinates,
117 project.getProperties().getProperty("mainChecksum") + ":" + project.getProperties().getProperty(
119 writeCurrentPMDState(pmdCurrentStateFile, data);
121 pmdReportFile.delete();
123 removeProcessFiles();
127 private void removeProcessFiles() {
128 if (moduleCoordinates.equals(persistingModuleCoordinates) && pmdStateFile.exists()) {
129 for (File file : pmdStateFile.getParentFile().listFiles()) {
135 if (pmdTargetLocation.exists()) {
136 pmdTargetLocation.delete();
140 private void init() {
141 if (pmdCurrentStateFile == null) {
142 setPmdCurrentStateFile(
143 getStateFile(pmdCurrentStateFilePath.substring(0, pmdCurrentStateFilePath.indexOf('/')), project,
144 pmdCurrentStateFilePath));
146 pmdReportFile.getParentFile().mkdirs();
150 private static void setPmdCurrentStateFile(File file) {
151 pmdCurrentStateFile = file;
152 pmdCurrentStateFile.getParentFile().mkdirs();
155 private Map<String, List<Violation>> readCurrentModulePMDReport() {
157 PMDState.reset(compiledFilesList, compiledTestFilesList, moduleCoordinates);
158 if (pmdReportFile.exists()) {
159 List<String> lines = Files.readAllLines(pmdReportFile.toPath());
161 for (String line : lines) {
162 PMDState.addViolation(line, moduleCoordinates);
165 } catch (IOException ioe) {
166 throw new UncheckedIOException(ioe);
168 return PMDState.getState();
171 private void warnDataIssuesIfAny() {
172 if (PMDState.getHistoricState() != null && PMDState.getHistoricState().isEmpty()) {
173 getLog().error("PMD Check is skipped. problem while loading data.");
177 private Map<String, List<Violation>> reinitializeIfNeeded(boolean required, Map<String, List<Violation>> orig) {
179 return readCurrentPMDState(pmdCurrentStateFile);