e711cb0ae74229691f776c9f3d6fd6fab327eff3
[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.JACOCO_SKIP;
20 import static org.openecomp.sdc.onboarding.Constants.SKIP_TEST_RUN;
21 import static org.openecomp.sdc.onboarding.Constants.RESOURCES_CHANGED;
22 import static org.openecomp.sdc.onboarding.Constants.UNICORN;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.io.UncheckedIOException;
27 import java.util.stream.Collectors;
28 import org.apache.maven.plugin.AbstractMojo;
29 import org.apache.maven.plugin.MojoExecutionException;
30 import org.apache.maven.plugin.MojoFailureException;
31 import org.apache.maven.plugins.annotations.LifecyclePhase;
32 import org.apache.maven.plugins.annotations.Mojo;
33 import org.apache.maven.plugins.annotations.Parameter;
34 import org.apache.maven.plugins.annotations.ResolutionScope;
35 import org.apache.maven.project.MavenProject;
36
37 @Mojo(name = "pre-test-compile-helper", threadSafe = true, defaultPhase = LifecyclePhase.GENERATE_TEST_RESOURCES,
38         requiresDependencyResolution = ResolutionScope.TEST)
39 public class PreTestCompileHelperMojo extends AbstractMojo {
40
41     @Parameter
42     private File compiledFilesList;
43     @Parameter
44     private Long staleThreshold;
45     @Parameter
46     private File inputTestFilesList;
47     @Parameter
48     private BuildState buildState;
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 String excludePackaging;
55
56
57     public void execute() throws MojoExecutionException, MojoFailureException {
58         if (!System.getProperties().containsKey(UNICORN)) {
59             return;
60         }
61         if (project.getPackaging().equals(excludePackaging)) {
62             return;
63         }
64         if (compiledFilesList.exists()
65                     && compiledFilesList.lastModified() > System.currentTimeMillis() - staleThreshold) {
66             try {
67                 buildState.markModuleDirty(inputTestFilesList);
68                 project.getProperties().setProperty(SKIP_TEST_RUN, Boolean.FALSE.toString());
69             } catch (IOException e) {
70                 throw new UncheckedIOException(e);
71             }
72         }
73         boolean isTestMust = buildState.isTestMust(moduleCoordinates,
74                 project.getDependencies().stream().map(d -> d.getGroupId() + ":" + d.getArtifactId())
75                        .collect(Collectors.toList()));
76         if (isTestMust) {
77             project.getProperties().setProperty(RESOURCES_CHANGED, Boolean.TRUE.toString());
78             if (!project.getProperties().containsKey(SKIP_TEST_RUN)) {
79                 project.getProperties().setProperty(SKIP_TEST_RUN, Boolean.FALSE.toString());
80             }
81         }
82         if (!project.getProperties().containsKey(SKIP_TEST_RUN)) {
83             project.getProperties().setProperty(SKIP_TEST_RUN, Boolean.TRUE.toString());
84         }
85         if (System.getProperties().containsKey(JACOCO_SKIP) && Boolean.FALSE.equals(Boolean.valueOf(
86                 System.getProperties().getProperty(JACOCO_SKIP)))) {
87             project.getProperties().setProperty(SKIP_TEST_RUN, Boolean.FALSE.toString());
88         }
89     }
90 }