4102a1b3828729c68677cb59272e54eef6bf670a
[sdc.git] /
1 /*
2  * Copyright © 2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.onboarding.pmd;
18
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;
22
23 import java.io.File;
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;
34 import java.util.Map;
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;
43
44 @Mojo(name = "init-pmd-helper", threadSafe = true, defaultPhase = LifecyclePhase.PREPARE_PACKAGE,
45         requiresDependencyResolution = ResolutionScope.NONE)
46 public class InitializationHelperMojo extends AbstractMojo {
47
48     private static final String SKIP_PMD = "skipPMD";
49
50     @Parameter(defaultValue = "${project}", readonly = true)
51     private MavenProject project;
52     @Parameter(defaultValue = "${project.artifact.groupId}:${project.artifact.artifactId}")
53     private String moduleCoordinates;
54     @Parameter
55     private File pmdTargetLocation;
56     @Parameter
57     private File pmdReportFile;
58     @Parameter
59     private String persistingModuleCoordinates;
60     @Parameter
61     private File pmdStateFile;
62     @Parameter
63     private String pmdCurrentStateFilePath;
64     @Parameter
65     private String excludePackaging;
66
67     static {
68         PMDState.setHistoricState(readCurrentPMDState("pmd.dat"));
69     }
70
71     public void execute() throws MojoExecutionException, MojoFailureException {
72         if (project.getPackaging().equals(excludePackaging)) {
73             return;
74         }
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());
85                 } else {
86                     oos.writeObject(data);
87                 }
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);
92                 }
93             } catch (IOException e) {
94                 throw new UncheckedIOException(e);
95             }
96             return;
97         }
98         if (project.getProperties().containsKey(SKIP_PMD) && Boolean.TRUE.equals(Boolean.valueOf(
99                 project.getProperties().getProperty(SKIP_PMD)))) {
100             return;
101         }
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);
109         }
110     }
111
112 }