re base code
[sdc.git] / openecomp-be / tools / compile-helper-plugin / src / main / java / org / openecomp / sdc / onboarding / PostCompileHelperMojo.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;
18
19 import org.apache.maven.plugin.AbstractMojo;
20 import org.apache.maven.plugin.MojoExecutionException;
21 import org.apache.maven.plugins.annotations.LifecyclePhase;
22 import org.apache.maven.plugins.annotations.Mojo;
23 import org.apache.maven.plugins.annotations.Parameter;
24 import org.apache.maven.plugins.annotations.ResolutionScope;
25 import org.apache.maven.project.MavenProject;
26
27 import java.io.File;
28
29 import static org.openecomp.sdc.onboarding.Constants.*;
30
31 @Mojo(name = "post-compile-helper", threadSafe = true, defaultPhase = LifecyclePhase.TEST_COMPILE,
32         requiresDependencyResolution = ResolutionScope.TEST)
33 public class PostCompileHelperMojo extends AbstractMojo {
34
35     @Parameter(defaultValue = "${project}", readonly = true)
36     private MavenProject project;
37     @Parameter(defaultValue = "${project.artifact.groupId}:${project.artifact.artifactId}")
38     private String moduleCoordinates;
39     @Parameter
40     private String excludePackaging;
41     @Parameter
42     private BuildState buildState;
43     @Parameter
44     private File mainResourceLocation;
45     @Parameter
46     private File testResourceLocation;
47
48
49     public void execute() throws MojoExecutionException {
50         if (!System.getProperties().containsKey(UNICORN)) {
51             return;
52         }
53         if (project.getPackaging().equals(excludePackaging)) {
54             return;
55         }
56         if (project.getProperties().containsKey(TEST_ONLY)) {
57             project.getProperties().setProperty(SKIP_MAIN_SOURCE_COMPILE, Boolean.TRUE.toString());
58             project.getProperties().remove(TEST_ONLY);
59         }
60         postProcessInstrumentedModules();
61
62         if (project.getProperties().containsKey(RESOURCE_WITH_TEST_ONLY)) {
63             project.getProperties().setProperty(SKIP_MAIN_SOURCE_COMPILE, Boolean.TRUE.toString());
64             project.getProperties().remove(RESOURCE_WITH_TEST_ONLY);
65         }
66         if (project.getProperties().containsKey(RESOURCE_ONLY)) {
67             project.getProperties().setProperty(SKIP_MAIN_SOURCE_COMPILE, Boolean.TRUE.toString());
68             project.getProperties().setProperty(SKIP_TEST_SOURCE_COMPILE, Boolean.TRUE.toString());
69             project.getProperties().remove(RESOURCE_ONLY);
70         }
71         if (project.getProperties().containsKey(TEST_RESOURCE_ONLY)) {
72             project.getProperties().setProperty(SKIP_MAIN_SOURCE_COMPILE, Boolean.TRUE.toString());
73             project.getProperties().setProperty(SKIP_TEST_SOURCE_COMPILE, Boolean.TRUE.toString());
74             project.getProperties().remove(TEST_RESOURCE_ONLY);
75         }
76         if (!project.getProperties().containsKey(SKIP_MAIN_SOURCE_COMPILE)) {
77             buildState.addModuleBuildTime(moduleCoordinates, System.currentTimeMillis());
78             project.getProperties().setProperty(SKIP_PMD, Boolean.FALSE.toString());
79         }
80         if (!project.getProperties().containsKey(SKIP_TEST_SOURCE_COMPILE)) {
81             project.getProperties().setProperty(SKIP_PMD, Boolean.FALSE.toString());
82         }
83         buildState.saveModuleBuildData(moduleCoordinates);
84     }
85
86     private void postProcessInstrumentedModules() {
87         if (project.getProperties().containsKey(INSTRUMENT_ONLY)) {
88             project.getProperties().setProperty(SKIP_MAIN_SOURCE_COMPILE, Boolean.TRUE.toString());
89             project.getProperties().setProperty(SKIP_TEST_SOURCE_COMPILE, Boolean.TRUE.toString());
90             project.getProperties().remove(INSTRUMENT_ONLY);
91         }
92         if (project.getProperties().containsKey(INSTRUMENT_WITH_TEST_ONLY)) {
93             project.getProperties().setProperty(SKIP_MAIN_SOURCE_COMPILE, Boolean.TRUE.toString());
94             project.getProperties().remove(INSTRUMENT_WITH_TEST_ONLY);
95         }
96     }
97 }