Sunday, November 9, 2014

Resource in Maven POM

Recently I was engaged to a project which convert a legacy ant build application into Maven build. My main application project has several sub projects. One child project fully contained sql scripts and cron job scripts. So I had to find where to include these sql and .sh files in the maven structure. I google and went through few articles to find out the correct place.

There is no clear cut solution I found. Some people suggest to put it into resources folder and some suggest to use separate folder. So, I thought to go ahead with a separate folder because the content of resource folder will attach to ear once I build it ( but still we can exclude them. Yet I thought it could be messy).


While surfing the web, I found more about Resources. We can include our separate folder into jar file. In my case, I do not want sql and .sh files to available in final jar. I just want to keep those in the same project. Let's see where to include those files and how to bundle our own folder into a jar.

Below is the apache introduction about Resources..
" Resources is a nice feature of build elements is specifying where resources exist within the 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, such as code generation"

So I included all my sql and .sh files in its own directory instead put them in src/main/resource/scripts/sql, Now my sql and .sh files are located in src/main/scripts/sql and src/main/scripts/sh folders respectively. When I build the project, scripts folder is not included to jar.

Say, now I need to build a jar including the sql and sh files.

For this I used JAR plugin to bundle the resource correctly, and I would specify resources similar to the following:

<project xmlns="" xmlns:xsi=""

See below detail about each tag.

  • resources: is a list of resource elements that each describe what and where to include files associated with this project.
  • targetPath: Specifies the directory structure to place the set of resources from a build. Target path defaults to the base directory. A commonly specified target path for resources that will be packaged in a JAR is META-INF.
  • filtering: is true or false, denoting if filtering is to be enabled for this resource. Note, that filter *.properties files do not have to be defined for filtering to occur - resources can also use properties that are by default defined in the POM (such as ${project.version}), passed into the command line using the "-D" flag (for example, "-Dname=value") or are explicitly defined by the properties element. Filter files were covered above.
  • directory: This element's value defines where the resources are to be found. The default directory for a build is ${basedir}/src/main/resources.
  • includes: A set of files patterns which specify the files to include as resources under that specified directory, using * as a wildcard.
  • excludes: The same structure as includes, but specifies which files to ignore. In conflicts between include and exclude, exclude wins.
  • testResources: The testResources element block contains testResource elements. Their definitions are similar to resource elements, but are naturally used during test phases. The one difference is that the default (Super POM defined) test resource directory for a project is ${basedir}/src/test/resources. Test resources are not deployed.
Hope this would help to increase your knowledge about maven resources.

No comments:

Post a Comment