aboutsummaryrefslogtreecommitdiffstats
path: root/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/GenerateProvidedArtifactManifestMojo.java
blob: 17cd063667cc7544a1a3e8e3c4ba40df4ef0f464 (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
// Copyright Vespa.ai. 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.commons.io.FileUtils;
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.plugins.shade.DefaultShader;
import org.apache.maven.plugins.shade.ShadeRequest;
import org.apache.maven.plugins.shade.relocation.Relocator;
import org.apache.maven.plugins.shade.resource.ResourceTransformer;
import org.apache.maven.project.MavenProject;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;

/**
 * Replaces the Class-Path of a jar file manifest with a list of provided artifacts in a new manifest entry.
 * The Class-Path is used as input because it is trivial to generate with the maven-jar-plugin.
 *
 * @author gjoranv
 */
@Mojo(name = "generate-provided-artifact-manifest", requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true)
public class GenerateProvidedArtifactManifestMojo extends AbstractMojo {

    public static final String PROVIDED_ARTIFACTS_MANIFEST_ENTRY = "X-JDisc-Provided-Artifact";

    @Parameter(defaultValue = "${project}")
    public MavenProject project;

    @Parameter(defaultValue = "${project.build.directory}")
    public File outputDirectory;


    @Override
    public void execute() throws MojoExecutionException {
        var originalJar = project.getArtifact().getFile();
        var shadedJar = shadedJarFile();

        var req = new ShadeRequest();
        req.setJars(Set.of(originalJar));
        req.setUberJar(shadedJar);
        req.setResourceTransformers(List.of(new ProvidedArtifactsManifestUpdater()));
        req.setRelocators(List.of());
        req.setFilters(List.of());

        try {
            new DefaultShader().shade(req);
        } catch (IOException e) {
            throw new MojoExecutionException(e);
        }
        try {
            getLog().info("Replacing original jar with transformed jar");
            FileUtils.copyFile(shadedJar, originalJar);
        } catch (IOException e) {
            throw new MojoExecutionException(e);
        }
    }

    private File shadedJarFile() {
        var a = project.getArtifact();
        var name = project.getArtifactId() + "-shaded." + a.getArtifactHandler().getExtension();
        return new File(outputDirectory, name);
    }

    private static class ProvidedArtifactsManifestUpdater implements ResourceTransformer {

        private Manifest manifest;

        @Override
        public boolean canTransformResource(String resource) {
            return JarFile.MANIFEST_NAME.equalsIgnoreCase(resource);
        }

        @SuppressWarnings("deprecation")
        @Override
        public void processResource(String resource, InputStream is, List<Relocator> relocators) throws IOException {
            manifest = new Manifest(is);
            Attributes attributes = manifest.getMainAttributes();
            var providedArtifacts = attributes.getValue("Class-Path");
            if (providedArtifacts == null) return;

            attributes.remove(new Attributes.Name("Class-Path"));
            attributes.putValue(PROVIDED_ARTIFACTS_MANIFEST_ENTRY, providedArtifacts.replace(" ", ","));
            attributes.putValue("Created-By", "vespa container maven plugin");
        }

        @Override
        public boolean hasTransformedResource() {
            return true;
        }

        @Override
        public void modifyOutputStream(JarOutputStream os) throws IOException {
            if (manifest == null) return;

            JarEntry jarEntry = new JarEntry(JarFile.MANIFEST_NAME);
            os.putNextEntry(jarEntry);
            manifest.write(os);
        }
    }

}