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

Is it possible to specify the entry points for minimize configuration? #885

Open
chorniyn opened this issue Sep 16, 2023 · 3 comments
Open

Comments

@chorniyn
Copy link

Use Case: The final jar contains multiple classes A and B from which the execution can start (these are the AWS lambda handlers).

Question: Is it possible to configure minimizing so that it would clean everything except A, B and their transitive dependencies?

@maxlim33
Copy link

I wonder this as well. It will be extremely helpful for my use case as well, which is similar to yours.

This is possible with Maven Shade's :

By default, all of the target module’s classes are kept and used as entry points for JAR minimization. By explicitly limiting the set of entry points, you can further minimize the set of classes kept in the shaded JAR. This affects both classes in the module itself and dependency classes.

It seems that there is something similar in Gradle Shadow: #273, which is closed. Unfortunately, I am new and I can't understand. Perhaps, you can make sense of it?

@maxlim33
Copy link

I have taken the time to try to understand the commit from superbobry in #273, following snippet will remove unused classes in my own project:

import org.vafer.jdependency.Clazz
import org.vafer.jdependency.Clazzpath

final entryPoint = 'com.example.testgroovy.HelloWorld'

shadowJar {
    doFirst {
        final cp = new Clazzpath()
        for (SourceSet sourceSet in sourceSets) {
            FileCollection sourceSetClassesDirs = sourceSet.output.classesDirs
            sourceSetClassesDirs.each { File classDir ->
                if (classDir.isDirectory()) {
                    cp.addClazzpathUnit(classDir)
                }
            }
        }
        Set<Clazz> unused = cp.clazzes
        Clazz clazz = cp.getClazz(entryPoint)
        if (clazz == null) {
            throw new RuntimeException("Entry point not found: " + entryPoint);
        }

        unused.remove(clazz)
        unused.removeAll(clazz.transitiveDependencies)

        unused.collect { it.name }.toSet()
        exclude(unused.collect { it.name.replaceAll(/\./, '/') + '.class' }.toSet())
    }
    minimize()
}

So far, I have just tested a simple setups for my project. This is still a good starting point.

@Vampire
Copy link

Vampire commented Dec 29, 2023

I am new and I can't understand. Perhaps, you can make sense of it?

That PR added minimization possibility at all, including entrypoint support.
But it was closed without being merged.
A different PR (#390) was instead merged which does not have the entrypoint functionality, hence this issue about the missing entrypoint support.

PS: Changing the configuration of a task at execution time like in your snippet is generally a pretty bad idea and if you ever want to use configuration cache, it even is forbidden.

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

No branches or pull requests

3 participants