re base code
[sdc.git] / openecomp-be / tools / pmd-helper-plugin / src / main / java / org / openecomp / sdc / onboarding / pmd / InitializationHelperMojo.java
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 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;
27
28 import java.io.File;
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;
39 import java.util.Map;
40
41 import static org.openecomp.sdc.onboarding.pmd.PMDHelperUtils.*;
42
43 @Mojo(name = "init-pmd-helper", threadSafe = true, defaultPhase = LifecyclePhase.PREPARE_PACKAGE,
44         requiresDependencyResolution = ResolutionScope.NONE)
45 public class InitializationHelperMojo extends AbstractMojo {
46
47     private static final String SKIP_PMD = "skipPMD";
48
49     @Parameter(defaultValue = "${project}", readonly = true)
50     private MavenProject project;
51     @Parameter(defaultValue = "${project.artifact.groupId}:${project.artifact.artifactId}")
52     private String moduleCoordinates;
53     @Parameter
54     private File pmdTargetLocation;
55     @Parameter
56     private File pmdReportFile;
57     @Parameter
58     private String persistingModuleCoordinates;
59     @Parameter
60     private File pmdStateFile;
61     @Parameter
62     private String pmdCurrentStateFilePath;
63     @Parameter
64     private String excludePackaging;
65
66     static {
67         PMDState.setHistoricState(readCurrentPMDState("pmd.dat"));
68     }
69
70     public void execute() throws MojoExecutionException, MojoFailureException {
71         if (project.getPackaging().equals(excludePackaging)) {
72             return;
73         }
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());
84                 } else {
85                     oos.writeObject(data);
86                 }
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);
91                 }
92             } catch (IOException e) {
93                 throw new UncheckedIOException(e);
94             }
95             return;
96         }
97         if (project.getProperties().containsKey(SKIP_PMD) && Boolean.TRUE.equals(Boolean.valueOf(
98                 project.getProperties().getProperty(SKIP_PMD)))) {
99             return;
100         }
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);
108         }
109     }
110
111 }