Tuesday, September 2, 2014

Using regular expression during Maven packaging stage

During maven build, the default Maven implementation will package all libraries into WEB-INF/lib folder. But the requirement has remind me that I shouldn’t include those libraries in the build except those custom build library since those libraries has already been deploy into WebSphere Application Server (WAS). Thus it helps in reducing the resulting jar and making the deployment process run faster in WAS.

Now my objective is to ensure the resulting jar is clean except those custom made library. To achieve this mission, the clue is to use packagingExcludes of maven-war-plugin’s configuration, as shown in the code snippet below:

 org.apache.maven.plugins
 maven-war-plugin
 2.2
 
  ...  
 

The challenge of this mission is to exclude any other libraries except those with custom made. I have been struggling for quite some while until my colleague has discover this could be done by using regular expression as shown below:

 org.apache.maven.plugins
 maven-war-plugin
 2.2
 
  %regex[WEB-INF/lib/(?!mycustomelibrary).*.jar]  
 

The code snippet above telling Maven to exclude any libraries other than mycustomlibrary.jar during the build. This has brought up another issue to me, what if I need to retain 2 libraries while ignoring the rest during the build? Fortunately right after this discovery, I found the solution on this problem which is by using packagingIncludes where I only need to specified the particular library that need to be includes.

1 comment:

TahitianGabriel said...

If you want to retain more than one librairie, you can use the "or" condition using a '|' :

%regex[WEB-INF/lib/(?!mycustomelibrary|anotherlibrary).*.jar]