Package org.apache.maven.plugins.shade.resource

Examples of org.apache.maven.plugins.shade.resource.ResourceTransformer


    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();
        FileOutputStream fileOutputStream = new FileOutputStream( shadeRequest.getUberJar() );
        JarOutputStream jos = new JarOutputStream( new BufferedOutputStream( fileOutputStream ) );

        if ( manifestTransformer != null )
        {
            for ( File jar : shadeRequest.getJars() )
            {
                JarFile jarFile = newJarFile( jar );
                for ( Enumeration<JarEntry> en = jarFile.entries(); en.hasMoreElements(); )
                {
                    JarEntry entry = en.nextElement();
                    String resource = entry.getName();
                    if ( manifestTransformer.canTransformResource( resource ) )
                    {
                        resources.add( resource );
                        manifestTransformer.processResource( resource, jarFile.getInputStream( entry ),
                                                             shadeRequest.getRelocators() );
                        break;
                    }
                }
            }
            if ( manifestTransformer.hasTransformedResource() )
            {
                manifestTransformer.modifyOutputStream( jos );
            }
        }

        Multimap<String, File> duplicates = HashMultimap.create( 10000, 3 );

        for ( File jar : shadeRequest.getJars() )
        {

            getLogger().debug( "Processing JAR " + jar );

            List<Filter> jarFilters = getFilters( jar, shadeRequest.getFilters() );

            JarFile jarFile = newJarFile( jar );

            for ( Enumeration<JarEntry> j = jarFile.entries(); j.hasMoreElements(); )
            {
                JarEntry entry = j.nextElement();

                String name = entry.getName();

                if ( "META-INF/INDEX.LIST".equals( name ) )
                {
                    // we cannot allow the jar indexes to be copied over or the
                    // jar is useless. Ideally, we could create a new one
                    // later
                    continue;
                }

                if ( !entry.isDirectory() && !isFiltered( jarFilters, name ) )
                {
                    InputStream is = jarFile.getInputStream( entry );

                    String mappedName = remapper.map( name );

                    int idx = mappedName.lastIndexOf( '/' );
                    if ( idx != -1 )
                    {
                        // make sure dirs are created
                        String dir = mappedName.substring( 0, idx );
                        if ( !resources.contains( dir ) )
                        {
                            addDirectory( resources, jos, dir );
                        }
                    }

                    if ( name.endsWith( ".class" ) )
                    {
                        duplicates.put( name, jar );
                        addRemappedClass( remapper, jos, jar, name, is );
                    }
                    else if ( shadeRequest.isShadeSourcesContent() && name.endsWith( ".java" ) )
                    {
                        // Avoid duplicates
                        if ( resources.contains( mappedName ) )
                        {
                            continue;
                        }

                        addJavaSource( resources, jos, mappedName, is, shadeRequest.getRelocators() );
                    }
                    else
                    {
                        if ( !resourceTransformed( transformers, mappedName, is, shadeRequest.getRelocators() ) )
                        {
                            // Avoid duplicates that aren't accounted for by the resource transformers
                            if ( resources.contains( mappedName ) )
                            {
                                continue;
                            }

                            addResource( resources, jos, mappedName, is );
                        }
                    }

                    IOUtil.close( is );
                }
            }

            jarFile.close();
        }

        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
        for ( Collection<File> jarz : overlapping.keySet() )
        {
            List<String> jarzS = new LinkedList<String>();

            for ( File jjar : jarz )
                jarzS.add( jjar.getName() );

            List<String> classes = new LinkedList<String>();

            for ( String clazz : overlapping.get( jarz ) )
                classes.add( clazz.replace( ".class", "" ).replace( "/", "." ) );

            getLogger().warn( Joiner.on( ", " ).join( jarzS ) + " define " + classes.size() + " overlappping classes: " );

            int max = 10;

            for ( int i = 0; i < Math.min( max, classes.size() ); i++ )
                getLogger().warn( "  - " + classes.get( i ) );

            if ( classes.size() > max )
                getLogger().warn( "  - " + ( classes.size() - max ) + " more..." );

        }

        if ( overlapping.keySet().size() > 0 )
        {
            getLogger().warn( "maven-shade-plugin has detected that some .class files" );
            getLogger().warn( "are present in two or more JARs. When this happens, only" );
            getLogger().warn( "one single version of the class is copied in the uberjar." );
            getLogger().warn( "Usually this is not harmful and you can skeep these" );
            getLogger().warn( "warnings, otherwise try to manually exclude artifacts" );
            getLogger().warn( "based on mvn dependency:tree -Ddetail=true and the above" );
            getLogger().warn( "output" );
            getLogger().warn( "See http://docs.codehaus.org/display/MAVENUSER/Shade+Plugin" );
        }

        for ( ResourceTransformer transformer : transformers )
        {
            if ( transformer.hasTransformedResource() )
            {
                transformer.modifyOutputStream( jos );
            }
        }

        IOUtil.close( jos );
View Full Code Here


    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() );

        shadeRequest.getUberJar().getParentFile().mkdirs();
        JarOutputStream jos = new JarOutputStream( new FileOutputStream( shadeRequest.getUberJar() ) );

        if ( manifestTransformer != null )
        {
            for ( File jar : shadeRequest.getJars() )
            {
                JarFile jarFile = newJarFile( jar );
                for ( Enumeration<JarEntry> en = jarFile.entries(); en.hasMoreElements(); )
                {
                    JarEntry entry = en.nextElement();
                    String resource = entry.getName();
                    if ( manifestTransformer.canTransformResource( resource ) )
                    {
                        resources.add( resource );
                        manifestTransformer.processResource( resource, jarFile.getInputStream( entry ), shadeRequest.getRelocators() );
                        break;
                    }
                }
            }
            if ( manifestTransformer.hasTransformedResource() )
            {
                manifestTransformer.modifyOutputStream( jos );
            }
        }

        for ( File jar : shadeRequest.getJars() )
        {

            getLogger().debug( "Processing JAR " + jar );

            List<Filter> jarFilters = getFilters( jar, shadeRequest.getFilters() );

            JarFile jarFile = newJarFile( jar );

            for ( Enumeration<JarEntry> j = jarFile.entries(); j.hasMoreElements(); )
            {
                JarEntry entry = j.nextElement();

                String name = entry.getName();

                if ( "META-INF/INDEX.LIST".equals( name ) )
                {
                    // we cannot allow the jar indexes to be copied over or the
                    // jar is useless. Ideally, we could create a new one
                    // later
                    continue;
                }

                if ( !entry.isDirectory() && !isFiltered( jarFilters, name ) )
                {
                    InputStream is = jarFile.getInputStream( entry );

                    String mappedName = remapper.map( name );

                    int idx = mappedName.lastIndexOf( '/' );
                    if ( idx != -1 )
                    {
                        // make sure dirs are created
                        String dir = mappedName.substring( 0, idx );
                        if ( !resources.contains( dir ) )
                        {
                            addDirectory( resources, jos, dir );
                        }
                    }

                    if ( name.endsWith( ".class" ) )
                    {
                        addRemappedClass( remapper, jos, jar, name, is );
                    }
                    else if ( shadeRequest.isShadeSourcesContent() && name.endsWith( ".java" ) )
                    {
                        // Avoid duplicates
                        if ( resources.contains( mappedName ) )
                        {
                            continue;
                        }
                       
                        addJavaSource( resources, jos, mappedName, is, shadeRequest.getRelocators() );
                    }
                    else
                    {
                        if ( !resourceTransformed( transformers, mappedName, is, shadeRequest.getRelocators() ) )
                        {
                            // Avoid duplicates that aren't accounted for by the resource transformers
                            if ( resources.contains( mappedName ) )
                            {
                                continue;
                            }

                            addResource( resources, jos, mappedName, is );
                        }
                    }

                    IOUtil.close( is );
                }
            }

            jarFile.close();
        }

        for ( ResourceTransformer transformer : transformers )
        {
            if ( transformer.hasTransformedResource() )
            {
                transformer.modifyOutputStream( jos );
            }
        }

        IOUtil.close( jos );
View Full Code Here

                       List<ResourceTransformer> resourceTransformers )
        throws IOException, MojoExecutionException
    {
        Set resources = new HashSet();

        ResourceTransformer manifestTransformer = null;
        List transformers = new ArrayList( resourceTransformers );
        for ( Iterator<ResourceTransformer> it = transformers.iterator(); it.hasNext(); )
        {
            ResourceTransformer transformer = it.next();
            if ( transformer instanceof ManifestResourceTransformer )
            {
                manifestTransformer = transformer;
                it.remove();
            }
        }

        RelocatorRemapper remapper = new RelocatorRemapper( relocators );

        uberJar.getParentFile().mkdirs();
        JarOutputStream jos = new JarOutputStream( new FileOutputStream( uberJar ) );

        if ( manifestTransformer != null )
        {
            for ( File jar : jars )
            {
                JarFile jarFile = newJarFile( jar );
                for ( Enumeration en = jarFile.entries(); en.hasMoreElements(); )
                {
                    JarEntry entry = (JarEntry) en.nextElement();
                    String resource = entry.getName();
                    if ( manifestTransformer.canTransformResource( resource ) )
                    {
                        resources.add( resource );
                        manifestTransformer.processResource( resource, jarFile.getInputStream( entry ), relocators );
                        break;
                    }
                }
            }
            if ( manifestTransformer.hasTransformedResource() )
            {
                manifestTransformer.modifyOutputStream( jos );
            }
        }

        for ( File jar : jars )
        {

            getLogger().debug( "Processing JAR " + jar );

            List jarFilters = getFilters( jar, filters );

            JarFile jarFile = newJarFile( jar );

            for ( Enumeration<JarEntry> j = jarFile.entries(); j.hasMoreElements(); )
            {
                JarEntry entry = j.nextElement();

                String name = entry.getName();

                if ( "META-INF/INDEX.LIST".equals( name ) )
                {
                    // we cannot allow the jar indexes to be copied over or the
                    // jar is useless. Ideally, we could create a new one
                    // later
                    continue;
                }

                if ( !entry.isDirectory() && !isFiltered( jarFilters, name ) )
                {
                    InputStream is = jarFile.getInputStream( entry );

                    String mappedName = remapper.map( name );

                    int idx = mappedName.lastIndexOf( '/' );
                    if ( idx != -1 )
                    {
                        // make sure dirs are created
                        String dir = mappedName.substring( 0, idx );
                        if ( !resources.contains( dir ) )
                        {
                            addDirectory( resources, jos, dir );
                        }
                    }

                    if ( name.endsWith( ".class" ) )
                    {
                        addRemappedClass( remapper, jos, jar, name, is );
                    }
                    else
                    {
                        if ( !resourceTransformed( transformers, mappedName, is, relocators ) )
                        {
                            // Avoid duplicates that aren't accounted for by the resource transformers
                            if ( resources.contains( mappedName ) )
                            {
                                continue;
                            }

                            addResource( resources, jos, mappedName, is );
                        }
                    }

                    IOUtil.close( is );
                }
            }

            jarFile.close();
        }

        for ( Iterator i = transformers.iterator(); i.hasNext(); )
        {
            ResourceTransformer transformer = (ResourceTransformer) i.next();

            if ( transformer.hasTransformedResource() )
            {
                transformer.modifyOutputStream( jos );
            }
        }

        IOUtil.close( jos );
View Full Code Here

    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();
        FileOutputStream fileOutputStream = new FileOutputStream( shadeRequest.getUberJar() );
        JarOutputStream jos = new JarOutputStream( new BufferedOutputStream( fileOutputStream ) );

        if ( manifestTransformer != null )
        {
            for ( File jar : shadeRequest.getJars() )
            {
                JarFile jarFile = newJarFile( jar );
                for ( Enumeration<JarEntry> en = jarFile.entries(); en.hasMoreElements(); )
                {
                    JarEntry entry = en.nextElement();
                    String resource = entry.getName();
                    if ( manifestTransformer.canTransformResource( resource ) )
                    {
                        resources.add( resource );
                        manifestTransformer.processResource( resource, jarFile.getInputStream( entry ),
                                                             shadeRequest.getRelocators() );
                        break;
                    }
                }
            }
            if ( manifestTransformer.hasTransformedResource() )
            {
                manifestTransformer.modifyOutputStream( jos );
            }
        }

        Multimap<String, File> duplicates = HashMultimap.create( 10000, 3 );

        for ( File jar : shadeRequest.getJars() )
        {

            getLogger().debug( "Processing JAR " + jar );

            List<Filter> jarFilters = getFilters( jar, shadeRequest.getFilters() );

            JarFile jarFile = newJarFile( jar );

            for ( Enumeration<JarEntry> j = jarFile.entries(); j.hasMoreElements(); )
            {
                JarEntry entry = j.nextElement();

                String name = entry.getName();

                if ( "META-INF/INDEX.LIST".equals( name ) )
                {
                    // we cannot allow the jar indexes to be copied over or the
                    // jar is useless. Ideally, we could create a new one
                    // later
                    continue;
                }

                if ( !entry.isDirectory() && !isFiltered( jarFilters, name ) )
                {
                    InputStream is = jarFile.getInputStream( entry );

                    String mappedName = remapper.map( name );

                    int idx = mappedName.lastIndexOf( '/' );
                    if ( idx != -1 )
                    {
                        // make sure dirs are created
                        String dir = mappedName.substring( 0, idx );
                        if ( !resources.contains( dir ) )
                        {
                            addDirectory( resources, jos, dir );
                        }
                    }

                    if ( name.endsWith( ".class" ) )
                    {
                        duplicates.put( name, jar );
                        addRemappedClass( remapper, jos, jar, name, is );
                    }
                    else if ( shadeRequest.isShadeSourcesContent() && name.endsWith( ".java" ) )
                    {
                        // Avoid duplicates
                        if ( resources.contains( mappedName ) )
                        {
                            continue;
                        }

                        addJavaSource( resources, jos, mappedName, is, shadeRequest.getRelocators() );
                    }
                    else
                    {
                        if ( !resourceTransformed( transformers, mappedName, is, shadeRequest.getRelocators() ) )
                        {
                            // Avoid duplicates that aren't accounted for by the resource transformers
                            if ( resources.contains( mappedName ) )
                            {
                                continue;
                            }

                            addResource( resources, jos, mappedName, is );
                        }
                    }

                    IOUtil.close( is );
                }
            }

            jarFile.close();
        }

        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
        for ( Collection<File> jarz : overlapping.keySet() )
        {
            List<String> jarzS = new LinkedList<String>();

            for ( File jjar : jarz )
                jarzS.add( jjar.getName() );

            List<String> classes = new LinkedList<String>();

            for ( String clazz : overlapping.get( jarz ) )
                classes.add( clazz.replace( ".class", "" ).replace( "/", "." ) );

            getLogger().warn( Joiner.on( ", " ).join( jarzS ) + " define " + classes.size() + " overlapping classes: " );

            int max = 10;

            for ( int i = 0; i < Math.min( max, classes.size() ); i++ )
                getLogger().warn( "  - " + classes.get( i ) );

            if ( classes.size() > max )
                getLogger().warn( "  - " + ( classes.size() - max ) + " more..." );

        }

        if ( overlapping.keySet().size() > 0 )
        {
            getLogger().warn( "maven-shade-plugin has detected that some class files are" );
            getLogger().warn( "present in two or more JARs. When this happens, only one" );
            getLogger().warn( "single version of the class is copied to the uber jar." );
            getLogger().warn( "Usually this is not harmful and you can skip these warnings," );
            getLogger().warn( "otherwise try to manually exclude artifacts based on" );
            getLogger().warn( "mvn dependency:tree -Ddetail=true and the above output." );
            getLogger().warn( "See http://docs.codehaus.org/display/MAVENUSER/Shade+Plugin" );
        }

        for ( ResourceTransformer transformer : transformers )
        {
            if ( transformer.hasTransformedResource() )
            {
                transformer.modifyOutputStream( jos );
            }
        }

        IOUtil.close( jos );
View Full Code Here

    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();
        FileOutputStream fileOutputStream = new FileOutputStream( shadeRequest.getUberJar() );
        JarOutputStream jos = new JarOutputStream( new BufferedOutputStream( fileOutputStream ) );

        if ( manifestTransformer != null )
        {
            for ( File jar : shadeRequest.getJars() )
            {
                JarFile jarFile = newJarFile( jar );
                for ( Enumeration<JarEntry> en = jarFile.entries(); en.hasMoreElements(); )
                {
                    JarEntry entry = en.nextElement();
                    String resource = entry.getName();
                    if ( manifestTransformer.canTransformResource( resource ) )
                    {
                        resources.add( resource );
                        manifestTransformer.processResource( resource, jarFile.getInputStream( entry ),
                                                             shadeRequest.getRelocators() );
                        break;
                    }
                }
            }
            if ( manifestTransformer.hasTransformedResource() )
            {
                manifestTransformer.modifyOutputStream( jos );
            }
        }

        Multimap<String, File> duplicates = HashMultimap.create( 10000, 3 );
       
        for ( File jar : shadeRequest.getJars() )
        {

            getLogger().debug( "Processing JAR " + jar );

            List<Filter> jarFilters = getFilters( jar, shadeRequest.getFilters() );

            JarFile jarFile = newJarFile( jar );

            for ( Enumeration<JarEntry> j = jarFile.entries(); j.hasMoreElements(); )
            {
                JarEntry entry = j.nextElement();

                String name = entry.getName();

                if ( "META-INF/INDEX.LIST".equals( name ) )
                {
                    // we cannot allow the jar indexes to be copied over or the
                    // jar is useless. Ideally, we could create a new one
                    // later
                    continue;
                }

                if ( !entry.isDirectory() && !isFiltered( jarFilters, name ) )
                {
                    InputStream is = jarFile.getInputStream( entry );

                    String mappedName = remapper.map( name );

                    int idx = mappedName.lastIndexOf( '/' );
                    if ( idx != -1 )
                    {
                        // make sure dirs are created
                        String dir = mappedName.substring( 0, idx );
                        if ( !resources.contains( dir ) )
                        {
                            addDirectory( resources, jos, dir );
                        }
                    }

                    if ( name.endsWith( ".class" ) )
                    {
                      duplicates.put(name, jar);
                        addRemappedClass( remapper, jos, jar, name, is );
                    }
                    else if ( shadeRequest.isShadeSourcesContent() && name.endsWith( ".java" ) )
                    {
                        // Avoid duplicates
                        if ( resources.contains( mappedName ) )
                        {
                            continue;
                        }
                       
                        addJavaSource( resources, jos, mappedName, is, shadeRequest.getRelocators() );
                    }
                    else
                    {
                        if ( !resourceTransformed( transformers, mappedName, is, shadeRequest.getRelocators() ) )
                        {
                            // Avoid duplicates that aren't accounted for by the resource transformers
                            if ( resources.contains( mappedName ) )
                            {
                                continue;
                            }

                            addResource( resources, jos, mappedName, is );
                        }
                    }

                    IOUtil.close( is );
                }
            }

            jarFile.close();
        }
       
        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
        for ( Collection<File> jarz : overlapping.keySet() )
        {
          List<String> jarzS = new LinkedList<String>();
         
          for (File jjar : jarz)
            jarzS.add(jjar.getName());
         
        List<String> classes = new LinkedList<String>();
         
          for (String clazz : overlapping.get(jarz))
          classes.add(clazz.replace(".class", "").replace("/", "."));
       
        getLogger().warn( Joiner.on( ", " ).join(jarzS) + " define " + classes.size()
            + " overlappping classes: " );
       
        int max = 10;
       
        for ( int i = 0; i < Math.min(max, classes.size()); i++ )
          getLogger().warn("  - " + classes.get(i));
       
        if ( classes.size() > max )
          getLogger().warn("  - " + (classes.size() - max) + " more...");
       
        }
       
        if (overlapping.keySet().size() > 0) {
          getLogger().warn("maven-shade-plugin has detected that some .class files");
          getLogger().warn("are present in two or more JARs. When this happens, only");
          getLogger().warn("one single version of the class is copied in the uberjar.");
          getLogger().warn("Usually this is not harmful and you can skeep these");
          getLogger().warn("warnings, otherwise try to manually exclude artifacts");
          getLogger().warn("based on mvn dependency:tree -Ddetail=true and the above");
          getLogger().warn("output");
          getLogger().warn("See http://docs.codehaus.org/display/MAVENUSER/Shade+Plugin");
        }

        for ( ResourceTransformer transformer : transformers )
        {
            if ( transformer.hasTransformedResource() )
            {
                transformer.modifyOutputStream( jos );
            }
        }

        IOUtil.close( jos );
View Full Code Here

TOP

Related Classes of org.apache.maven.plugins.shade.resource.ResourceTransformer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.