Gradle Jar 结构与实际项目结构不同
Gradle Jar structure is different than the real project structure
我正在尝试使用 gradle 构建我的项目,但由于某些原因,资源的级别与其实际级别不同。
这是构建:
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)
这是项目的实际结构(仅显示 src 文件夹):
|src-
| main-
| java-
| client-...
| server-...
| resources-
| images-...
生成的jar结构如下:
|jar-
| images-...
| main-
| java-
| client-...
| server-...
| META_INF-...
| org-...
显然这不是我想要的。我希望 jar 中的图像在 main.resources 内,就像它们在现实中一样。为什么 jar 不遵循项目的真实结构?有人可以帮我纠正这种情况吗?
编辑:
用于访问资源的代码段:
ClientConstants.RESOURCES = "/main/resources/images/";
if (status.equals(ClientConstants.STATUS_OFFLINE))
{
img = ClientConstants.RESOURCES+"ON.png";
action = "Connect";
}
else
{
img = ClientConstants.RESOURCES+"OFF.png";
action = "Disconnect";
}
Image imageConnect = new Image(getClass().getResourceAsStream(img));
ImageView viewBottom = new ImageView(imageConnect);
当您的项目由 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。
我正在尝试使用 gradle 构建我的项目,但由于某些原因,资源的级别与其实际级别不同。 这是构建:
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)
这是项目的实际结构(仅显示 src 文件夹):
|src-
| main-
| java-
| client-...
| server-...
| resources-
| images-...
生成的jar结构如下:
|jar-
| images-...
| main-
| java-
| client-...
| server-...
| META_INF-...
| org-...
显然这不是我想要的。我希望 jar 中的图像在 main.resources 内,就像它们在现实中一样。为什么 jar 不遵循项目的真实结构?有人可以帮我纠正这种情况吗?
编辑: 用于访问资源的代码段:
ClientConstants.RESOURCES = "/main/resources/images/";
if (status.equals(ClientConstants.STATUS_OFFLINE))
{
img = ClientConstants.RESOURCES+"ON.png";
action = "Connect";
}
else
{
img = ClientConstants.RESOURCES+"OFF.png";
action = "Disconnect";
}
Image imageConnect = new Image(getClass().getResourceAsStream(img));
ImageView viewBottom = new ImageView(imageConnect);
当您的项目由 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。