Java 类org.apache.maven.plugins.shade.relocation.SimpleRelocator 实例源码

项目:apache-maven-shade-plugin    文件:ShadeMojo.java   
private List<Relocator> getRelocators()
{
    List<Relocator> relocators = new ArrayList<Relocator>();

    if ( relocations == null )
    {
        return relocators;
    }

    for ( PackageRelocation r : relocations )
    {
        relocators.add( new SimpleRelocator( r.getPattern(), r.getShadedPattern(), r.getIncludes(), r.getExcludes(),
                                             r.isRawString() ) );
    }

    return relocators;
}
项目:maven-shade-plugin    文件:ShadeMojo.java   
private List<Relocator> getRelocators()
{
    List<Relocator> relocators = new ArrayList<Relocator>();

    if ( relocations == null )
    {
        return relocators;
    }

    for ( PackageRelocation r : relocations )
    {
        relocators.add( new SimpleRelocator( r.getPattern(), r.getShadedPattern(), r.getIncludes(), r.getExcludes(),
                                             r.isRawString() ) );
    }

    return relocators;
}
项目:apache-maven-shade-plugin    文件:DefaultShaderTest.java   
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 ) );
}
项目:apache-maven-shade-plugin    文件:ShadeMojoTest.java   
public void testShaderWithExclusions()
    throws Exception
{
    File jarFile = new File( getBasedir(), "target/unit/foo-bar.jar" );

    Shader s = (Shader) lookup( Shader.ROLE, "default" );

    Set<File> set = new LinkedHashSet<File>();
    set.add( new File( getBasedir(), "src/test/jars/test-artifact-1.0-SNAPSHOT.jar" ) );

    List<Relocator> relocators = new ArrayList<Relocator>();
    relocators.add( new SimpleRelocator( "org.codehaus.plexus.util", "hidden", null, Arrays.asList(
            "org.codehaus.plexus.util.xml.Xpp3Dom", "org.codehaus.plexus.util.xml.pull.*") ) );

    List<ResourceTransformer> resourceTransformers = new ArrayList<ResourceTransformer>();

    List<Filter> filters = new ArrayList<Filter>();

    ShadeRequest shadeRequest = new ShadeRequest();
    shadeRequest.setJars( set );
    shadeRequest.setUberJar( jarFile );
    shadeRequest.setFilters( filters );
    shadeRequest.setRelocators( relocators );
    shadeRequest.setResourceTransformers( resourceTransformers );

    s.shade( shadeRequest );

    ClassLoader cl = new URLClassLoader( new URL[]{ jarFile.toURI().toURL() } );
    Class<?> c = cl.loadClass( "org.apache.maven.plugins.shade.Lib" );

    Field field = c.getDeclaredField( "CLASS_REALM_PACKAGE_IMPORT" );
    assertEquals( "org.codehaus.plexus.util.xml.pull", field.get( null ) );

    Method method = c.getDeclaredMethod( "getClassRealmPackageImport", new Class[0] );
    assertEquals( "org.codehaus.plexus.util.xml.pull", method.invoke( null, new Object[0] ) );
}
项目:maven-shaded-log4j-transformer    文件:PluginsCacheFileTransformerTest.java   
@Test
public void test() throws Exception {
    PluginsCacheFileTransformer t = new PluginsCacheFileTransformer();
    final InputStream is = getClass().getClassLoader().getResourceAsStream(PLUGIN_CACHE_FILE);
    t.processResource(PLUGIN_CACHE_FILE, is, null);
    assertFalse(t.hasTransformedResource());

    List<Relocator> relocators = new ArrayList<Relocator>();
    relocators.add(new SimpleRelocator(null, null, null, null));
    t.processResource(PLUGIN_CACHE_FILE, is, relocators);
    assertTrue(t.hasTransformedResource());
}
项目:maven-shaded-log4j-transformer    文件:PluginsCacheFileTransformerTest.java   
private void testRelocation(String src, String pattern, String target) throws IOException {
    PluginsCacheFileTransformer t = new PluginsCacheFileTransformer();
    Relocator log4jRelocator = new SimpleRelocator(src, pattern, null, null);
    PluginCache aggregator = new PluginCache();
    aggregator.loadCacheFiles(enumeration(singletonList(pluginUrl)));

    t.relocatePlugin(aggregator, singletonList(log4jRelocator));

    for (Map<String, PluginEntry> pluginEntryMap : aggregator.getAllCategories().values()) {
        for (PluginEntry entry : pluginEntryMap.values()) {
            assertTrue(entry.getClassName().startsWith(target));
        }
    }
}
项目:maven-shade-plugin    文件:DefaultShaderTest.java   
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 ) );
}
项目:maven-shade-plugin    文件:ShadeMojoTest.java   
public void testShaderWithExclusions()
    throws Exception
{
    File jarFile = new File( getBasedir(), "target/unit/foo-bar.jar" );

    Shader s = (Shader) lookup( Shader.ROLE, "default" );

    Set<File> set = new LinkedHashSet<File>();
    set.add( new File( getBasedir(), "src/test/jars/test-artifact-1.0-SNAPSHOT.jar" ) );

    List<Relocator> relocators = new ArrayList<Relocator>();
    relocators.add( new SimpleRelocator( "org.codehaus.plexus.util", "hidden", null, Arrays.asList(
            "org.codehaus.plexus.util.xml.Xpp3Dom", "org.codehaus.plexus.util.xml.pull.*") ) );

    List<ResourceTransformer> resourceTransformers = new ArrayList<ResourceTransformer>();

    List<Filter> filters = new ArrayList<Filter>();

    ShadeRequest shadeRequest = new ShadeRequest();
    shadeRequest.setJars( set );
    shadeRequest.setUberJar( jarFile );
    shadeRequest.setFilters( filters );
    shadeRequest.setRelocators( relocators );
    shadeRequest.setResourceTransformers( resourceTransformers );

    s.shade( shadeRequest );

    ClassLoader cl = new URLClassLoader( new URL[]{ jarFile.toURI().toURL() } );
    Class<?> c = cl.loadClass( "org.apache.maven.plugins.shade.Lib" );

    Field field = c.getDeclaredField( "CLASS_REALM_PACKAGE_IMPORT" );
    assertEquals( "org.codehaus.plexus.util.xml.pull", field.get( null ) );

    Method method = c.getDeclaredMethod( "getClassRealmPackageImport", new Class[0] );
    assertEquals( "org.codehaus.plexus.util.xml.pull", method.invoke( null, new Object[0] ) );
}
项目:apache-maven-shade-plugin    文件:ServiceResourceTransformerTest.java   
@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, false );
        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();
    }
}
项目:apache-maven-shade-plugin    文件:DefaultShaderTest.java   
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] );
}
项目:maven-shade-plugin    文件:DefaultShaderTest.java   
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] );
}
项目:apache-maven-shade-plugin    文件:DefaultShaderTest.java   
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 );
}
项目:apache-maven-shade-plugin    文件:ShadeMojoTest.java   
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 );
}
项目:maven-shade-plugin    文件:DefaultShaderTest.java   
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 );
}
项目:maven-shade-plugin    文件:ShadeMojoTest.java   
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 );
}