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

て烟熏妆下的殇ゞ 提交于 2020-12-08 05:52:58

问题


It is written in manual:

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


回答1:


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



回答2:


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.




回答3:


Brand new IntelliJ Java Gradle project. (Disclaimer: there was a setting to auto-create empty source directories automatically that I checked)

Upon adding this, it created that demo directory and the resources under it.

sourceSets {
    demo
}

To compile your additional sourceSet you can run

gradlew demoClasses



回答4:


As described in gradle's documentation and already mentioned in the other answers, to add a new source set it should be sufficient to write:

sourceSets {
    demo {
        compileClasspath += main.output
    }
}

The Java plugin will automatically add corresponding compileDemoJava and processDemoResources tasks to your build.

To automatically execute these tasks during your regular build, you can add a dependency to the default tasks:

tasks.compileJava.dependsOn compileDemoJava

or the other way around:

tasks.compileJava.finalizedBy compileDemoJava

To add the output of your new source set to some distribution file, you will however need to define a corresponding task and declare this task's output to be an artifact of your build:

task demoJar(type: Jar) {
    from sourceSets.demo.output
}

artifacts {
    archives demoJar
}

See here for further reading on the publishing stuff


UPDATE:

  • Added main.output to compile classpath of demo source set as already suggested by @Amine


来源:https://stackoverflow.com/questions/36421798/how-to-define-custom-source-set-without-defining-its-path-explicitly-in-gradle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!