Support `$` prefix for private/non-importable classes.
Is there a way to rename shaded classes (prefix with $), in order to prevent importing on user's side? Like https://github.com/immutables/maven-shade-plugin#1-relocation-with--uglyfication
Have you tried exactly that?
shadowJar {
relocate 'com.google.common', 'com.my.internal.$guava$'
}
It leads to
Caused by: java.lang.IllegalArgumentException: Illegal group reference
at java.util.regex.Matcher.appendReplacement(Matcher.java:857)
at java.util.regex.Matcher.replaceFirst(Matcher.java:1004)
at java.lang.String.replaceFirst(String.java:2178)
at java_lang_String$replaceFirst$8.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
at com.github.jengelman.gradle.plugins.shadow.relocation.SimpleRelocator.relocatePath(SimpleRelocator.groovy:171)
at com.github.jengelman.gradle.plugins.shadow.relocation.Relocator$relocatePath$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at com.github.jengelman.gradle.plugins.shadow.impl.RelocatorRemapper.map(RelocatorRemapper.groovy:95)
at org.objectweb.asm.commons.Remapper.mapDesc(Unknown Source)
at org.objectweb.asm.commons.RemappingClassAdapter.visitAnnotation(Unknown Source)
at org.objectweb.asm.ClassReader.accept(Unknown Source)
at org.objectweb.asm.ClassReader.accept(Unknown Source)
at org.objectweb.asm.ClassReader$accept.call(Unknown Source)
at com.github.jengelman.gradle.plugins.shadow.tasks.ShadowCopyAction$StreamAction.remapClass(ShadowCopyAction.groovy:269)
... 153 more
Try escaping the symbols - "$"
@johnrengelman
- Need double escape like
relocate 'com.google.common', 'com.my.internal.\\$guava\\$' - It doesn't rename classes, it merely renames the package to $guava$ (a valid package name), but doesn't prepend class names with $.
That doesn't appear to be a standard feature of Shade. Does having $ in the package not do the same thing?
@johnrengelman no. When I type in IDE Immu... which is not imported yet, IDE suggest me to import com.google.common.collect.ImmutableList, as well as com.my.internal.$guava$.ImmutableList. If in shaded version I have a class name prefixed i. e. $ImmutableList, IDE won't suggest it, because class name prefix doesn't match.
See https://github.com/google/auto/issues/167, https://github.com/google/auto/issues/263
I tested the pattern
relocate('com.google.common', 'com.my.internal.\\$guava\\$')
for the example from https://github.com/immutables/maven-shade-plugin#1-relocation-with--uglyfication
The com.google.common.Main will be relocated to com.my.internal.$guava$.Main. See #1729.
I tried to address this in #1729. Ideally, there should be an extra flag in SimpleRelocator to control this feature. However, since this is such an edge case, implementing it could introduce more complex scenarios to handle. I don’t believe it’s worth doing at this point.