How to filter just some srcDirs of java task?

I have a Java project here; we have the source for a library in a first level folder (it’s a git submodule).

This is part of my directory structure.

+-------------------src-+-main-+-java-+-* <- Want this
|
+-"YAGSL-example"-+-src-+-main-+-java-+-frc-* <- ignore this
|                                     |
+                                     +-swervelib-*      want this, treat as in package 'swervelib'

the source code in directory ‘swervelib’ is in java package ‘swervelib’, so I can’t just add that directory to the main sourceset, the resultant jar does not have those class files in the swervelib folder.

how do I include two folder trees in my java build, applying a filter to only one of them?

Just

sourceSets {
    main {
        java {
            exclude("frc")
        }
    }
}

Ah. Forgot that detail; both my main and the YAGSL-example have frc.robot; I want the former, but not the latter.

+-------------------src-+-main-+-java-+-frc-* <- Want this
|                                     |
+                                     +-org-* <- Want this
|
+-"YAGSL-example"-+-src-+-main-+-java-+-frc-* <- ignore this
|                                     |
+                                     +-swervelib-*      want this, treat as in package 'swervelib'

exclude will exclude both of them.

Got a little closer; changed the mindset to “how to exclude some files in source set”.

that yielded:

sourceSets {
    main {
        java {
            srcDir 'YAGSL-Example/src/main/java/'
            exclude { filedetails ->
                // https://discuss.gradle.org/t/how-to-exclude-all-files-under-a-directory-which-do-not-match-a-given-criteria/21528/2?u=fovea1959

                // filedetails is org.gradle.api.internal.file.DefaultFileVisitDetails
                // print "??? " + filedetails + "\n"
                // print "?1  " + filedetails.class + "\n"
                // print "?2  " + filedetails.path + "\n"
                String filepath = filedetails.file.path.replace("\\", "/")
                // print "?3  " + filepath + "\n"
                if (filepath.contains("YAGSL-Example/src/main/java/frc")) {
                    print "--- " + filepath + "\n"
                    return true
                }
                print "+++ " + filepath + "\n"
                return false
            }
        }
    }
}

But the compile is failing on the files in the main project that try to import anything from swervelib; it’s like the compile is not seeing the stuff in swervelib?

What about just

sourceSets {
    main {
        java {
            srcDir("YAGSL-Example/src/main/java/swervelib")
        }
    }
}

The directory structure files are in is actually irrelevant to the Java compiler, only the package statement in the source file is relevant.
That typically the directory structure is following the package naming is just usual convention and furthermore supports the spreading of the false-fact that foo.bar.baz is a “sub-package” of foo.bar which is not true, as no package hierarchy exists.

Or you could probably also just work with two source sets like

val example by sourceSets.creating {
    java {
        setSrcDirs(listOf("YAGSL-Example/src/main/java"))
        exclude("frc")
    }
}
dependencies {
    implementation(example.output)
}

But actually, also your fancy exclude logic should work as far as I have tested.

I tried this, but the class files for the swervelib package ended up at the root of my final jar, instead of the swervelib folder.

is there a typo in here? I tried this, but the first line throws

* Where:
Build file 'C:\Users\dwegs\Documents\2024 Code\FRC3620_2024_Cheep-Cheep_YAGSL\build.gradle' line: 133

* What went wrong:
A problem occurred evaluating root project 'FRC3620_2024_Cheep-Cheep_YAGSL'.
> Could not get unknown property 'example' for root project 'FRC3620_2024_Cheep-Cheep_YAGSL' of type org.gradle.api.Project.

got it.

sourceSets {
    example {
        java {
            srcDir "YAGSL-Example/src/main/java"
            exclude("frc")
        }
    }
}
dependencies {
    implementation sourceSets.example.output
}

having trouble with dependencies with the build of example, but that is another topic.

I tried this, but the class files for the swervelib package ended up at the root of my final jar, instead of the swervelib folder.

Then you did something else the causes this most probably.
I checked in build/classes/java/main and there the structure was exactly like expected.
Can you maybe show an MCVE where it is not?

is there a typo in here? I tried this, but the first line throws

No, no typo.
But you copied Kotlin DSL code into a Groovy DSL file and that naturally does not compile properly. :slight_smile:

having trouble with dependencies with the build of example, but that is another topic.

Well, it is a separate source set now, so it has separate dependency configurations.
But you could also make them extend the one from the main source set to have the same dependencies.