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