如何在不使用Gradle显式定义其路径的情况下定义自定义源集? [英] How to define custom source set without defining it's path explicitly in Gradle?

查看:100
本文介绍了如何在不使用Gradle显式定义其路径的情况下定义自定义源集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是用手册编写的:

src/sourceSet/java是给定源集的Java源"的默认路径.

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

如何利用这一点?假设我想创建源集demo.

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

我可以写

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?

更新

我已经测试

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

sourceSets {
    demo {
        java
        resources
    }
}

sourceSets {
    demo
}

在所有情况下,运行gradle build都不会导致源文件被编译.

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

build.gradle文件如下:

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

示例项目: https://github.com/dims12/MultipleSourceRoots

推荐答案

Gradle默认任务将不会构建非默认源集,除非在主链中对它们有明确的依赖性.为了编译您的demo类,您必须根据cricket_007的答案调用gradle demoClasses-或做得更好,只需执行gradle build demo,这还将把生成的类放入目标jar中.

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.

您需要的是这个

sourceSets {
    demo
}

...和build demo任务确实会按预期创建任何丢失的目录,除非您有一些有效的怪异权限方案.

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

现在我看了看您的git项目,看来您的演示源集取决于主要类.您需要明确此依存关系(即将它们添加到demo类路径中),以避免编译错误:

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

这篇关于如何在不使用Gradle显式定义其路径的情况下定义自定义源集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆