Netbeans Maven项目找不到资源

时间:2019-05-06 13:44:46

标签: maven netbeans 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的信息,但我一直无法弄清楚。

1 个答案:

答案 0 :(得分:0)

首先,您将目录 srv / subdir 设置为资源,而不是项目根目录下的 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>

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

ResourceTest
  src
    main
      java
      resources
        subdir
          readme.txt

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

ResourceTest.class.getResource 

应该工作。您正在解决您的资源现在班级相对。系统将处理您的资源是在应用程序的jar文件中还是在文件位置中。看这里: getClass().getResourceAsStream() in maven project