Add another java source directory to gradle script

2019-04-18 04:29发布

问题:

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?

回答1:

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'
        }
    }
 }


回答2:

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 :)



回答3:

sourceSets.main.java.srcDirs = ['build/jasper','src/main/java']



标签: java gradle