Netbeans maven 项目找不到资源



我正在尝试将NetBeans项目从使用蚂蚁转换为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");
    }
}

当我运行蚂蚁版本时,它毫无问题,我得到了预期的输出。

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 资源,而不是 subdir 在您的项目root上。

但是,您应该遵循Maven标准。

https://maven.apache.org/guides/introduction/introduction-to-the-the-pandard-directory-layout.html

只需删除pom.xml的此资源部分:

<resources>
     <resource>
       <directory>src/subdir</directory>
     </resource>
</resources>

并将您的 subdir 放入 src/main/resources 中。所有目录都有应用程序资源。您现在应该有类似的东西:

ResourceTest
  src
    main
      java
      resources
        subdir
          readme.txt

现在,您的资源文件现在应自动复制到目标文件夹中。您不需要此类加载程序的东西,一个简单:

ResourceTest.class.getResource 

应该工作。您正在介绍现在的资源相关班级。如果您的资源在应用程序的JAR文件中或文件位置内,则系统将处理。在这里查看: getClass((。

最新更新