How To Change Maven's Default Resource Folder

By , 31 August 2012

How To Change Maven's Default Resource Folder

Sometimes it's nice to have your projects resource files alongside the java source files they're related to. By default, Maven forces you to have a separate resource tree so, for example, whenever you need to a add a String for internationalization you have to go digging through your project's folders.

The good news is, you can change change maven's default resource folder this with this little bit of XML magic in Maven pom.xml.

  <build>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <excludes><exclude>**/*.java</exclude></excludes>
      </resource>
    </resources>
  </build>

Note this overrides the default location, so make sure src/main/resources is empty, or add to the pom.xml like this.

  <build>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <excludes><exclude>**/*.java</exclude></excludes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
    </resources>
  </build>

Don't forget to exclude the java files unless you want to ship your source code!

How To Change Maven's Default Resource Folder

About Roger Keays

How To Change Maven's Default Resource Folder

Roger Keays is an artist, an engineer, and a student of life. He has no fixed address and has left footprints on 40-something different countries around the world. Roger is addicted to surfing. His other interests are music, psychology, languages, the proper use of semicolons, and finding good food.

Leave a Comment

Please visit https://rogerkeays.com/how-to-change-mavens-default-resource-folder to add your comments.

Comment posted by: Anne, 6 years ago

 

Thank you for this post. You just saved my day!

 

Comment posted by: Daniel, 8 years ago

Really nice information, is there a way to include the dependency project property files ?
How to include the property files of dependency project ?
I am trying to get the property files of dependency project but it doesn't add in the final jar file. Currently i am adding those files after the deployment.

Comment posted by: sonia, 8 years ago

Hi,

in my case, it is not building the java files.only adding the folder.

folder structure :

src/test/java

changes in my pom.xml :

<resources>
      <resource>
        <directory>src/test/java</directory>
        <excludes><exclude>**/*.java</exclude></excludes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
    </resources>