如何使用gradle将资源添加到sourceSet? [英] How do I add resources to sourceSet with gradle?
问题描述
apply plugin:'java'
存储库{
mavenCentral()
}
sourceSets {
main {
java {
srcDir'src / model'
资源{
srcDir'images / model'
}
}
测试{
java {
srcDir' tests / model'
}
resources {
srcDir'images / model'//< === NOT WORKING
}
}
}
依赖项{
编译文件('libs / mnist-tools.jar','libs / gson-2.2.4.jar')
runtime fileTree(dir:'libs' ,包括:'* .jar')
testCompile group:'junit',name:'junit',version:'4. +'
}
我的存储库如果在这里: https:// github。 COM / quinnliu / WalnutiQ
并且我的49个测试中有4个失败,因为文件夹tests / model中的测试需要文件夹images / model中的文件。我如何正确添加资源?谢谢!
我仔细看了一下你的build.gradle,看起来路径有点偏离。 p>
你指定source为 src / model
,但你的项目结构和Java源代码建议 model
是你的包名,这意味着源声明应该是:
main {
java {
srcDir'src'
}
}
相同测试:
测试{
java {
srcDir'测试'
}
现在,缺少资源。在您的代码中,您使用的是 ImageIO.read(getClass()。getResource(BMPFileName))
getClass()。getResource ()
正在使用资源的相对路径。为了将资源保持在同一级别,您应该更新资源声明并删除 model
:
test {
java {
srcDir'tests'
}
resources {
srcDir'images'
}
}
您可能还需要运行
./ gradlew clean
p>
下面是更新后的build.gradle的结果:
希望它有助于:)
Currently I have the following build.gradle file:
apply plugin: 'java'
repositories {
mavenCentral()
}
sourceSets {
main {
java {
srcDir 'src/model'
}
resources {
srcDir 'images/model'
}
}
test {
java {
srcDir 'tests/model'
}
resources {
srcDir 'images/model' // <=== NOT WORKING
}
}
}
dependencies {
compile files('libs/mnist-tools.jar', 'libs/gson-2.2.4.jar')
runtime fileTree(dir: 'libs', include: '*.jar')
testCompile group: 'junit', name: 'junit', version: '4.+'
}
My repository if here: https://github.com/quinnliu/WalnutiQ
and 4 out of my 49 tests are failing because the tests in folder "tests/model" need a file within the folder "images/model". How do I add the resources correctly? Thanks!
I had a closer look at your build.gradle and it seems that paths are a little bit off.
You specify source as src/model
, yet your project structure and Java source suggest that model
is your package name, which means the source declaration should be:
main {
java {
srcDir 'src'
}
}
Same for tests:
test {
java {
srcDir 'tests'
}
}
Now, with missing resources. In your code you are using ImageIO.read(getClass().getResource(BMPFileName))
getClass().getResource()
is using relative path to the resource. To keep the resources on the same level, you should update declaration for the resources and remove model
:
test {
java {
srcDir 'tests'
}
resources {
srcDir 'images'
}
}
You might also need to run
./gradlew clean
before it works.
Here's the result with the updated build.gradle:
Hope it helps :)
这篇关于如何使用gradle将资源添加到sourceSet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!