Simulate cannot find main class if I add a sourceSet

I have a (test) project here that we have brought a library in as a submodule (chasing down bugs features in the library). The library actually is a subdirectory in the source of a complete robot project, but I just added the library directory from the project to my build.gradle.

sourceSets {
    main {
        java {
            srcDir 'YAGSL-Example/src/main/java'
            include 'swervelib/'
        }
    }
}

it builds fine, but when I try to simulate on the desktop, VSCode says “Searching for main class”, then the debugger for java gives me “ConfigError: Main class ‘frc.robot.Main’ doesn’t exist in the workspace.”.

I looked at the built jar (build\libs\FRC3620_2024_Cheep-Cheep_YAGSL.jar), and there is no frc.robot.Main class in there. Actually, nothing from frc.robot… is there. looks like it built the swervelib/ only.

How do I correctly add the submodule’s swervelib/ to the build process?

Use srcDir instead of include. You can have multiple srcDir declarations in a source set.

include is actually a regex of files to include in the source set, not directories to include in the source set. By default everything is included, and then when you add an include it switches to only adding things that match that include.

I didn’t even try that because I thought it would blow up because it thought that the files in the swervelib package should be in the null package, not realizing the swervelib was the package name. worked perfectly.

gradle is pretty smart!

spoke too soon. VSCode is happy, but a build fails.

the contents of the swervelib package are designed to be in package ‘swervelib’. gradle thinks they should be in the root package (since they are at the first level of srcdir), so I get a pile of:

The declared package "swervelib.encoders" does not match the expected package "encoders"

when doing a build.

How do I tell it to use ‘YAGSL-Example/src/main/java’ as root and compile only the swervelib directory, and use ‘src/main/java’ as a root, compiling everything?

Can you post your folder layout? I’m a bit confused on what you’re expecting it to actually be.

Each srcDir in a source set is considered as a package root. Theres not really a way around that. If you want a class to have package swervelib.encoders, that file needs to be in srcDirProvidedPath/swervelib/encoders. Thats just how Java works, theres not really a way around it.

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

I want to compile everything from src/main/java, and only compile the swervelib/ folder from YAGSL-example/src/main/java.

I was hoping that the include swervelib/ would only affect the srcDir 'YAGSL-Example/src/main/java' and not the implicit srcDir src/main/java.
`

I understand that. I am trying to figure out how to apply a filter to one srcDir, but not the other.

You can probably use a filter block to filter the extra files out. You should be able to do a directoy match easily enough.

https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/util/PatternFilterable.html

Otherwise, you can create a 2nd source set and build that as well. The gradle docs for that are
https://docs.gradle.org/current/userguide/building_java_projects.html#sec:java_source_sets

Ah. I was trying to modify the existing default sourceset with the include, which affected everything in the source set.

I have it correctly making a new sourceSet.

sourceSets {
    swervelib {
        java {
            srcDir 'YAGSL-Example/src/main/java'
            include 'swervelib/**'
        }
    }
}

// https://gist.github.com/bazted/550b9206ec5dc1e0450083959a43cab2
sourceSets.each {
    println(it)
    it.allSource.each {
        println(it)
    }
}

How to I tell gradle to include this ‘swervelib’ sourceset to the set of files it should compile? I’m not recognizing that in the gradle docs (I’m sure it’s in there, but I’m not seeing it).

Have an ugly solution.

  1. Make an empty swervelib folder under src/main/java; make sure it’s listed in .gitignore.

  2. Add a gradle task to copy swervelib from YAGSL-Example/src/main/java/swervelib to src/main/java.

  3. Make sure that the compile depends on that task.

tasks.register('frc3620_swervelib_sync', Sync) {
    from layout.projectDirectory.dir("YAGSL-Example/src/main/java/swervelib")
    into layout.projectDirectory.dir("src/main/java/swervelib")
}

compileJava {
    dependsOn frc3620_swervelib_sync
}

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.