Just a tip on the specific example below.

If your “generated” dir is from a tool also running in Gradle, then you can add 
the extra source dir within that task – and can (should really) also declare 
your output dirs. An example of the start of our Antlr task:

    task generateGrammarSource {
        description = 'Generates Java sources from Antlr2 grammars.'
        inputs.files grammarFiles
        outputs.dir file(antlrGeneratedSrcDir)

        // Make sure generated sources are included in Java set (also ensures 
Netbeans picks them!)
        project.sourceSets.matching { it.name == "main" } .all {
            it.java.srcDir "${antlrGeneratedSrcDir}"
        }

If you do it this way, you get 2 benefits:


  *   Gradle knows to rerun the tool (Antlr in the above) if the input sources 
change
  *   The NB gradle handling knows about the outputs too and resolves them 
properly when finding sources

It took us a couple of spins to come to this approach, but it’s worth it. We do 
the same with GWT, XmlBeans schema handling, and a couple of other generators 
our codebase uses

-Rob

From: Ulrich Mayring <[email protected]>
Sent: 20 September 2025 17:42
To: Scott Palmer <[email protected]>
Cc: [email protected]
Subject: Re: Multiple source roots?


Am Samstag, September 20, 2025 16:59 CEST, schrieb Scott Palmer 
<[email protected]<mailto:[email protected]>>:

Back on topic… Multiple sources roots are trivial with Gradle:

sourceSets {
    main {
        java {
            srcDir 'src/main/java'
            srcDir 'generated/src/main/java' // Add a generated source directory
        }
        resources {
            srcDir 'src/main/resources'
            srcDir 'src/main/config' // Add another resource directory
        }
    }
}

To be fair, how does that show up in Netbeans, namely the project properties 
dialog?

I think there is a certain lack of cohesion there. Some build systems only 
allow one source root, others allow many - how do you design a UI for that?


Reply via email to