use of org.apache.maven.plugins.shade.relocation.Relocator in project maven-plugins by apache.
the class ServicesResourceTransformer method modifyOutputStream.
public void modifyOutputStream(JarOutputStream jos) throws IOException {
for (Map.Entry<String, ServiceStream> entry : serviceEntries.entrySet()) {
String key = entry.getKey();
ServiceStream data = entry.getValue();
if (relocators != null) {
key = key.substring(SERVICES_PATH.length() + 1);
for (Relocator relocator : relocators) {
if (relocator.canRelocateClass(key)) {
key = relocator.relocateClass(key);
break;
}
}
key = SERVICES_PATH + '/' + key;
}
jos.putNextEntry(new JarEntry(key));
//read the content of service file for candidate classes for relocation
PrintWriter writer = new PrintWriter(jos);
InputStreamReader streamReader = new InputStreamReader(data.toInputStream());
BufferedReader reader = new BufferedReader(streamReader);
String className;
while ((className = reader.readLine()) != null) {
if (relocators != null) {
for (Relocator relocator : relocators) {
//if the class can be relocated then relocate it
if (relocator.canRelocateClass(className)) {
className = relocator.applyToSourceContent(className);
break;
}
}
}
writer.println(className);
writer.flush();
}
reader.close();
data.reset();
}
}
use of org.apache.maven.plugins.shade.relocation.Relocator in project maven-plugins by apache.
the class DefaultShaderTest method shaderWithPattern.
private void shaderWithPattern(String shadedPattern, File jar, String[] excludes) throws Exception {
DefaultShader s = newShader();
Set<File> set = new LinkedHashSet<File>();
set.add(new File("src/test/jars/test-project-1.0-SNAPSHOT.jar"));
set.add(new File("src/test/jars/plexus-utils-1.4.1.jar"));
List<Relocator> relocators = new ArrayList<Relocator>();
relocators.add(new SimpleRelocator("org/codehaus/plexus/util", shadedPattern, null, Arrays.asList(excludes)));
List<ResourceTransformer> resourceTransformers = new ArrayList<ResourceTransformer>();
resourceTransformers.add(new ComponentsXmlResourceTransformer());
List<Filter> filters = new ArrayList<Filter>();
ShadeRequest shadeRequest = new ShadeRequest();
shadeRequest.setJars(set);
shadeRequest.setUberJar(jar);
shadeRequest.setFilters(filters);
shadeRequest.setRelocators(relocators);
shadeRequest.setResourceTransformers(resourceTransformers);
s.shade(shadeRequest);
}
use of org.apache.maven.plugins.shade.relocation.Relocator in project maven-plugins by apache.
the class DefaultShaderTest method testShaderWithStaticInitializedClass.
public void testShaderWithStaticInitializedClass() throws Exception {
Shader s = newShader();
Set<File> set = new LinkedHashSet<File>();
set.add(new File("src/test/jars/test-artifact-1.0-SNAPSHOT.jar"));
List<Relocator> relocators = new ArrayList<Relocator>();
relocators.add(new SimpleRelocator("org.apache.maven.plugins.shade", null, null, null));
List<ResourceTransformer> resourceTransformers = new ArrayList<ResourceTransformer>();
List<Filter> filters = new ArrayList<Filter>();
File file = new File("target/testShaderWithStaticInitializedClass.jar");
ShadeRequest shadeRequest = new ShadeRequest();
shadeRequest.setJars(set);
shadeRequest.setUberJar(file);
shadeRequest.setFilters(filters);
shadeRequest.setRelocators(relocators);
shadeRequest.setResourceTransformers(resourceTransformers);
s.shade(shadeRequest);
URLClassLoader cl = new URLClassLoader(new URL[] { file.toURI().toURL() });
Class<?> c = cl.loadClass("hidden.org.apache.maven.plugins.shade.Lib");
Object o = c.newInstance();
assertEquals("foo.bar/baz", c.getDeclaredField("CONSTANT").get(o));
}
use of org.apache.maven.plugins.shade.relocation.Relocator in project maven-plugins by apache.
the class DefaultShader method addJavaSource.
private void addJavaSource(Set<String> resources, JarOutputStream jos, String name, InputStream is, List<Relocator> relocators) throws IOException {
jos.putNextEntry(new JarEntry(name));
String sourceContent = IOUtil.toString(new InputStreamReader(is, "UTF-8"));
for (Relocator relocator : relocators) {
sourceContent = relocator.applyToSourceContent(sourceContent);
}
final Writer writer = new OutputStreamWriter(jos, "UTF-8");
IOUtil.copy(sourceContent, writer);
writer.flush();
resources.add(name);
}
use of org.apache.maven.plugins.shade.relocation.Relocator in project maven-plugins by apache.
the class ShadeMojo method execute.
/**
* @throws MojoExecutionException
*/
public void execute() throws MojoExecutionException {
setupHintedShader();
Set<File> artifacts = new LinkedHashSet<File>();
Set<String> artifactIds = new LinkedHashSet<String>();
Set<File> sourceArtifacts = new LinkedHashSet<File>();
Set<File> testArtifacts = new LinkedHashSet<File>();
ArtifactSelector artifactSelector = new ArtifactSelector(project.getArtifact(), artifactSet, shadedGroupFilter);
if (artifactSelector.isSelected(project.getArtifact()) && !"pom".equals(project.getArtifact().getType())) {
if (invalidMainArtifact()) {
createErrorOutput();
throw new MojoExecutionException("Failed to create shaded artifact, " + "project main artifact does not exist.");
}
artifacts.add(project.getArtifact().getFile());
if (createSourcesJar) {
File file = shadedSourcesArtifactFile();
if (file.isFile()) {
sourceArtifacts.add(file);
}
}
if (shadeTestJar) {
File file = shadedTestArtifactFile();
if (file.isFile()) {
testArtifacts.add(file);
}
}
}
processArtifactSelectors(artifacts, artifactIds, sourceArtifacts, artifactSelector);
File outputJar = (outputFile != null) ? outputFile : shadedArtifactFileWithClassifier();
File sourcesJar = shadedSourceArtifactFileWithClassifier();
File testJar = shadedTestArtifactFileWithClassifier();
// Now add our extra resources
try {
List<Filter> filters = getFilters();
List<Relocator> relocators = getRelocators();
List<ResourceTransformer> resourceTransformers = getResourceTransformers();
ShadeRequest shadeRequest = shadeRequest(artifacts, outputJar, filters, relocators, resourceTransformers);
shader.shade(shadeRequest);
if (createSourcesJar) {
ShadeRequest shadeSourcesRequest = createShadeSourcesRequest(sourceArtifacts, sourcesJar, filters, relocators, resourceTransformers);
shader.shade(shadeSourcesRequest);
}
if (shadeTestJar) {
ShadeRequest shadeSourcesRequest = createShadeSourcesRequest(testArtifacts, testJar, filters, relocators, resourceTransformers);
shader.shade(shadeSourcesRequest);
}
if (outputFile == null) {
boolean renamed = false;
// because this will be handled implicitly later
if (//
finalName != null && finalName.length() > 0 && !finalName.equals(project.getBuild().getFinalName())) {
String finalFileName = finalName + "." + project.getArtifact().getArtifactHandler().getExtension();
File finalFile = new File(outputDirectory, finalFileName);
replaceFile(finalFile, outputJar);
outputJar = finalFile;
// Also support the sources JAR
if (createSourcesJar) {
finalFileName = finalName + "-sources.jar";
finalFile = new File(outputDirectory, finalFileName);
replaceFile(finalFile, sourcesJar);
sourcesJar = finalFile;
}
// Also support the test JAR
if (shadeTestJar) {
finalFileName = finalName + "-tests.jar";
finalFile = new File(outputDirectory, finalFileName);
replaceFile(finalFile, testJar);
testJar = finalFile;
}
renamed = true;
}
if (shadedArtifactAttached) {
getLog().info("Attaching shaded artifact.");
projectHelper.attachArtifact(project, project.getArtifact().getType(), shadedClassifierName, outputJar);
if (createSourcesJar) {
projectHelper.attachArtifact(project, "java-source", shadedClassifierName + "-sources", sourcesJar);
}
} else if (!renamed) {
getLog().info("Replacing original artifact with shaded artifact.");
File originalArtifact = project.getArtifact().getFile();
if (originalArtifact != null) {
replaceFile(originalArtifact, outputJar);
if (createSourcesJar) {
getLog().info("Replacing original source artifact with shaded source artifact.");
File shadedSources = shadedSourcesArtifactFile();
replaceFile(shadedSources, sourcesJar);
projectHelper.attachArtifact(project, "java-source", "sources", shadedSources);
}
if (shadeTestJar) {
getLog().info("Replacing original test artifact with shaded test artifact.");
File shadedTests = shadedTestArtifactFile();
replaceFile(shadedTests, testJar);
projectHelper.attachArtifact(project, "jar", "tests", shadedTests);
}
if (createDependencyReducedPom) {
createDependencyReducedPom(artifactIds);
}
}
}
}
} catch (Exception e) {
throw new MojoExecutionException("Error creating shaded jar: " + e.getMessage(), e);
}
}
Aggregations