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.plugin.AbstractMojo;
20 import org.apache.maven.plugin.MojoExecutionException;
21 import org.apache.maven.plugin.MojoFailureException;
22 import org.apache.maven.plugins.annotations.LifecyclePhase;
23 import org.apache.maven.plugins.annotations.Mojo;
24 import org.apache.maven.plugins.annotations.Parameter;
25 import org.apache.maven.plugins.annotations.ResolutionScope;
26 import org.apache.maven.project.MavenProject;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.ObjectOutputStream;
33 import java.io.OutputStream;
34 import java.io.UncheckedIOException;
35 import java.nio.file.Files;
36 import java.nio.file.Paths;
37 import java.nio.file.StandardCopyOption;
38 import java.util.List;
41 import static org.openecomp.sdc.onboarding.pmd.PMDHelperUtils.*;
43 @Mojo(name = "init-pmd-helper", threadSafe = true, defaultPhase = LifecyclePhase.PREPARE_PACKAGE,
44 requiresDependencyResolution = ResolutionScope.NONE)
45 public class InitializationHelperMojo extends AbstractMojo {
47 private static final String SKIP_PMD = "skipPMD";
49 @Parameter(defaultValue = "${project}", readonly = true)
50 private MavenProject project;
51 @Parameter(defaultValue = "${project.artifact.groupId}:${project.artifact.artifactId}")
52 private String moduleCoordinates;
54 private File pmdTargetLocation;
56 private File pmdReportFile;
58 private String persistingModuleCoordinates;
60 private File pmdStateFile;
62 private String pmdCurrentStateFilePath;
64 private String excludePackaging;
67 PMDState.setHistoricState(readCurrentPMDState("pmd.dat"));
70 public void execute() throws MojoExecutionException, MojoFailureException {
71 if (project.getPackaging().equals(excludePackaging)) {
74 if (moduleCoordinates.equals(persistingModuleCoordinates)) {
75 pmdStateFile.getParentFile().mkdirs();
76 try (OutputStream os = new FileOutputStream(pmdStateFile);
77 ObjectOutputStream oos = new ObjectOutputStream(os)) {
78 File f = getStateFile(pmdCurrentStateFilePath.substring(0, pmdCurrentStateFilePath.indexOf('/')),
79 project, pmdCurrentStateFilePath);
80 Map<String, List<Violation>> data = readCurrentPMDState(f);
81 if (PMDState.getHistoricState() != null) {
82 PMDState.getHistoricState().putAll(data);
83 oos.writeObject(PMDState.getHistoricState());
85 oos.writeObject(data);
87 if (Paths.get(f.getParentFile().getAbsolutePath(), "compileState.dat").toFile().exists()) {
88 Files.copy(Paths.get(f.getParentFile().getAbsolutePath(), "compileState.dat"),
89 Paths.get(pmdStateFile.getParentFile().getAbsolutePath(), "compile.dat"),
90 StandardCopyOption.REPLACE_EXISTING);
92 } catch (IOException e) {
93 throw new UncheckedIOException(e);
97 if (project.getProperties().containsKey(SKIP_PMD) && Boolean.TRUE.equals(Boolean.valueOf(
98 project.getProperties().getProperty(SKIP_PMD)))) {
101 pmdTargetLocation.getParentFile().mkdirs();
102 try (InputStream is = this.getClass().getResourceAsStream("/pmd-empty.xml");
103 OutputStream os = new FileOutputStream(pmdTargetLocation)) {
104 String text = readInputStream(is);
105 os.write(text.getBytes());
106 } catch (IOException ioe) {
107 throw new UncheckedIOException(ioe);