faa3167e370689f2e8197e256c5e6dee6d2cb15d
[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.MAIN;
20 import static org.openecomp.sdc.onboarding.Constants.SKIP_TEST_RUN;
21 import static org.openecomp.sdc.onboarding.Constants.TEST;
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.Collection;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import org.apache.maven.artifact.Artifact;
32 import org.apache.maven.plugin.AbstractMojo;
33 import org.apache.maven.plugin.MojoExecutionException;
34 import org.apache.maven.plugins.annotations.LifecyclePhase;
35 import org.apache.maven.plugins.annotations.Mojo;
36 import org.apache.maven.plugins.annotations.Parameter;
37 import org.apache.maven.plugins.annotations.ResolutionScope;
38 import org.apache.maven.project.MavenProject;
39
40 @Mojo(name = "pre-compile-helper", threadSafe = true, defaultPhase = LifecyclePhase.GENERATE_SOURCES,
41         requiresDependencyResolution = ResolutionScope.TEST)
42 public class PreCompileHelperMojo extends AbstractMojo {
43
44     @Parameter(defaultValue = "${project}", readonly = true)
45     private MavenProject project;
46     @Parameter(defaultValue = "${project.artifact.groupId}:${project.artifact.artifactId}")
47     private String moduleCoordinates;
48     @Parameter
49     private String excludePackaging;
50     @Parameter
51     private List<String> excludeDependencies;
52     @Parameter
53     private File mainCompiledLocation;
54     @Parameter
55     private File testCompiledLocation;
56     @Parameter
57     private File inputSourceFilesList;
58     @Parameter
59     private File inputTestFilesList;
60     @Parameter
61     private BuildState buildState;
62
63     public void execute() throws MojoExecutionException {
64         if (!System.getProperties().containsKey(UNICORN)) {
65             return;
66         }
67         if (project.getPackaging().equals(excludePackaging)) {
68             return;
69         }
70
71         Map<String, Object> moduleBuildData = getCurrentModuleBuildData();
72         Map<String, Object> lastTimeModuleBuildData = buildState.readModuleBuildData();
73
74         boolean buildDataSameWithPreviousBuild = lastTimeModuleBuildData.get(MAIN) != null && moduleBuildData.get(MAIN)
75                                                                                                              .equals(lastTimeModuleBuildData
76                                                                                                                              .get(MAIN));
77         boolean isFirstBuild = buildState.getBuildTime(moduleCoordinates) == 0;
78
79         if (isCompileNeeded(HashMap.class.cast(moduleBuildData.get(MAIN)).keySet(), isFirstBuild,
80                 buildDataSameWithPreviousBuild)) {
81             try {
82                 buildState.markModuleDirty(inputSourceFilesList);
83                 buildState.markModuleDirty(inputTestFilesList);
84                 project.getProperties().setProperty(SKIP_TEST_RUN, "false");
85             } catch (IOException e) {
86                 throw new UncheckedIOException(e);
87             }
88         }
89
90         if (!moduleBuildData.get(TEST).equals(lastTimeModuleBuildData.get(TEST))) {
91             try {
92                 buildState.markModuleDirty(inputTestFilesList);
93                 project.getProperties().setProperty(SKIP_TEST_RUN, Boolean.FALSE.toString());
94             } catch (IOException e) {
95                 throw new UncheckedIOException(e);
96             }
97         }
98
99         if (!moduleBuildData.equals(lastTimeModuleBuildData)) {
100             buildState.addModuleBuildData(moduleCoordinates, moduleBuildData);
101         }
102
103         if (inputSourceFilesList.isFile() && inputSourceFilesList.length() == 0) {
104             if (!inputSourceFilesList.delete()) {
105                 throw new MojoExecutionException(
106                         "****** Please remove 'target' directory manually under path " + project.getBasedir()
107                                                                                                 .getAbsolutePath());
108             }
109         }
110         if (inputTestFilesList.isFile() && inputTestFilesList.length() == 0) {
111             if (!inputTestFilesList.delete()) {
112                 throw new MojoExecutionException(
113                         "****** Please remove 'target' directory manually under path " + project.getBasedir()
114                                                                                                 .getAbsolutePath());
115             }
116         }
117     }
118
119     private boolean isCompileNeeded(Collection<String> dependencyCoordinates, boolean isFirstBuild,
120             boolean buildDataSame) {
121         return isFirstBuild || !buildDataSame || buildState.isCompileMust(moduleCoordinates, dependencyCoordinates);
122     }
123
124     private Map<String, Object> getCurrentModuleBuildData() {
125         Map<String, Object> moduleBuildData = new HashMap<>();
126         moduleBuildData.put(MAIN, new HashMap<String, String>());
127         moduleBuildData.put(TEST, new HashMap<String, String>());
128         if (project.getArtifacts() == null || project.getArtifacts().isEmpty()) {
129             return moduleBuildData;
130         }
131         for (Artifact dependency : project.getArtifacts()) {
132             if (excludeDependencies.contains(dependency.getScope())) {
133                 HashMap.class.cast(moduleBuildData.get(TEST))
134                              .put(dependency.getGroupId() + ":" + dependency.getArtifactId(), dependency.getVersion());
135                 continue;
136             }
137             HashMap.class.cast(moduleBuildData.get(MAIN))
138                          .put(dependency.getGroupId() + ":" + dependency.getArtifactId(), dependency.getVersion());
139         }
140         return moduleBuildData;
141     }
142 }