Librabry shading example

26 views
Skip to first unread message

Goyot, Martin

unread,
Aug 17, 2020, 8:35:37 AM8/17/20
to jenkin...@googlegroups.com
Hi there,

I have an issue with a dependency on my plugin and would like to shade it. I've found the docs here: https://www.jenkins.io/doc/developer/plugin-development/dependencies-and-class-loading/#shading

But can't seem to get it to work. Right now I'm trying on Guava, I have this in my <build> section in pom.xml:

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>findbugs-maven-plugin</artifactId>
                    <version>3.0.5</version>
                </plugin>
                <plugin>
                    <groupId>org.jenkins-ci.tools</groupId>
                    <artifactId>maven-hpi-plugin</artifactId>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <relocations>
                                <relocation>
                                    <pattern>com.google.common</pattern>
                                    <shadedPattern>shaded.com.google.common</shadedPattern>
                                </relocation>
                            </relocations>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Any idea on how to proceed ?

Thanks in advance !

Gavin Mogan

unread,
Aug 17, 2020, 5:11:56 PM8/17/20
to Jenkins Developers
> But can't seem to get it to work.

A question online/async question should always include the 3 items:

What did you try (sorta provided)?
What happened?
What did you expect to happen?



--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/CA%2Bb6JB9fZvyE%3DS1ZGtuu3gD%3D13vDgxsc2w_o0TMfV%3DFnRNYLgQ%40mail.gmail.com.

Goyot, Martin

unread,
Aug 18, 2020, 4:35:43 AM8/18/20
to jenkin...@googlegroups.com
Yes, you are right sorry.

What did you try ?

So, I have a dependency on guava 29.0-jre. I had a problem due to the Guava in Jenkins so I decided to give shading a try. Here is what I added to my pom.xml:

I then tried to run mvn hpi:run to check if my bug (toImmutableList() no such method) was gone.

What happened ?

The bug still is present, I still get a toImmutableList no such methode. Also, in the trace of the mvn hpi:run I don't see any call to shading, while if I execute mvn package I see it, which seems normal as the selected <phase> is package but then what should I use ?

What did you expect ?

To see the call to shading in the trace, and my bug to be gone.

Gavin Mogan

unread,
Aug 18, 2020, 4:42:16 AM8/18/20
to Jenkins Developers
Did you switch your imports to use the new shaded library? Is the object coming from your code or external?

If you create the object with the new shaded imports does it have that method?

Goyot, Martin

unread,
Aug 18, 2020, 5:10:39 AM8/18/20
to jenkin...@googlegroups.com
Hi Gavin,

so, here is the incriminated code in my plugin:

    private Boolean scopesAreValid(Secret secret) {
        return client
        .getAccessKeyScopes(secret)
        .stream()
        .map(AccessKeyScope::getIdentifier)
        .collect(ImmutableList.toImmutableList())
        .containsAll(MANDATORY_SCOPES);
    }

which imports: import com.google.common.collect.ImmutableList;

I thought that the shading would do the switch by itself, but as you hinted me in your mail, I just tried to change the import with import shaded.com.google.common.collect.ImmutableList;  and re-run mvn hpi:run, which gives me the error package shaded.com.google.common.collect does not exist.

Jesse Glick

unread,
Aug 19, 2020, 4:04:53 PM8/19/20
to Jenkins Dev
On Mon, Aug 17, 2020 at 8:35 AM Goyot, Martin <martin...@enalean.com> wrote:
> I have an issue with a dependency on my plugin and would like to shade it.

In my experience you need to switch to a multimodule (reactor)
project, with one helper module that shades the lib, and then depend
on the helper module from your plugin.

`artifact-manager-s3` has a dep on newer Guava using
`pluginFirstClassLoader`, though this forms its own type of hell.

Goyot, Martin

unread,
Aug 20, 2020, 3:50:00 AM8/20/20
to jenkin...@googlegroups.com
Hi Jesse,

Thanks, I'm gonna look into this multimodule project, see if I can make it work.

Is `pluginFirstClassLoader` an acceptable tradeoff or am I still better off making shading work ?

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.

Jesse Glick

unread,
Aug 20, 2020, 9:01:17 AM8/20/20
to Jenkins Dev
On Thu, Aug 20, 2020 at 3:50 AM Goyot, Martin <martin...@enalean.com> wrote:
> Is `pluginFirstClassLoader` an acceptable tradeoff

It is just very risky. For example, you cannot say with confidence
that because all `JenkinsRule`-based tests pass that the plugin
actually works. It may explode in colorful ways when you actually run
it in a real Jenkins instance.
Reply all
Reply to author
Forward
0 new messages