[英]I can't load resource from a multi module maven project
noob here, I have a multi module maven project but I can't load a xml file under resources folder.菜鸟在这里,我有一个多模块 maven 项目,但我无法在资源文件夹下加载 xml 文件。 structure of the project:项目结构:
and the TestClass
source code is like the following snippet which is I stole from the internet: TestClass
源代码就像我从互联网上窃取的以下代码片段:
public class TestClass {
public static void main(String[] args) throws IOException {
TestClass test = new TestClass();
InputStream is = test.getFileFromResourceAsStream("general-analyser-job.xml");
}
private InputStream getFileFromResourceAsStream(String fileName) {
ClassLoader classLoader = getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream(fileName);
if (inputStream == null) {
throw new IllegalArgumentException("file not found! " + fileName);
} else {
return inputStream;
}
}
}
inputStream
is alwayse null! inputStream
始终为空!
problem solved, it was silly one: in pom.xml
of the module under build
tag resources get excluded like:问题解决了,这是一个愚蠢的问题:在build
标签资源下的模块的pom.xml
中,被排除在外,例如:
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>**/*.*</exclude>
</excludes>
</resource>
</build>
just comment out <excludes>
and everything works只需注释掉<excludes>
一切正常
First you don't need to add首先你不需要添加
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
</resource>
...
You can simply remove that part because it's the default (convention over configuration).您可以简单地删除该部分,因为它是默认的(约定优于配置)。
Furthermore you should put your test class TestClass.java
into src/test/java
plus a package name.此外,您应该将您的测试 class TestClass.java
放入src/test/java
加上一个 package 名称。 You shouldn't put the TestClass
directory into src/main/java
which is the default package which shouldn't being used.您不应将TestClass
目录放入src/main/java
中,这是不应使用的默认 package。
To access the resource you could go like this:要访问资源,您可以像这样 go :
this.getClass().getResourceAsStream("/general...");
Based on your post I'm not sure if TestClass
is really meant as test or as test implementation for production code later on.根据您的帖子,我不确定TestClass
是否真的意味着测试或稍后作为生产代码的测试实现。 If so you should move it into a real package and rename it to something more useful.如果是这样,您应该将其移动到真正的 package 并将其重命名为更有用的名称。
try to put your xml
file into src/test/resources
and then use code like below:尝试将您的xml
文件放入src/test/resources
然后使用如下代码:
InputStream is = TestClass.class.getClassLoader().getResourceAsStream("general-analyser-job.xml");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.