2 * Copyright © 2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.onboarding;
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;
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;
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;
40 @Mojo(name = "pre-compile-helper", threadSafe = true, defaultPhase = LifecyclePhase.GENERATE_SOURCES,
41 requiresDependencyResolution = ResolutionScope.TEST)
42 public class PreCompileHelperMojo extends AbstractMojo {
44 @Parameter(defaultValue = "${project}", readonly = true)
45 private MavenProject project;
46 @Parameter(defaultValue = "${project.artifact.groupId}:${project.artifact.artifactId}")
47 private String moduleCoordinates;
49 private String excludePackaging;
51 private List<String> excludeDependencies;
53 private File mainCompiledLocation;
55 private File testCompiledLocation;
57 private File inputSourceFilesList;
59 private File inputTestFilesList;
61 private BuildState buildState;
63 public void execute() throws MojoExecutionException {
64 if (!System.getProperties().containsKey(UNICORN)) {
67 if (project.getPackaging().equals(excludePackaging)) {
71 Map<String, Object> moduleBuildData = getCurrentModuleBuildData();
72 Map<String, Object> lastTimeModuleBuildData = buildState.readModuleBuildData();
74 boolean buildDataSameWithPreviousBuild = lastTimeModuleBuildData.get(MAIN) != null && moduleBuildData.get(MAIN)
75 .equals(lastTimeModuleBuildData
77 boolean isFirstBuild = buildState.getBuildTime(moduleCoordinates) == 0;
79 if (isCompileNeeded(HashMap.class.cast(moduleBuildData.get(MAIN)).keySet(), isFirstBuild,
80 buildDataSameWithPreviousBuild)) {
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);
90 if (!moduleBuildData.get(TEST).equals(lastTimeModuleBuildData.get(TEST))) {
92 buildState.markModuleDirty(inputTestFilesList);
93 project.getProperties().setProperty(SKIP_TEST_RUN, Boolean.FALSE.toString());
94 } catch (IOException e) {
95 throw new UncheckedIOException(e);
99 if (!moduleBuildData.equals(lastTimeModuleBuildData)) {
100 buildState.addModuleBuildData(moduleCoordinates, moduleBuildData);
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()
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()
119 private boolean isCompileNeeded(Collection<String> dependencyCoordinates, boolean isFirstBuild,
120 boolean buildDataSame) {
121 return isFirstBuild || !buildDataSame || buildState.isCompileMust(moduleCoordinates, dependencyCoordinates);
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;
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());
137 HashMap.class.cast(moduleBuildData.get(MAIN))
138 .put(dependency.getGroupId() + ":" + dependency.getArtifactId(), dependency.getVersion());
140 return moduleBuildData;