简体   繁体   English

Java构建时如何控制资源处理?

[英]How to control resources processing while Java build?

Who facilitates, that any file from resources subdirectory from source set is copied to resources folder of build directory? 谁协助将源集中resources子目录中的任何文件复制到构建目录的资源文件夹中?

I have created simple Gradle project inside IntelliJ and have configured additional source root called demo : 我已经在IntelliJ中创建了一个简单的Gradle项目,并配置了名为demo其他源根目录:

在此处输入图片说明

build.gradle is follows: 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
}

Then I placed simple text files into rerources directories of both sets. 然后,我将简单的文本文件放入这两个集合的rerources目录中。

After that I noticed, that highlighted file (one from demo source set) appears only if I run build from IntelliJ. 之后,我注意到,仅当我从IntelliJ运行build时,才会显示突出显示的文件(来自demo源集)。 If I run build goal from Gradle, only second file (from main source set) appears. 如果我从Gradle运行build目标,则仅显示第二个文件(来自main源集)。

How to configure gradle to process all files from all source sets? 如何配置gradle处理来自所有源集的所有文件?

I don't use intellij so i can't tell you why it automatcally executes demoClasses to compile and copy the demo sourceSet but you can execute it and will see the resources as intellij would do it. 我不使用intellij,所以我无法告诉您为什么它会自动执行demoClasses来编译和复制演示sourceSet,但是您可以执行它,并且会像intellij那样看到资源。

just add 只需添加

build.dependsOn demoClasses

and it will do it on every build. 它将在每次构建中都执行。 To add them to your jar you have to add it as source like 要将它们添加到您的jar中,您必须将其添加为源

jar {
    from sourceSets.demo.output
}

For a sourceset that depends on the main just add it's output 对于依赖主源的源集,只需添加其输出即可

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

And if you also need it's compile/runtime libs for demo compile just do 而且,如果您还需要演示编译的运行时库,请执行

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM