Add another java source directory to gradle script

前端 未结 4 832
温柔的废话
温柔的废话 2020-12-17 07:59

I hava an example java project package

package com.example.testing;

with such file tree

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


        
相关标签:
4条回答
  • 2020-12-17 08:16

    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'
            }
        }
     }
    
    0 讨论(0)
  • 2020-12-17 08:17

    A slightly different solution:

    sourceSets.main.java.srcDirs = ['build/jasper', 'src/main/java']
    
    0 讨论(0)
  • 2020-12-17 08:18

    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.

    0 讨论(0)
  • 2020-12-17 08:19

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

    0 讨论(0)
提交回复
热议问题