Default excludes / includes in Jar task

issue-resolved
not-a-bug

(Stephan Krull) #1

The Jar task is packing all Java code to a .jar file in a Gradle Java project. It is a mystery to me which files are accepted for the jar file. Attached you find a sample Java project (created with buildship) with a source package (under “src/main/java”) that has unusual files. I would expect that while assembling the jar file these files will be taken into account. But they are not.

Then I printed excludes and includes of the java sourceSet and of the jar task. Just build with “./gradlew clean build”. The sets are empty but the unusual files are not copied into the JAR. Why?

Gradle Version: Gradle Wrapper 2.14.1
Operating System: Ubuntu 16.x
Is this a regression? If yes, which version of Gradle do you know it last worked for? No

test.jar.zip (5.0 KB)

(Mark Vieira) #2

Only *.java files are considered when they live in src/main/java. All other files will effectively be ignored. In any case, these files are technically included in the JAR, the output of the ‘compileJava’ task is (i.e. the *.class files). If you want something included in the JAR which is not compiled Java code it should reside in src/main/resources.


(Stephan Krull) #3

Could you demystify the logic? How can I change the configuration for files to take from src/main/java? It should not be hard coded into Gradle, right?

Your suggestion (to relocate files) is not very handy for huge projects with a grown different file structure. We have .sql, .xml, .properties files in the Java main source folder and I am not allowed to refactor their location while migrating to Gradle.


(Stephan Krull) #4

Found what I was looking for: sourceSets.main.java.filter.includes is “[**/*.java]” by default.

What really confuses me is that sourceSets.main.java.includes is “[]” by default. This comes from https://docs.gradle.org/nightly/javadoc/org/gradle/api/file/SourceDirectorySet.html extending from https://docs.gradle.org/nightly/javadoc/org/gradle/api/tasks/util/PatternFilterable.html.

I followed the hint from here to copy files from sourceSets.main.java too, doing it like that now:

sourceSets
{
    main
    {
        resources
        {
            source(sourceSets.main.java)
            filter.include("**/*.*")
            filter.exclude("**/*.java", "**/*.log")
            filter.exclude("**/.svn/*", "**/.git*")
        }
    }
}

This ensures that resources files under src/main/java are copied into the JAR because the task processResources is taking into account every source directory configured for the source set sourceSets.main.resources.