summaryrefslogtreecommitdiffstats
path: root/openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/PreCompileHelperMojo.java
blob: faa3167e370689f2e8197e256c5e6dee6d2cb15d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * Copyright © 2018 European Support Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on a "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openecomp.sdc.onboarding;

import static org.openecomp.sdc.onboarding.Constants.MAIN;
import static org.openecomp.sdc.onboarding.Constants.SKIP_TEST_RUN;
import static org.openecomp.sdc.onboarding.Constants.TEST;
import static org.openecomp.sdc.onboarding.Constants.UNICORN;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;

@Mojo(name = "pre-compile-helper", threadSafe = true, defaultPhase = LifecyclePhase.GENERATE_SOURCES,
        requiresDependencyResolution = ResolutionScope.TEST)
public class PreCompileHelperMojo extends AbstractMojo {

    @Parameter(defaultValue = "${project}", readonly = true)
    private MavenProject project;
    @Parameter(defaultValue = "${project.artifact.groupId}:${project.artifact.artifactId}")
    private String moduleCoordinates;
    @Parameter
    private String excludePackaging;
    @Parameter
    private List<String> excludeDependencies;
    @Parameter
    private File mainCompiledLocation;
    @Parameter
    private File testCompiledLocation;
    @Parameter
    private File inputSourceFilesList;
    @Parameter
    private File inputTestFilesList;
    @Parameter
    private BuildState buildState;

    public void execute() throws MojoExecutionException {
        if (!System.getProperties().containsKey(UNICORN)) {
            return;
        }
        if (project.getPackaging().equals(excludePackaging)) {
            return;
        }

        Map<String, Object> moduleBuildData = getCurrentModuleBuildData();
        Map<String, Object> lastTimeModuleBuildData = buildState.readModuleBuildData();

        boolean buildDataSameWithPreviousBuild = lastTimeModuleBuildData.get(MAIN) != null && moduleBuildData.get(MAIN)
                                                                                                             .equals(lastTimeModuleBuildData
                                                                                                                             .get(MAIN));
        boolean isFirstBuild = buildState.getBuildTime(moduleCoordinates) == 0;

        if (isCompileNeeded(HashMap.class.cast(moduleBuildData.get(MAIN)).keySet(), isFirstBuild,
                buildDataSameWithPreviousBuild)) {
            try {
                buildState.markModuleDirty(inputSourceFilesList);
                buildState.markModuleDirty(inputTestFilesList);
                project.getProperties().setProperty(SKIP_TEST_RUN, "false");
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        if (!moduleBuildData.get(TEST).equals(lastTimeModuleBuildData.get(TEST))) {
            try {
                buildState.markModuleDirty(inputTestFilesList);
                project.getProperties().setProperty(SKIP_TEST_RUN, Boolean.FALSE.toString());
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        if (!moduleBuildData.equals(lastTimeModuleBuildData)) {
            buildState.addModuleBuildData(moduleCoordinates, moduleBuildData);
        }

        if (inputSourceFilesList.isFile() && inputSourceFilesList.length() == 0) {
            if (!inputSourceFilesList.delete()) {
                throw new MojoExecutionException(
                        "****** Please remove 'target' directory manually under path " + project.getBasedir()
                                                                                                .getAbsolutePath());
            }
        }
        if (inputTestFilesList.isFile() && inputTestFilesList.length() == 0) {
            if (!inputTestFilesList.delete()) {
                throw new MojoExecutionException(
                        "****** Please remove 'target' directory manually under path " + project.getBasedir()
                                                                                                .getAbsolutePath());
            }
        }
    }

    private boolean isCompileNeeded(Collection<String> dependencyCoordinates, boolean isFirstBuild,
            boolean buildDataSame) {
        return isFirstBuild || !buildDataSame || buildState.isCompileMust(moduleCoordinates, dependencyCoordinates);
    }

    private Map<String, Object> getCurrentModuleBuildData() {
        Map<String, Object> moduleBuildData = new HashMap<>();
        moduleBuildData.put(MAIN, new HashMap<String, String>());
        moduleBuildData.put(TEST, new HashMap<String, String>());
        if (project.getArtifacts() == null || project.getArtifacts().isEmpty()) {
            return moduleBuildData;
        }
        for (Artifact dependency : project.getArtifacts()) {
            if (excludeDependencies.contains(dependency.getScope())) {
                HashMap.class.cast(moduleBuildData.get(TEST))
                             .put(dependency.getGroupId() + ":" + dependency.getArtifactId(), dependency.getVersion());
                continue;
            }
            HashMap.class.cast(moduleBuildData.get(MAIN))
                         .put(dependency.getGroupId() + ":" + dependency.getArtifactId(), dependency.getVersion());
        }
        return moduleBuildData;
    }
}