re base code
[sdc.git] / openecomp-be / tools / compile-helper-plugin / src / main / java / org / openecomp / sdc / onboarding / PreTestCompileHelperMojo.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.model.Plugin;
20 import org.apache.maven.plugin.AbstractMojo;
21 import org.apache.maven.plugin.MojoExecutionException;
22 import org.apache.maven.plugin.MojoFailureException;
23 import org.apache.maven.plugins.annotations.LifecyclePhase;
24 import org.apache.maven.plugins.annotations.Mojo;
25 import org.apache.maven.plugins.annotations.Parameter;
26 import org.apache.maven.plugins.annotations.ResolutionScope;
27 import org.apache.maven.project.MavenProject;
28 import org.codehaus.plexus.util.xml.Xpp3Dom;
29
30 import java.util.List;
31
32 import static org.openecomp.sdc.onboarding.Constants.*;
33
34 @Mojo(name = "pre-test-compile-helper", threadSafe = true, defaultPhase = LifecyclePhase.PROCESS_TEST_CLASSES,
35         requiresDependencyResolution = ResolutionScope.TEST)
36 public class PreTestCompileHelperMojo extends AbstractMojo {
37
38     @Parameter
39     private BuildState buildState;
40     @Parameter(defaultValue = "${project}", readonly = true)
41     private MavenProject project;
42     @Parameter(defaultValue = "${project.artifact.groupId}:${project.artifact.artifactId}")
43     private String moduleCoordinates;
44     @Parameter(defaultValue = "${project.buildPlugins}", readonly = true)
45     private List<Plugin> plugins;
46     @Parameter
47     private String excludePackaging;
48
49
50     public void execute() throws MojoExecutionException, MojoFailureException {
51         if (!System.getProperties().containsKey(UNICORN)) {
52             return;
53         }
54         if (project.getPackaging().equals(excludePackaging)) {
55             return;
56         }
57         if (buildState.isTestExecutionMandatory()) {
58             project.getProperties().setProperty(SKIP_TEST_RUN, Boolean.FALSE.toString());
59         }
60         boolean isTestMust = buildState.isTestMust(moduleCoordinates);
61         if (isTestMust) {
62             project.getProperties().setProperty(RESOURCES_CHANGED, Boolean.TRUE.toString());
63             if (!project.getProperties().containsKey(SKIP_TEST_RUN)) {
64                 project.getProperties().setProperty(SKIP_TEST_RUN, Boolean.FALSE.toString());
65             }
66         }
67         if (!project.getProperties().containsKey(SKIP_TEST_RUN) || isTestSkippedExplicitly()) {
68             project.getProperties().setProperty(SKIP_TEST_RUN, Boolean.TRUE.toString());
69         }
70         if (System.getProperties().containsKey(JACOCO_SKIP) && Boolean.FALSE.equals(Boolean.valueOf(
71                 System.getProperties().getProperty(JACOCO_SKIP)))) {
72             project.getProperties().setProperty(SKIP_TEST_RUN, Boolean.FALSE.toString());
73         }
74     }
75
76
77     private boolean isTestSkippedExplicitly() {
78         for (Plugin p : plugins) {
79             if ("org.apache.maven.plugins:maven-surefire-plugin".equals(p.getKey())) {
80                 Xpp3Dom dom = Xpp3Dom.class.cast(p.getConfiguration());
81                 if (dom.getChild(SKIP_TESTS) != null) {
82                     return Boolean.TRUE.equals(Boolean.valueOf(dom.getValue()));
83                 }
84             }
85         }
86         return false;
87     }
88 }