50

I hava an example java project package

package com.example.testing;

with such file tree

app
|
  src->com->example->testing->Main.java

and a gradle script:

apply plugin: 'java'
apply plugin: 'application'

sourceSets {
    main {
        java {
            srcDirs 'src'
        }
    }


}

sourceSets.main.output.classesDir = file("classes")
mainClassName = 'com.example.testing.Main'

defaultTasks 'compileJava', 'run'

Now I want to add some module to this project and my folders will be something like this

app
|
  src1->com->example->testing->Main.java

  src2->com->another_example->another_testing->Library.java

How do I add new source code to gradle script?

RedCollarPanda
  • 1,389
  • 1
  • 20
  • 40
  • 6
    Why don't you respect the standard gradle/Maven conventions? Everything would work fine automatically, without needing to configure all the paths. – JB Nizet Jun 26 '15 at 16:35
  • 10
    I for instance would like to have a separate directory for generated sources. – Florian F Oct 09 '18 at 14:38

4 Answers4

63

I agree with @JB Nizet about respecting standard conventions. If you still insist on being an Anarchist though:

You already have src declared in your sourceset, why not add src1 and src2 as well? You can add them to the same sourceset, or define a sourceset per module if you want.

sourceSets {
    main {
        java {
            srcDirs 'src'
            srcDirs 'src1'
            srcDirs 'src2'
        }
    }
 }

To reference files outside the project, see this answer.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
RaGe
  • 22,696
  • 11
  • 72
  • 104
  • 18
    While I agree with wanting to use standard conventions, this one tip was useful when dealing with "generated" sources, which wind up wherever the generator tool feel like tossing them... so, thanks! – Marco Massenzio May 25 '17 at 20:52
23

The question is about "Adding"; the question of the text is describing a more concrete scenario. If one just wants to add an existing directory, this is the way to add:

sourceSets.main.java.srcDirs += ['src/gen/java']

An example full build.gradle is as follows:

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'com.squareup:javapoet:1.12.1'
}

sourceSets.main.java.srcDirs += ['src/gen/java']

JavaPoet s a Java API for generating .java source files. It is just used as example library for the build.gradle file.

koppor
  • 19,079
  • 15
  • 119
  • 161
17

I have a slightly different approach with a Gradle 4.6:

sourceSets {
    main {
        java {
            srcDir 'src/main/java'
            srcDir 'build/swagger-code-dummy/src/main/java'
        }
    }
}

as you can see, I had to specify the directories with the "/main/java" subdirectories as well, otherwise gradle/intellij was not setting the right path.

Maybe this helps someone else too :)

gabowsky
  • 572
  • 4
  • 13
  • This is also helpful if your project involves multiple languages, such as Python or Bash, and you need those directories to be treated like source roots also. – Jonathan E. Landrum Sep 25 '19 at 13:13
  • When do you do this? Before or after generating the code? And do you need to add the dependencies? – arc1880 Aug 21 '23 at 01:04
13

A slightly different solution:

sourceSets.main.java.srcDirs = ['build/jasper', 'src/main/java']
Nolequen
  • 3,032
  • 6
  • 36
  • 55
Fresh Codemonger
  • 1,125
  • 1
  • 10
  • 20