【问题标题】:Netbeans maven project can't find resourcesNetbeans maven项目找不到资源
【发布时间】:2019-05-06 13:44:46
【问题描述】:

我正在尝试将我的 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 的东西,但我无法弄清楚。

【问题讨论】:

    标签: maven netbeans resources


    【解决方案1】:

    首先,您将目录 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

    【讨论】:

    • 谢谢。我不想承认我已经工作了多久。来这里之前想了很多,但是不需要 pom 的 部分。
    猜你喜欢
    • 2018-11-29
    • 2011-11-18
    • 1970-01-01
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    • 2022-11-24
    相关资源
    最近更新 更多