我试图用gradle构建我的项目,但由于某种原因,资源被放在与实际级别不同的级别上。 这是构建:
{
"name" : "nw-demo",
"main" : "index.html",
"window" : {
"toolbar" : true
}
}
并且这里是项目的实际结构(仅显示src文件夹):
apply plugin: 'java'
version = '1.1'
archivesBaseName = 'DesktopOmegle'
repositories {
//mavenCentral()
maven {
url 'http://oss.sonatype.org/content/repositories/snapshots/'
url "http://repo1.maven.org/maven2"
}
}
dependencies {
compile 'org.json:json:20090211'
compile 'org.fxmisc.richtext:richtextfx:0.6.4'
}
jar {
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
manifest {
attributes 'Main-Class': 'main.java.client.OmegleClient'
}
}
task copyDeps(type: Copy) {
from configurations.runtime
into 'lib'
rename {
String fileName ->
fileName.replace('-20090211', '')
}
}
build.dependsOn(copyDeps)
生成的jar结构如下:
|src-
| main-
| java-
| client-...
| server-...
| resources-
| images-...
显然,这不是我想要的。我希望jar中的图像在main.resources中,因为它们在现实中。为什么罐子不是坚持项目的真实结构?有人可以帮我纠正这种情况吗?
编辑: 用于访问资源的代码段:
|jar-
| images-...
| main-
| java-
| client-...
| server-...
| META_INF-...
| org-...
答案 0 :(得分:1)
当你的项目由gradle管理时,你应该总是使用maven引入的标准布局,或者有一个很好的借口来使用任何不同的布局。 Here您可以找到示例项目,该项目显示如何使用此布局并读取稍后将包含在jar中的资源。这些资源将始终置于jar文件的根目录下。在下面,您可以找到说明如何阅读此类文件的示例代码:
private void readResource(String path) {
InputStream in = getClass().getClassLoader().getResourceAsStream(path);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
try (Scanner scanner = new Scanner(reader)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println("LINE: " + line);
}
scanner.close();
} catch (Exception e) {
e.printStackTrace();
}
}
示例代码取自here。