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.readInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.ObjectOutputStream;
28 import java.io.OutputStream;
29 import java.io.UncheckedIOException;
30 import java.nio.file.Files;
31 import java.nio.file.Paths;
32 import java.nio.file.StandardCopyOption;
33 import java.util.List;
35 import org.apache.maven.plugin.AbstractMojo;
36 import org.apache.maven.plugin.MojoExecutionException;
37 import org.apache.maven.plugin.MojoFailureException;
38 import org.apache.maven.plugins.annotations.LifecyclePhase;
39 import org.apache.maven.plugins.annotations.Mojo;
40 import org.apache.maven.plugins.annotations.Parameter;
41 import org.apache.maven.plugins.annotations.ResolutionScope;
42 import org.apache.maven.project.MavenProject;
44 @Mojo(name = "init-pmd-helper", threadSafe = true, defaultPhase = LifecyclePhase.PREPARE_PACKAGE,
45 requiresDependencyResolution = ResolutionScope.NONE)
46 public class InitializationHelperMojo extends AbstractMojo {
48 private static final String SKIP_PMD = "skipPMD";
50 @Parameter(defaultValue = "${project}", readonly = true)
51 private MavenProject project;
52 @Parameter(defaultValue = "${project.artifact.groupId}:${project.artifact.artifactId}")
53 private String moduleCoordinates;
55 private File pmdTargetLocation;
57 private File pmdReportFile;
59 private String persistingModuleCoordinates;
61 private File pmdStateFile;
63 private String pmdCurrentStateFilePath;
65 private String excludePackaging;
68 PMDState.setHistoricState(readCurrentPMDState("pmd.dat"));
71 public void execute() throws MojoExecutionException, MojoFailureException {
72 if (project.getPackaging().equals(excludePackaging)) {
75 if (moduleCoordinates.equals(persistingModuleCoordinates)) {
76 pmdStateFile.getParentFile().mkdirs();
77 try (OutputStream os = new FileOutputStream(pmdStateFile);
78 ObjectOutputStream oos = new ObjectOutputStream(os)) {
79 File f = getStateFile(pmdCurrentStateFilePath.substring(0, pmdCurrentStateFilePath.indexOf('/')),
80 project, pmdCurrentStateFilePath);
81 Map<String, List<Violation>> data = readCurrentPMDState(f);
82 if (PMDState.getHistoricState() != null) {
83 PMDState.getHistoricState().putAll(data);
84 oos.writeObject(PMDState.getHistoricState());
86 oos.writeObject(data);
88 if (Paths.get(f.getParentFile().getAbsolutePath(), "compileState.dat").toFile().exists()) {
89 Files.copy(Paths.get(f.getParentFile().getAbsolutePath(), "compileState.dat"),
90 Paths.get(pmdStateFile.getParentFile().getAbsolutePath(), "compile.dat"),
91 StandardCopyOption.REPLACE_EXISTING);
93 } catch (IOException e) {
94 throw new UncheckedIOException(e);
98 if (project.getProperties().containsKey(SKIP_PMD) && Boolean.TRUE.equals(Boolean.valueOf(
99 project.getProperties().getProperty(SKIP_PMD)))) {
102 pmdTargetLocation.getParentFile().mkdirs();
103 try (InputStream is = this.getClass().getResourceAsStream("/pmd-empty.xml");
104 OutputStream os = new FileOutputStream(pmdTargetLocation)) {
105 String text = readInputStream(is);
106 os.write(text.getBytes());
107 } catch (IOException ioe) {
108 throw new UncheckedIOException(ioe);