-
-
Notifications
You must be signed in to change notification settings - Fork 424
Migrating Maven transformer to Gradle. #393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
...hub/jengelman/gradle/plugins/shadow/transformers/Log4j2PluginsCacheFileTransformer.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License") you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package com.github.jengelman.gradle.plugins.shadow.transformers | ||
|
|
||
| import com.github.jengelman.gradle.plugins.shadow.ShadowStats | ||
| import com.github.jengelman.gradle.plugins.shadow.relocation.RelocateClassContext | ||
| import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator | ||
| import org.apache.commons.io.IOUtils | ||
| import org.apache.commons.io.output.CloseShieldOutputStream | ||
| import org.apache.logging.log4j.core.config.plugins.processor.PluginCache | ||
| import org.apache.logging.log4j.core.config.plugins.processor.PluginEntry | ||
| import org.apache.tools.zip.ZipEntry | ||
| import org.apache.tools.zip.ZipOutputStream | ||
| import org.gradle.api.file.FileTreeElement | ||
|
|
||
| import static org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor.PLUGIN_CACHE_FILE; | ||
|
|
||
| /** | ||
| * Modified from the maven equivalent to work with gradle | ||
| * | ||
| * @author Paul Nelson Baker | ||
| * @see <a href="https://www.linkedin.com/in/paul-n-baker/">LinkedIn</a> | ||
| * @see <a href="https://github.com/paul-nelson-baker/">GitHub</a> | ||
| * @see <a href="https://github.com/edwgiz/maven-shaded-log4j-transformer">edwgiz/maven-shaded-log4j-transformer</a> | ||
| * @see <a href="https://github.com/edwgiz/maven-shaded-log4j-transformer/blob/master/src/main/java/com/github/edwgiz/mavenShadePlugin/log4j2CacheTransformer/PluginsCacheFileTransformer.java">PluginsCacheFileTransformer.java</a> | ||
| */ | ||
| class Log4j2PluginsCacheFileTransformer implements Transformer { | ||
|
|
||
| private final List<File> temporaryFiles; | ||
| private final List<Relocator> relocators; | ||
|
|
||
| public Log4j2PluginsCacheFileTransformer() { | ||
| temporaryFiles = new ArrayList<>(); | ||
| relocators = new ArrayList<>(); | ||
| } | ||
|
|
||
| @Override | ||
| boolean canTransformResource(FileTreeElement element) { | ||
| return PLUGIN_CACHE_FILE == element.name | ||
| } | ||
|
|
||
| @Override | ||
| void transform(TransformerContext context) { | ||
| def inputStream = context.is | ||
| def temporaryFile = File.createTempFile("Log4j2Plugins", ".dat") | ||
| temporaryFile.deleteOnExit() | ||
| temporaryFiles.add(temporaryFile) | ||
| IOUtils.copy(inputStream, new FileOutputStream(temporaryFile)) | ||
| def contextRelocators = context.relocators | ||
| if (contextRelocators != null) { | ||
| this.relocators.addAll(contextRelocators) | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| boolean hasTransformedResource() { | ||
| // This functionality matches the original plugin, however, I'm not clear what | ||
| // the exact logic is. From what I can tell temporaryFiles should be never be empty | ||
| // if anything has been performed. | ||
| def hasTransformedMultipleFiles = temporaryFiles.size() > 1 | ||
| def hasAtLeastOneFileAndRelocator = !temporaryFiles.isEmpty() && !relocators.isEmpty() | ||
| def hasTransformedResources = hasTransformedMultipleFiles || hasAtLeastOneFileAndRelocator | ||
| return hasTransformedResources | ||
| } | ||
|
|
||
| @Override | ||
| void modifyOutputStream(ZipOutputStream zipOutputStream) { | ||
| PluginCache pluginCache = new PluginCache() | ||
| pluginCache.loadCacheFiles(getUrlEnumeration()) | ||
| relocatePlugins(pluginCache) | ||
| zipOutputStream.putNextEntry(new ZipEntry(PLUGIN_CACHE_FILE)) | ||
| pluginCache.writeCache(new CloseShieldOutputStream(zipOutputStream)) | ||
| temporaryFiles.clear() | ||
| } | ||
|
|
||
| private Enumeration<URL> getUrlEnumeration() { | ||
| def urls = temporaryFiles.collect({ it.toURL() }).asList() | ||
| return Collections.enumeration(urls) | ||
| } | ||
|
|
||
| private void relocatePlugins(PluginCache pluginCache) { | ||
| for (Map<String, PluginEntry> currentMap : pluginCache.getAllCategories().values()) { | ||
| pluginEntryLoop: | ||
| for (PluginEntry currentPluginEntry : currentMap.values()) { | ||
| String className = currentPluginEntry.getClassName(); | ||
| RelocateClassContext relocateClassContext = new RelocateClassContext(className, new ShadowStats()); | ||
| for (Relocator currentRelocator : relocators) { | ||
| // If we have a relocator that can relocate our current entry... | ||
| boolean canRelocateClass = currentRelocator.canRelocateClass(relocateClassContext); | ||
| if (canRelocateClass) { | ||
| // Then we perform that relocation and update the plugin entry to reflect the new value. | ||
| String relocatedClassName = currentRelocator.relocateClass(relocateClassContext); | ||
| currentPluginEntry.setClassName(relocatedClassName); | ||
| continue pluginEntryLoop; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
86 changes: 86 additions & 0 deletions
86
...jengelman/gradle/plugins/shadow/transformers/Log4j2PluginsCacheFileTransformerTest.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package com.github.jengelman.gradle.plugins.shadow.transformers | ||
|
|
||
| import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator | ||
| import com.github.jengelman.gradle.plugins.shadow.relocation.SimpleRelocator | ||
| import org.apache.tools.zip.ZipOutputStream | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
|
|
||
| import java.util.zip.ZipEntry | ||
| import java.util.zip.ZipFile | ||
|
|
||
| import static java.util.Collections.singletonList | ||
| import static org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor.PLUGIN_CACHE_FILE | ||
| import static org.junit.Assert.assertFalse | ||
| import static org.junit.Assert.assertTrue | ||
|
|
||
| /** | ||
| * @author Paul Nelson Baker | ||
| * @since 2018-08 | ||
| * @see <a href="https://github.com/paul-nelson-baker/">GitHub</a> | ||
| * @see <a href="https://www.linkedin.com/in/paul-n-baker/">LinkedIn</a> | ||
| */ | ||
| //@RunWith(Parameterized.class) | ||
| class Log4j2PluginsCacheFileTransformerTest { | ||
|
|
||
| Log4j2PluginsCacheFileTransformer transformer | ||
|
|
||
| @Before | ||
| void setUp() { | ||
| transformer = new Log4j2PluginsCacheFileTransformer() | ||
| } | ||
|
|
||
| @Test | ||
| void testShouldNotTransform() { | ||
| transformer.transform(new TransformerContext(PLUGIN_CACHE_FILE, getResourceStream(PLUGIN_CACHE_FILE), null)) | ||
| assertFalse(transformer.hasTransformedResource()) | ||
| } | ||
|
|
||
| @Test | ||
| void testShouldTransform() { | ||
| List<Relocator> relocators = new ArrayList<>() | ||
| relocators.add(new SimpleRelocator(null, null, null, null)) | ||
| transformer.transform(new TransformerContext(PLUGIN_CACHE_FILE, getResourceStream(PLUGIN_CACHE_FILE), relocators)) | ||
| assertTrue(transformer.hasTransformedResource()) | ||
| } | ||
|
|
||
| @Test | ||
| void testRelocators() { | ||
| testRelocate("org.apache.logging", "new.location.org.apache.logging", "new.location.org.apache.logging") | ||
| testRelocate("org.apache.logging", "new.location.org.apache.logging", "org.apache.logging") | ||
| } | ||
|
|
||
| void testRelocate(String source, String pattern, String target) throws IOException { | ||
| List<Relocator> relocators = singletonList((Relocator) new SimpleRelocator(source, pattern, null, null)) | ||
| transformer.transform(new TransformerContext(PLUGIN_CACHE_FILE, getResourceStream(PLUGIN_CACHE_FILE), relocators)) | ||
| assertTrue("Transformer didn't transform resources", transformer.hasTransformedResource()) | ||
| // Write out to a fake jar file | ||
| def testableZipFile = File.createTempFile("testable-zip-file-", ".jar") | ||
| def fileOutputStream = new FileOutputStream(testableZipFile) | ||
| def bufferedOutputStream = new BufferedOutputStream(fileOutputStream) | ||
| def zipOutputStream = new ZipOutputStream(bufferedOutputStream) | ||
| transformer.modifyOutputStream(zipOutputStream) | ||
| zipOutputStream.close() | ||
| bufferedOutputStream.close() | ||
| fileOutputStream.close() | ||
| // Pull the data back out and make sure it was transformed | ||
| ZipFile zipFile = new ZipFile(testableZipFile) | ||
| ZipEntry zipFileEntry = zipFile.getEntry(PLUGIN_CACHE_FILE) | ||
| InputStream inputStream = zipFile.getInputStream(zipFileEntry) | ||
| new Scanner(inputStream).withCloseable { scanner -> | ||
| boolean hasAtLeastOneTransform = false | ||
| while (scanner.hasNextLine()) { | ||
| String nextLine = scanner.nextLine() | ||
| if (nextLine.contains(source)) { | ||
| hasAtLeastOneTransform = true | ||
| assertTrue("Target wasn't included in transform", nextLine.contains(target)) | ||
| } | ||
| } | ||
| assertTrue("There were no transformations inside the file", hasAtLeastOneTransform) | ||
| } | ||
| } | ||
|
|
||
| InputStream getResourceStream(String resource) { | ||
| return this.class.getClassLoader().getResourceAsStream(resource); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line does not work for
Seems we should use
element.relativePath.pathStringhere.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressing the fix to #1175.