blob: 05b679f3bfe6fca01369b31547b391d7b20c2143 (
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
|
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;
/**
* @author gjoranv
*/
@Mojo(name = "generate-provided-artifact-manifest", requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true)
public class GenerateProvidedArtifactManifestMojo extends AbstractMojo {
@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("X-JDisc-Provided-Artifact", 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);
}
}
}
|