1

I am trying to convert my netbeans projects from using ant to maven. I have this code I found in search of my answer.

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");
    }
}

When I run the ant version it works with no problem and I get the expected output.

ClassLoader.getResource(/subdir/readme.txt): NULL
Class.getResource(/subdir/readme.txt): SUCCESS
ClassLoader.getResource(subdir/readme.txt): SUCCESS
Class.getResource(subdir/readme.txt): NULL

I created the same project in netbeans using maven. My structure looks like this:

ResourceTest
  src
    main
      java
  subdir
    readme.txt

This my 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>

I have moved the subdir folder all around, but to no avail. When I run the maven project, I get all nulls. I am missing something regarding maven, but I have not been able to figure out.

todivefor
  • 121
  • 9

1 Answers1

2

First of all, you are making the directory srv/subdir a resource and not subdir at your project root.

However, you should follow maven standards.

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

Just remove this resource part of your pom.xml:

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

and put your subdir into src/main/resources. All directories there are application resources. You should now have something like:

ResourceTest
  src
    main
      java
      resources
        subdir
          readme.txt

Your resource file should now be automatically copied into the target folder. You do not need this class loader stuff, a simple:

ResourceTest.class.getResource 

should work. You are addressing your resource now class relative. The system will handle if your resource is within the jar file of your application or within the file position. Look here: getClass().getResourceAsStream() in maven project.

wumpz
  • 8,257
  • 3
  • 30
  • 25