Search code examples
javagradlejarsource-sets

What files Gradle packs into a jar archive?


What expression (in build.gradle file) evaluates to list of files that are packed into a jar archive by default (without adjusting "jar" task?

I can't easily spot it in sources of org.gradle.jvm.tasks.Jar and org.gradle.api.tasks.bundling.Jar.

This question is motivated by using of a custom sourceSet files of which are not added to jar archive.


Solution

  • It's the Java Plugin that adds the jar task and configures it. You can see this in the source code of the org.gradle.api.plugins.JavaPlugin class:

    private TaskProvider<Jar> registerJarTaskFor(Project project, JavaPluginConvention pluginConvention) {
        return project.getTasks().register(JAR_TASK_NAME, Jar.class, jar -> {
            jar.setDescription("Assembles a jar archive containing the main classes.");
            jar.setGroup(BasePlugin.BUILD_GROUP);
            jar.from(mainSourceSetOf(pluginConvention).getOutput());
        });
    }
    

    https://github.com/gradle/gradle/blob/e343cfe2b37317a3de119da2809887f76042fe0d/subprojects/plugins/src/main/java/org/gradle/api/plugins/JavaPlugin.java#L315-L321