Search in sources :

Example 1 with Relocator

use of org.apache.maven.plugins.shade.relocation.Relocator in project maven-plugins by apache.

the class ServicesResourceTransformer method processResource.

public void processResource(String resource, InputStream is, final List<Relocator> relocators) throws IOException {
    ServiceStream out = serviceEntries.get(resource);
    if (out == null) {
        out = new ServiceStream();
        serviceEntries.put(resource, out);
    }
    final ServiceStream fout = out;
    final String content = IOUtils.toString(is);
    StringReader reader = new StringReader(content);
    LineReader lineReader = new LineReader(reader);
    String line;
    while ((line = lineReader.readLine()) != null) {
        String relContent = line;
        for (Relocator relocator : relocators) {
            if (relocator.canRelocateClass(relContent)) {
                relContent = relocator.applyToSourceContent(relContent);
            }
        }
        fout.append(relContent + "\n");
    }
    if (this.relocators == null) {
        this.relocators = relocators;
    }
}
Also used : LineReader(com.google.common.io.LineReader) StringReader(java.io.StringReader) Relocator(org.apache.maven.plugins.shade.relocation.Relocator)

Example 2 with Relocator

use of org.apache.maven.plugins.shade.relocation.Relocator in project maven-plugins by apache.

the class DefaultShaderTest method testShaderWithRelocatedClassname.

public void testShaderWithRelocatedClassname() 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/", "_plexus/util/__", null, Arrays.<String>asList()));
    List<ResourceTransformer> resourceTransformers = new ArrayList<ResourceTransformer>();
    resourceTransformers.add(new ComponentsXmlResourceTransformer());
    List<Filter> filters = new ArrayList<Filter>();
    File file = new File("target/foo-relocate-class.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("_plexus.util.__StringUtils");
    // first, ensure it works:
    Object o = c.newInstance();
    assertEquals("", c.getMethod("clean", String.class).invoke(o, (String) null));
    // now, check that its source file was rewritten:
    final String[] source = { null };
    final ClassReader classReader = new ClassReader(cl.getResourceAsStream("_plexus/util/__StringUtils.class"));
    classReader.accept(new ClassVisitor(Opcodes.ASM4) {

        @Override
        public void visitSource(String arg0, String arg1) {
            super.visitSource(arg0, arg1);
            source[0] = arg0;
        }
    }, ClassReader.SKIP_CODE);
    assertEquals("__StringUtils.java", source[0]);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) ClassVisitor(org.objectweb.asm.ClassVisitor) Relocator(org.apache.maven.plugins.shade.relocation.Relocator) SimpleRelocator(org.apache.maven.plugins.shade.relocation.SimpleRelocator) SimpleRelocator(org.apache.maven.plugins.shade.relocation.SimpleRelocator) Filter(org.apache.maven.plugins.shade.filter.Filter) URLClassLoader(java.net.URLClassLoader) ComponentsXmlResourceTransformer(org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer) ClassReader(org.objectweb.asm.ClassReader) ResourceTransformer(org.apache.maven.plugins.shade.resource.ResourceTransformer) ComponentsXmlResourceTransformer(org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer) File(java.io.File)

Example 3 with Relocator

use of org.apache.maven.plugins.shade.relocation.Relocator in project maven-plugins by apache.

the class ShadeMojoTest method shaderWithPattern.

public void shaderWithPattern(String shadedPattern, File jar) throws Exception {
    Shader s = (Shader) lookup(Shader.ROLE);
    Set<File> set = new LinkedHashSet<File>();
    set.add(new File(getBasedir(), "src/test/jars/test-project-1.0-SNAPSHOT.jar"));
    set.add(new File(getBasedir(), "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("org/codehaus/plexus/util/xml/Xpp3Dom", "org/codehaus/plexus/util/xml/pull.*")));
    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);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) Shader(org.apache.maven.plugins.shade.Shader) ShadeRequest(org.apache.maven.plugins.shade.ShadeRequest) SimpleRelocator(org.apache.maven.plugins.shade.relocation.SimpleRelocator) Relocator(org.apache.maven.plugins.shade.relocation.Relocator) SimpleRelocator(org.apache.maven.plugins.shade.relocation.SimpleRelocator) Filter(org.apache.maven.plugins.shade.filter.Filter) ComponentsXmlResourceTransformer(org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer) ResourceTransformer(org.apache.maven.plugins.shade.resource.ResourceTransformer) ComponentsXmlResourceTransformer(org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer) File(java.io.File)

Example 4 with Relocator

use of org.apache.maven.plugins.shade.relocation.Relocator in project maven-plugins by apache.

the class ApacheNoticeResourceTransformerParameterTests method processAndFailOnNullPointer.

private void processAndFailOnNullPointer(final String noticeText) throws IOException {
    try {
        final ByteArrayInputStream noticeInputStream = new ByteArrayInputStream(noticeText.getBytes());
        final List<Relocator> emptyList = Collections.emptyList();
        subject.processResource(NOTICE_RESOURCE, noticeInputStream, emptyList);
        noticeInputStream.close();
    } catch (NullPointerException e) {
        fail("Null pointer should not be thrown when no parameters are set.");
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Relocator(org.apache.maven.plugins.shade.relocation.Relocator)

Example 5 with Relocator

use of org.apache.maven.plugins.shade.relocation.Relocator in project maven-plugins by apache.

the class ServiceResourceTransformerTest method relocatedClasses.

@Test
public void relocatedClasses() throws Exception {
    SimpleRelocator relocator = new SimpleRelocator("org.foo", "borg.foo", null, Arrays.asList("org.foo.exclude.*"));
    List<Relocator> relocators = Lists.<Relocator>newArrayList(relocator);
    String content = "org.foo.Service\norg.foo.exclude.OtherService\n";
    byte[] contentBytes = content.getBytes("UTF-8");
    InputStream contentStream = new ByteArrayInputStream(contentBytes);
    String contentResource = "META-INF/services/org.foo.something.another";
    String contentResourceShaded = "META-INF/services/borg.foo.something.another";
    ServicesResourceTransformer xformer = new ServicesResourceTransformer();
    xformer.processResource(contentResource, contentStream, relocators);
    contentStream.close();
    File tempJar = File.createTempFile("shade.", ".jar");
    tempJar.deleteOnExit();
    FileOutputStream fos = new FileOutputStream(tempJar);
    JarOutputStream jos = new JarOutputStream(fos);
    try {
        xformer.modifyOutputStream(jos);
        jos.close();
        jos = null;
        JarFile jarFile = new JarFile(tempJar);
        JarEntry jarEntry = jarFile.getJarEntry(contentResourceShaded);
        assertNotNull(jarEntry);
        InputStream entryStream = jarFile.getInputStream(jarEntry);
        try {
            String xformedContent = IOUtils.toString(entryStream, "utf-8");
            assertEquals("borg.foo.Service" + System.getProperty("line.separator") + "org.foo.exclude.OtherService" + System.getProperty("line.separator"), xformedContent);
        } finally {
            IOUtils.closeQuietly(entryStream);
            jarFile.close();
        }
    } finally {
        if (jos != null) {
            IOUtils.closeQuietly(jos);
        }
        tempJar.delete();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JarOutputStream(java.util.jar.JarOutputStream) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) Relocator(org.apache.maven.plugins.shade.relocation.Relocator) SimpleRelocator(org.apache.maven.plugins.shade.relocation.SimpleRelocator) SimpleRelocator(org.apache.maven.plugins.shade.relocation.SimpleRelocator) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) JarFile(java.util.jar.JarFile) File(java.io.File) Test(org.junit.Test)

Aggregations

Relocator (org.apache.maven.plugins.shade.relocation.Relocator)15 File (java.io.File)8 SimpleRelocator (org.apache.maven.plugins.shade.relocation.SimpleRelocator)8 LinkedHashSet (java.util.LinkedHashSet)6 Filter (org.apache.maven.plugins.shade.filter.Filter)6 ResourceTransformer (org.apache.maven.plugins.shade.resource.ResourceTransformer)6 ArrayList (java.util.ArrayList)5 JarEntry (java.util.jar.JarEntry)5 ComponentsXmlResourceTransformer (org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStreamReader (java.io.InputStreamReader)3 URLClassLoader (java.net.URLClassLoader)3 ShadeRequest (org.apache.maven.plugins.shade.ShadeRequest)3 BufferedReader (java.io.BufferedReader)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 PrintWriter (java.io.PrintWriter)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2