aboutsummaryrefslogtreecommitdiffstats
path: root/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/GenerateSourcesMojo.java
blob: 5fd9f574946e06a413c0da587f57d5f042fac27d (plain) (blame)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.plugin.mojo;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.annotations.Requirement;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;
import java.util.regex.Pattern;

import static org.twdata.maven.mojoexecutor.MojoExecutor.*;

/**
 * Calls the generate-sources phase in the container lifecycle defined in lifecycle.xml.
 *
 * @author Tony Vaagenes
 */
@Mojo(name = "generateSources", requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true)
public class GenerateSourcesMojo extends AbstractMojo {

    @Parameter(defaultValue = "${project}")
    protected org.apache.maven.project.MavenProject project;

    @Parameter(defaultValue = "${session}", readonly = true, required = true)
    protected MavenSession session;

    @Component
    @Requirement
    private BuildPluginManager pluginManager;

    @Parameter
    protected String configGenVersion;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        String configGenVersion = getConfigGenVersion();
        getLog().debug("configGenVersion = " + configGenVersion);

        executeMojo(
                plugin(
                        groupId("com.yahoo.vespa"),
                        artifactId("config-class-plugin"),
                        version(releaseVersion(configGenVersion))),
                goal("config-gen"),
                configuration(
                        element(name("defFilesDirectories"), "src/main/resources/configdefinitions")),
                createExecutionEnvironment());
        //Compile source roots added in container-lifecycle is not currently
        //propagated automatically to this project.
        project.addCompileSourceRoot(project.getBuild().getDirectory() + "/generated-sources/vespa-configgen-plugin");
    }

    private ExecutionEnvironment createExecutionEnvironment() throws MojoExecutionException {
        return executionEnvironment(
                project,
                session,
                pluginManager);
    }

    private String getConfigGenVersion() throws MojoExecutionException {
        if (configGenVersion != null && !configGenVersion.isEmpty()) {
            return configGenVersion;
        }

        Dependency container = getVespaDependency("container");
        if (container != null)
            return container.getVersion();

        Dependency containerDev = getVespaDependency("container-dev");
        if (containerDev != null)
            return containerDev.getVersion();

        Dependency docproc = getVespaDependency("docproc");
        if (docproc != null)
            return docproc.getVersion();

        MavenProject parent = getVespaParent();
        if (parent != null)
            return parent.getVersion();

        String defaultConfigGenVersion = loadDefaultConfigGenVersion();
        getLog().warn(String.format(
                "Did not find either container or container-dev artifact in project dependencies, "
                + "using default version '%s' of the config class plugin.",
                defaultConfigGenVersion));

        return defaultConfigGenVersion;
    }

    static String loadDefaultConfigGenVersion() throws MojoExecutionException {
        Properties props = new Properties();
        try {
            props.load(GenerateSourcesMojo.class.getResourceAsStream("/build.properties"));
        } catch (IOException e) {
            throw new MojoExecutionException("Failed to resolve version of com.yahoo.vespa:config-class-plugin.",
                                             new FileNotFoundException("/build.properties"));
        }
        return props.getProperty("projectVersion");
    }

    private MavenProject getVespaParent() {
        MavenProject parent = project.getParent();
        if (parent != null &&
                "com.yahoo.vespa".equals(parent.getGroupId()) &&
                "parent".equals(parent.getArtifactId())) {

            return parent;
        }

        return null;
    }

    private Dependency getVespaDependency(String artifactId) {
        for (Object element : project.getDependencies()) {
            Dependency dependency = (Dependency) element;

            if ("com.yahoo.vespa".equals(dependency.getGroupId()) &&
                    artifactId.equals(dependency.getArtifactId())) {
                return dependency;
            }
        }

        return null;
    }

    static String releaseVersion(String mavenVersion) {
        if (mavenVersion.endsWith("-SNAPSHOT")) {
            return mavenVersion;
        } else {
            String[] parts = mavenVersion.split(Pattern.quote("."));
            if (parts.length <= 3) {
                return mavenVersion;
            } else {
                return stringJoin(Arrays.asList(parts).subList(0, 3), ".");
            }
        }
    }

    static String stringJoin(Collection<String> elements, String sep) {
        StringBuilder builder = new StringBuilder();
        Iterator<String> i = elements.iterator();

        if (i.hasNext())
            builder.append(i.next());

        while(i.hasNext()) {
            builder.append(sep).append(i.next());
        }

        return builder.toString();
    }
}