[英]Netbeans maven project can't find resources
我正在尝试将我的netbeans项目从使用ant转换为maven。 我找到了我在寻找答案时找到的代码。
public class ResourceTest {
public static void main(String[] args) {
ResourceTest app = new ResourceTest ();
}
public ResourceTest () {
doClassLoaderGetResource ("/subdir/readme.txt");
doClassGetResource ("/subdir/readme.txt");
doClassLoaderGetResource ("subdir/readme.txt");
doClassGetResource ("subdir/readme.txt");
}
private void doClassLoaderGetResource (String sPath) {
URL url = getClass().getClassLoader().getResource(sPath);
if (url == null)
System.out.println("ClassLoader.getResource(" + sPath + "): NULL");
else
System.out.println("ClassLoader.getResource(" + sPath + "): SUCCESS");
}
private void doClassGetResource (String sPath) {
URL url = getClass().getResource(sPath);
if (url == null)
System.out.println("Class.getResource(" + sPath + "): NULL");
else
System.out.println("Class.getResource(" + sPath + "): SUCCESS");
}
}
当我运行ant版本时它没有问题,我得到了预期的输出。
ClassLoader.getResource(/subdir/readme.txt): NULL
Class.getResource(/subdir/readme.txt): SUCCESS
ClassLoader.getResource(subdir/readme.txt): SUCCESS
Class.getResource(subdir/readme.txt): NULL
我使用maven在netbeans中创建了相同的项目。 我的结构看起来像这样:
ResourceTest
src
main
java
subdir
readme.txt
这是我的pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.todivefor</groupId>
<artifactId>ResourceTest</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<resources>
<resource>
<directory>src/subdir</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadeTestJar>false</shadeTestJar>
<shadedClassifierName>SHADED</shadedClassifierName>
<shadedArtifactAttached>true</shadedArtifactAttached>
<!-- <addClasspath>true</addClasspath> -->
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>ResourceTest</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我已经移动了subdir文件夹,但无济于事。 当我运行maven项目时,我得到所有空值。 我错过了关于maven的事情,但我一直都没弄清楚。
首先,你可以将这个目录SRV /子目录的资源,并在你的项目根本不SUBDIR。
但是,您应该遵循maven标准。
https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
只需删除pom.xml的此资源部分:
<resources>
<resource>
<directory>src/subdir</directory>
</resource>
</resources>
并将您的子目录放入src / main / resources 。 所有目录都有应用程序资源。 你现在应该有类似的东西:
ResourceTest
src
main
java
resources
subdir
readme.txt
您的资源文件现在应自动复制到目标文件夹中。 你不需要这个类加载器的东西,一个简单的:
ResourceTest.class.getResource
应该管用。 您现在正在处理您的资源类相对。 如果您的资源位于应用程序的jar文件中或文件位置内,系统将处理。 在这里看: maven项目中的getClass()。getResourceAsStream() 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.