Search in sources :

Example 1 with ResourceTransformer

use of org.apache.maven.plugins.shade.resource.ResourceTransformer in project maven-plugins by apache.

the class DefaultShader method resourceTransformed.

private boolean resourceTransformed(List<ResourceTransformer> resourceTransformers, String name, InputStream is, List<Relocator> relocators) throws IOException {
    boolean resourceTransformed = false;
    for (ResourceTransformer transformer : resourceTransformers) {
        if (transformer.canTransformResource(name)) {
            getLogger().debug("Transforming " + name + " using " + transformer.getClass().getName());
            transformer.processResource(name, is, relocators);
            resourceTransformed = true;
            break;
        }
    }
    return resourceTransformed;
}
Also used : ResourceTransformer(org.apache.maven.plugins.shade.resource.ResourceTransformer) ManifestResourceTransformer(org.apache.maven.plugins.shade.resource.ManifestResourceTransformer)

Example 2 with ResourceTransformer

use of org.apache.maven.plugins.shade.resource.ResourceTransformer in project maven-plugins by apache.

the class DefaultShader method shade.

public void shade(ShadeRequest shadeRequest) throws IOException, MojoExecutionException {
    Set<String> resources = new HashSet<String>();
    ResourceTransformer manifestTransformer = null;
    List<ResourceTransformer> transformers = new ArrayList<ResourceTransformer>(shadeRequest.getResourceTransformers());
    for (Iterator<ResourceTransformer> it = transformers.iterator(); it.hasNext(); ) {
        ResourceTransformer transformer = it.next();
        if (transformer instanceof ManifestResourceTransformer) {
            manifestTransformer = transformer;
            it.remove();
        }
    }
    RelocatorRemapper remapper = new RelocatorRemapper(shadeRequest.getRelocators());
    // noinspection ResultOfMethodCallIgnored
    shadeRequest.getUberJar().getParentFile().mkdirs();
    JarOutputStream out = null;
    try {
        out = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(shadeRequest.getUberJar())));
        goThroughAllJarEntriesForManifestTransformer(shadeRequest, resources, manifestTransformer, out);
        // CHECKSTYLE_OFF: MagicNumber
        Multimap<String, File> duplicates = HashMultimap.create(10000, 3);
        // CHECKSTYLE_ON: MagicNumber
        shadeJars(shadeRequest, resources, transformers, remapper, out, duplicates);
        // CHECKSTYLE_OFF: MagicNumber
        Multimap<Collection<File>, String> overlapping = HashMultimap.create(20, 15);
        for (String clazz : duplicates.keySet()) {
            Collection<File> jarz = duplicates.get(clazz);
            if (jarz.size() > 1) {
                overlapping.put(jarz, clazz);
            }
        }
        // Log a summary of duplicates
        logSummaryOfDuplicates(overlapping);
        if (overlapping.keySet().size() > 0) {
            showOverlappingWarning();
        }
        for (ResourceTransformer transformer : transformers) {
            if (transformer.hasTransformedResource()) {
                transformer.modifyOutputStream(out);
            }
        }
        out.close();
        out = null;
    } finally {
        IOUtil.close(out);
    }
    for (Filter filter : shadeRequest.getFilters()) {
        filter.finished();
    }
}
Also used : ArrayList(java.util.ArrayList) JarOutputStream(java.util.jar.JarOutputStream) Filter(org.apache.maven.plugins.shade.filter.Filter) FileOutputStream(java.io.FileOutputStream) Collection(java.util.Collection) ManifestResourceTransformer(org.apache.maven.plugins.shade.resource.ManifestResourceTransformer) ResourceTransformer(org.apache.maven.plugins.shade.resource.ResourceTransformer) ManifestResourceTransformer(org.apache.maven.plugins.shade.resource.ManifestResourceTransformer) BufferedOutputStream(java.io.BufferedOutputStream) JarFile(java.util.jar.JarFile) File(java.io.File) HashSet(java.util.HashSet)

Example 3 with ResourceTransformer

use of org.apache.maven.plugins.shade.resource.ResourceTransformer 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 4 with ResourceTransformer

use of org.apache.maven.plugins.shade.resource.ResourceTransformer 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 5 with ResourceTransformer

use of org.apache.maven.plugins.shade.resource.ResourceTransformer 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);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ShadeRequest(org.apache.maven.plugins.shade.ShadeRequest) SimpleRelocator(org.apache.maven.plugins.shade.relocation.SimpleRelocator) Relocator(org.apache.maven.plugins.shade.relocation.Relocator) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) DependencyGraphBuilderException(org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException) ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) ContextException(org.codehaus.plexus.context.ContextException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) SimpleFilter(org.apache.maven.plugins.shade.filter.SimpleFilter) Filter(org.apache.maven.plugins.shade.filter.Filter) MinijarFilter(org.apache.maven.plugins.shade.filter.MinijarFilter) ResourceTransformer(org.apache.maven.plugins.shade.resource.ResourceTransformer) File(java.io.File)

Aggregations

ResourceTransformer (org.apache.maven.plugins.shade.resource.ResourceTransformer)8 File (java.io.File)7 Filter (org.apache.maven.plugins.shade.filter.Filter)7 ArrayList (java.util.ArrayList)6 LinkedHashSet (java.util.LinkedHashSet)6 Relocator (org.apache.maven.plugins.shade.relocation.Relocator)6 SimpleRelocator (org.apache.maven.plugins.shade.relocation.SimpleRelocator)6 ComponentsXmlResourceTransformer (org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer)5 URLClassLoader (java.net.URLClassLoader)3 ShadeRequest (org.apache.maven.plugins.shade.ShadeRequest)3 Shader (org.apache.maven.plugins.shade.Shader)2 ManifestResourceTransformer (org.apache.maven.plugins.shade.resource.ManifestResourceTransformer)2 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 JarFile (java.util.jar.JarFile)1