maven-shade-plugin icon indicating copy to clipboard operation
maven-shade-plugin copied to clipboard

[MSHADE-452]Fix ServiceResourceTransformer not correct issue.

Open hk-lrzy opened this issue 2 years ago • 6 comments

Background

We have java path as below

  • org.apache.demo1
  • org.apache.demo2

In maven-shade-plugin 3.2.1, we use relocation to process path and SPI Service file at the same time, and we will follow result.

<relocation>
    <pattern>org.apache.demo</pattern>
    <shadedPattern>shaded.org.apache.demo</shadedPattern>
</relocation> 
META-INF/services/shaded.org.apache.demo

But in maven-shaded-plugin 3.5.0, it can't work as before, alouthght it SPI file name alao could be changed, but the SPI file content will have no change.

I compared the differneces between the maven-shaded-plugin 3.5.0 and maven-shaded-plugin 3.2.1, finally find in maven-shaded-plugin 3.2.1, file content changed by follow code

public String applyToSourceContent( String sourceContent )
{
    if ( rawString )
    {
        return sourceContent;
    }
    else
    {
        return sourceContent.replaceAll( "\\b" + pattern, shadedPattern );
    }
}

but in maven-shaded-plugin 3.5.0, the code change to 

private String shadeSourceWithExcludes( String sourceContent, String patternFrom, String patternTo,
                                        Set<String> excludedPatterns )
{
    // Usually shading makes package names a bit longer, so make buffer 10% bigger than original source
    StringBuilder shadedSourceContent = new StringBuilder( sourceContent.length() * 11 / 10 );
    boolean isFirstSnippet = true;
    // Make sure that search pattern starts at word boundary and we look for literal ".", not regex jokers
    for ( String snippet : sourceContent.split( "\\b" + patternFrom.replace( ".", "[.]" + "\\b" ) ) )
    {
        boolean doExclude = false;
        for ( String excludedPattern : excludedPatterns )
        {
            if ( snippet.startsWith( excludedPattern ) )
            {
                doExclude = true;
                break;
            }
        }
        if ( isFirstSnippet )
        {
            shadedSourceContent.append( snippet );
            isFirstSnippet = false;
    }
        else
        {
            shadedSourceContent.append( doExclude ? patternFrom : patternTo ).append( snippet );
        }
    }
    return shadedSourceContent.toString();
} 

In my cases, source content is org.apache.demo1, pattern from is org.apache.demo, pattern to is shaded.org.apache.demo.

Following this checklist to help us incorporate your contribution quickly and easily:

  • [x] Make sure there is a JIRA issue filed for the change (usually before you start working on it). Trivial changes like typos do not require a JIRA issue. Your pull request should address just this issue, without pulling in other changes.
  • [x] Each commit in the pull request should have a meaningful subject line and body.
  • [x] Format the pull request title like [MSHADE-XXX] - Fixes bug in ApproximateQuantiles, where you replace MSHADE-XXX with the appropriate JIRA issue. Best practice is to use the JIRA issue title in the pull request title and in the first line of the commit message.
  • [x] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • [x] Run mvn clean verify to make sure basic checks pass. A more thorough check will be performed on your pull request automatically.
  • [x] You have run the integration tests successfully (mvn -Prun-its clean verify).

If your pull request is about ~20 lines of code you don't need to sign an Individual Contributor License Agreement if you are unsure please ask on the developers list.

To make clear that you license your contribution under the Apache License Version 2.0, January 2004 you have to acknowledge this by using the following check-box.

hk-lrzy avatar Jul 20 '23 12:07 hk-lrzy

Hi @hk-lrzy , sorry for the delay (guess the PR popped during holidays). From my window this PR has the risk to revert some of the fixes done before. Long story short shade mixing the same API (relocation) for sources (variables, classes, packages), paths and other things, there is always a such a risk so ultimately I think a safer fix would make the relocated file more contextual (if (filePath ~ META-INF/services/xxxxxx then use relocateSPIFile()` to share the idea).

Hope it makes sense.

rmannibucau avatar Feb 18 '24 14:02 rmannibucau