aboutsummaryrefslogtreecommitdiffstats
path: root/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/GenerateBundleClassPathMappingsMojo.java
blob: 4d5b0e327d52387875b7b498810674124ad307ca (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
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.plugin.mojo;

import com.google.common.base.Preconditions;
import com.yahoo.container.plugin.bundle.AnalyzeBundle;
import com.yahoo.container.plugin.osgi.ProjectBundleClassPaths;
import com.yahoo.container.plugin.osgi.ProjectBundleClassPaths.BundleClasspathMapping;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
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;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Generates mapping from Bundle-SymbolicName to classpath elements, e.g myBundle -> [.m2/repository/com/mylib/Mylib.jar,
 * myBundleProject/target/classes] The mapping in stored in a json file.
 *
 * @author Tony Vaagenes
 * @author ollivir
 */
@Mojo(name = "generate-bundle-classpath-mappings", requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, threadSafe = true)
public class GenerateBundleClassPathMappingsMojo extends AbstractMojo {
    @Parameter(defaultValue = "${project}")
    private MavenProject project = null;

    //TODO: Combine with com.yahoo.container.plugin.mojo.GenerateOsgiManifestMojo.bundleSymbolicName
    @Parameter(alias = "Bundle-SymbolicName", defaultValue = "${project.artifactId}")
    private String bundleSymbolicName = null;

    /* Sample output -- target/test-classes/bundle-plugin.bundle-classpath-mappings.json
    {
      "mainBundle": {
          "bundleSymbolicName": "bundle-plugin-test",
          "classPathElements": [
              "/Users/tonyv/Repos/vespa/bundle-plugin-test/target/classes",
              "/Users/tonyv/.m2/repository/com/yahoo/vespa/jrt/7-SNAPSHOT/jrt-7-SNAPSHOT.jar",
              "/Users/tonyv/.m2/repository/com/yahoo/vespa/annotations/7-SNAPSHOT/annotations-7-SNAPSHOT.jar"
          ]
      },
      "providedDependencies": [
          {
              "bundleSymbolicName": "jrt",
              "classPathElements": [
                  "/Users/tonyv/.m2/repository/com/yahoo/vespa/jrt/7-SNAPSHOT/jrt-7-SNAPSHOT.jar"
              ]
          }
      ]
     }
    */
    @Override
    public void execute() throws MojoExecutionException {
        Preconditions.checkNotNull(bundleSymbolicName);

        Artifacts.ArtifactSet artifacts = Artifacts.getArtifacts(project);
        List<Artifact> embeddedArtifacts = artifacts.getJarArtifactsToInclude();
        List<Artifact> providedJarArtifacts = artifacts.getJarArtifactsProvided();

        List<File> embeddedArtifactsFiles = embeddedArtifacts.stream().map(Artifact::getFile).collect(Collectors.toList());

        List<String> classPathElements = Stream.concat(Stream.of(outputDirectory()), embeddedArtifactsFiles.stream())
                .map(File::getAbsolutePath).collect(Collectors.toList());

        ProjectBundleClassPaths classPathMappings = new ProjectBundleClassPaths(
                new BundleClasspathMapping(bundleSymbolicName, classPathElements),
                providedJarArtifacts.stream().map(f -> createDependencyClasspathMapping(f)).filter(Optional::isPresent).map(Optional::get)
                        .collect(Collectors.toList()));

        try {
            ProjectBundleClassPaths.save(testOutputPath().resolve(ProjectBundleClassPaths.CLASSPATH_MAPPINGS_FILENAME), classPathMappings);
        } catch (IOException e) {
            throw new MojoExecutionException("Error saving to file " + testOutputPath(), e);
        }
    }

    private File outputDirectory() {
        return new File(project.getBuild().getOutputDirectory());
    }

    private Path testOutputPath() {
        return Paths.get(project.getBuild().getTestOutputDirectory());
    }

    /* TODO:
     * 1) add the dependencies of the artifact in the future(i.e. dependencies of dependencies)
     * or
     * 2) obtain bundles with embedded dependencies from the maven repository,
     *     and support loading classes from the nested jar files in those bundles.
     */
    Optional<BundleClasspathMapping> createDependencyClasspathMapping(Artifact artifact) {
        return bundleSymbolicNameForArtifact(artifact)
                .map(name -> new BundleClasspathMapping(name, Arrays.asList(artifact.getFile().getAbsolutePath())));
    }

    private static Optional<String> bundleSymbolicNameForArtifact(Artifact artifact) {
        if (artifact.getFile().getName().endsWith(".jar")) {
            return AnalyzeBundle.bundleSymbolicName(artifact.getFile());
        } else {
            // Not the best heuristic. The other alternatives are parsing the pom file or
            // storing information in target/classes when building the provided bundles.
            return Optional.of(artifact.getArtifactId());
        }
    }
}