shadow icon indicating copy to clipboard operation
shadow copied to clipboard

Unable to transform file with multiple transformers

Open davidbokor opened this issue 7 years ago • 1 comments

Using version 2,.0.4 of the shadow plugin with Gradle 4.10.2, I have multiple custom transformers configured. If a single file passes the canTransformResource() filter for multiple transformers, only the first one configured gets executed.

My configuration basically looks like this:

shadowJar {
   transform(Transformer1) {
   }

   transform(Transformer2) {
   }
}

Transformer2.canTransformResource() never gets called while Transformer1.canTransformResource() actually gets called twice.

Is this meant to work or should I create a transformer that performs multiple transform actions?

davidbokor avatar Dec 15 '18 02:12 davidbokor

Here's a test that illustrates the issue. In this test I used the PropertiesFileTransformer to do two similar operations on a file. In the real world, I'm using two different transformer classes that each modify the file differently.

    def 'multiple transformer on single file'() {
        given:
        File one = buildJar('one.jar').insertFile('test.properties',
                'key1Before=foo\nkey2Before=bar').write()

        buildFile << """
            import ${PropertiesFileTransformer.name}
            shadowJar {
                from('${escapedPath(one)}')
            }
            shadowJar {
                transform(PropertiesFileTransformer) {
                    paths = [
                        'test.properties'
                    ]
                    keyTransformer = { key ->
                        key.replaceAll('key1Before', 'key1After')
                    }
                }
                transform(PropertiesFileTransformer) {
                    paths = [
                        'test.properties'
                    ]
                    keyTransformer = { key ->
                        key.replaceAll('key2Before', 'key2After')
                    }
                }
            }
        """.stripIndent()

        when:
        runner.withArguments('shadowJar').build()

        then:
        assert output.exists()

        and:
        String text = getJarFileContents(output, 'test.properties')

        Properties p = new Properties()
        p.load(new StringReader(text))

        assert p.containsKey('key1After')
        assert p.containsKey('key2After')
    }

The last assert fails...the properties still contains 'key2Before'.

davidbokor avatar Dec 15 '18 14:12 davidbokor

The first zip entry is closed, but the second is not, and they share the same path but different values. I don't think we need to handle this case, please thy to merge the transformers like #1550.

Goooler avatar Jul 30 '25 13:07 Goooler

We can fail the build if the duplicated entries are added in this case. Addressed a new issue to #1551.

Goooler avatar Jul 30 '25 13:07 Goooler