Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define custom source set without defining it's path explicitly in Gradle?

Tags:

java

gradle

It is written in manual:

enter image description here

I.e. that src/sourceSet/java is a default path for "Java source for the given source set".

How to utilize this? Suppose I wish to create source set demo.

Can I write

sourceSets {
    demo {
        java {
            srcDir 'src/demo/java'
        }
        resources {
            srcDir 'src/demo/resources'
        }
    }
}

Can I write somehow without explicit paths?

May be I not required to write anything, just put files into demo subfolder?

UPDATE

I have tested

sourceSets {
    demo {
        java {
            srcDir 'src/demo/java'
        }
        resources {
            srcDir 'src/demo/resources'
        }
    }
}

and

sourceSets {
    demo {
        java
        resources
    }
}

and

sourceSets {
    demo
}

In all cases running gradle build does not cause sources compiled.

build.gradle file is follows:

group 'net.inthemoon.tests'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.5

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

sourceSets {
    demo {
        java {
            srcDir 'src/demo/java'
        }
        resources {
            srcDir 'src/demo/resources'
        }
    }
}

Sample project: https://github.com/dims12/MultipleSourceRoots

like image 861
Dims Avatar asked Apr 05 '16 09:04

Dims


2 Answers

Gradle default tasks will not build non-default source sets unless there is an explicit dependency on them in the main chain. In order to compile your demo classes you'd have to call gradle demoClasses as per cricket_007's answer - or better yet, just do gradle build demo which will also put the generated classes in the target jar.

All you need is this :

sourceSets {
    demo
}

... and the build demo task will indeed create any missing directory as expected, unless you have some weird permission scheme in effect.

Now I have looked at your git project and it seems your demo source set depends on the main classes. You need to make this dependency clear (i.e. add them to the demo classpaths) to avoid compilation errors :

sourceSets {
    main
    demo {
      compileClasspath += main.output
      runtimeClasspath += main.output
    }
}
like image 133
ttzn Avatar answered Nov 15 '22 18:11

ttzn


This should be all you need:

sourceSets {
    demo
}

More configuration may be needed to define the dependencies of these sources and where they should be used.

like image 35
Henry Avatar answered Nov 15 '22 17:11

Henry