Maven – Generate a package with external resources

Maven – How to generate a package with external resources

While making a java batch, it’s a common way to generate a working directory structure like the following:

The directories above will contain:

  • conf – external configuration properties
  • lib – the jar libraries, comprensive of the main jar
  • logs – the log files
  • bin – the sh / bat script file within the instructions to run the batch properly

Our source structure folder will be similar:

  • applconf – external configuration properties
  • scripts – the sh / bat script file within the instructions to run the batch properly
  • logs – the log files

Now it’s time to edit pom.xml properly.

pom.xml

The following lines will copy applconf content inside target conf directory

The following lines copy scripts file inside bin directory

The following copy main jar with dependencies inside lib directory:

The following will generate manifest.inf within the main jar package

Finally, the pom.xml declare that directive to build the zipped package are contained inside assembly-app.xml file

assembly-app.xml

These directives will copy jar dependencies inside lib directory and applconf content inside conf directory.

If our need is to place main jar outside lib directory, copy the following lines instead:

 

Maven – build package for different environments

Build package for different environments using Maven

To make life easier for programmers who need to test and deploy application to a remote environment using different properties, it is possible to properly configure the pom file to fit with own necessity with just one command.

Suppose your are dealing with a web application that is tested on a local Tomcat;
the application itself read from a properties file mapped for the proper environment, one for local testing and one for distribution environment; so your need could be:

  • Build the webapp locally and distribuite to the local tomcat
  • Build the webapp and deploy it together with the properly properties file

Use the profile directive on the pom.xml to specify a test or deploy action in the following way:

Notice the following points of interest:

  • The tomcat7-maven-plugin can automatically deploy a war inside tomcat deploy folder by just giving the address in which tomcat is listening
  • The maven-antrun-plugin can execute some ant based tasks; In the case above we first delete the previous config.properties file and then copy the config.deploy.properties file (included in the source folder) to the output build directory, renaming it to config.properties.

Also notice that, mvn clean install run the test profile by default, because of the activeByDefault property set to true.

Therefore, to run the deploy profile just type the following command: