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