# How to filter just some srcDirs of java task?

**URL:** <https://discuss.gradle.org/t/how-to-filter-just-some-srcdirs-of-java-task/47514>\
**Category:** Help/Discuss\
**Created:** [January 24, 2024, 5:10pm UTC](https://discuss.gradle.org/t/how-to-filter-just-some-srcdirs-of-java-task/47514 "2024-01-24T17:10:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![fovea1959](https://avatars.discourse-cdn.com/v4/letter/f/3be4f8/32.png) [@fovea1959](https://discuss.gradle.org/u/fovea1959)\
**Post date:** [January 24, 2024, 5:10pm UTC](https://discuss.gradle.org/t/how-to-filter-just-some-srcdirs-of-java-task/47514/1 "2024-01-24T17:10:59Z")

</div>

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.

```gradle
+-------------------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?

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [January 24, 2024, 7:19pm UTC](https://discuss.gradle.org/t/how-to-filter-just-some-srcdirs-of-java-task/47514/2 "2024-01-24T19:19:35Z")

</div>

Just

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

```

---

<div class="post-metadata">

**Author:** ![fovea1959](https://avatars.discourse-cdn.com/v4/letter/f/3be4f8/32.png) [@fovea1959](https://discuss.gradle.org/u/fovea1959)\
**Post date:** [January 31, 2024, 3:48pm UTC](https://discuss.gradle.org/t/how-to-filter-just-some-srcdirs-of-java-task/47514/3 "2024-01-31T15:48:53Z")

</div>

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

```gradle
+-------------------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.

---

<div class="post-metadata">

**Author:** ![fovea1959](https://avatars.discourse-cdn.com/v4/letter/f/3be4f8/32.png) [@fovea1959](https://discuss.gradle.org/u/fovea1959)\
**Post date:** [January 31, 2024, 4:26pm UTC](https://discuss.gradle.org/t/how-to-filter-just-some-srcdirs-of-java-task/47514/4 "2024-01-31T16:26:50Z")

</div>

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

that yielded:

```gradle
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?

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [January 31, 2024, 4:44pm UTC](https://discuss.gradle.org/t/how-to-filter-just-some-srcdirs-of-java-task/47514/5 "2024-01-31T16:44:33Z")

</div>

What about just

```kts
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.

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [January 31, 2024, 4:54pm UTC](https://discuss.gradle.org/t/how-to-filter-just-some-srcdirs-of-java-task/47514/6 "2024-01-31T16:54:24Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [January 31, 2024, 4:58pm UTC](https://discuss.gradle.org/t/how-to-filter-just-some-srcdirs-of-java-task/47514/7 "2024-01-31T16:58:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fovea1959](https://avatars.discourse-cdn.com/v4/letter/f/3be4f8/32.png) [@fovea1959](https://discuss.gradle.org/u/fovea1959)\
**Post date:** [January 31, 2024, 7:03pm UTC](https://discuss.gradle.org/t/how-to-filter-just-some-srcdirs-of-java-task/47514/10 "2024-01-31T19:03:18Z")

</div>

> [@Vampire](#):
>
> What about just
> 
> ```gradle
> sourceSets {
> main {
> java {
> srcDir("YAGSL-Example/src/main/java/swervelib")
> }
> }
> }
> 
> ```

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.

---

<div class="post-metadata">

**Author:** ![fovea1959](https://avatars.discourse-cdn.com/v4/letter/f/3be4f8/32.png) [@fovea1959](https://discuss.gradle.org/u/fovea1959)\
**Post date:** [January 31, 2024, 7:10pm UTC](https://discuss.gradle.org/t/how-to-filter-just-some-srcdirs-of-java-task/47514/11 "2024-01-31T19:10:13Z")

</div>

> [@Vampire](#):
>
> Or you could probably also just work with two source sets like
> 
> ```gradle
> val example by sourceSets.creating {
> java {
> setSrcDirs(listOf("YAGSL-Example/src/main/java"))
> exclude("frc")
> }
> }
> dependencies {
> implementation(example.output)
> }
> 
> ```

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

```gradle
* 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.

```

---

<div class="post-metadata">

**Author:** ![fovea1959](https://avatars.discourse-cdn.com/v4/letter/f/3be4f8/32.png) [@fovea1959](https://discuss.gradle.org/u/fovea1959)\
**Post date:** [January 31, 2024, 7:26pm UTC](https://discuss.gradle.org/t/how-to-filter-just-some-srcdirs-of-java-task/47514/12 "2024-01-31T19:26:06Z")

</div>

got it.

```gradle
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.

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [January 31, 2024, 10:44pm UTC](https://discuss.gradle.org/t/how-to-filter-just-some-srcdirs-of-java-task/47514/13 "2024-01-31T22:44:16Z")

</div>

> 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](https://stackoverflow.com/help/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. 🙂

> 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.
