How do I add resources to Maven Project?

Resources are not (usually) code. They are not compiled, but are items meant to be bundled within your project or used for various other reasons.

Every Java project contains a folder named resources, different type of projects have different paths, but in a standard Maven project structure, the path is ${basedir}/src/main/resources. Maven uses a standard project layout for resources to package project as JAR.

As mentioned above adding resources requires no changes to the POM. For this common task, Maven again relies on the Standard Directory Layout, which means by using standard Maven conventions you can package resources within JARs simply by placing those resources in a standard directory structure.

The simple rule employed by Maven is this: any directories or files placed within the ${basedir}/src/main/resources directory are packaged in your JAR with the exact same structure starting at the base of the JAR.

Specifying resource directories

By default, Maven will look for your project’s resources under ${basedir}/src/main/resources.

Project
|-- pom.xml
`-- src
    `-- main
        `-- resources

However, all your resources may not be in ${basedir}/src/main/resources. Thus, you’d have to specify those directories by adding the following to your POM.

<project>
 ...
 <build>
   ...
   <resources>
     <resource>
       <directory>[your folder here]</directory>
     </resource>
   </resources>
   ...
 </build>
 ...
</project>

So if your resources resides in src/my-resources

Project
|-- pom.xml
`-- src
    `-- my-resources

you can specify that directory by doing the following:

...
   <resources>
     <resource>
       <directory>src/my-resources</directory>
     </resource>
   </resources>
...

Furthermore, you can have several directories by adding multiple <resource> elements:

...
   <resources>
     <resource>
       <directory>resource1</directory>
     </resource>
     <resource>
       <directory>resource2</directory>
     </resource>
     <resource>
       <directory>resource3</directory>
     </resource>
   </resources>
...

Conclusion

  • Resources are added as convention of Standard directory layout in Maven
  • Multiple resources directory can be added to project
  • All resources directory should be added to POM.xml file
Please follow and like us: