Skip to content
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

[MSHADE-452]Fix ServiceResourceTransformer not correct issue. #193

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

hk-lrzy
Copy link

@hk-lrzy hk-lrzy commented Jul 20, 2023

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:

  • 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.
  • Each commit in the pull request should have a meaningful subject line and body.
  • 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.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Run mvn clean verify to make sure basic checks pass. A more thorough check will
    be performed on your pull request automatically.
  • 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.

@elharo elharo changed the title [MSGADE-452]Fix ServiceResourceTransformer not correct issue. [MSHADE-452]Fix ServiceResourceTransformer not correct issue. Feb 18, 2024
@rmannibucau
Copy link
Contributor

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.

@hk-lrzy
Copy link
Author

hk-lrzy commented May 23, 2024

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.

Thanks for reply, i will recheck the logical follow your suggestion and commit it later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants