Follow me

Friday, October 25, 2013

Including and Excluding Jars and Resource Files While Building War using Maven

A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project.
Here I am just going to share the scenario where we have different profile like production, pre-production, QA … maintain for maven project and some time these environment may refer the different properties or resources file for ex. database or URL based properties.
Now while building the jar we need to pick up the right resource file in class path as per our profile/environment and maven has  MavenAntRun Plug-in which provides the ability to run Ant tasks from within Maven.
Maven AntRun Plug-in has only one goal called run which is an ant task for maven.
We have <execution> tag to run our goal. This is a build phase before creation of war in which

<Phase>prepare-package</phase> phase perform any operations necessary to prepare a package before the actual packaging.

<executions>
                        <execution>
                              <phase>prepare-package</phase>
                              <goals>
                              <goal>run</goal>
                              </goals>
                              <configuration>
                        …….
                              </configuration>
                        </execution>
                  </executions>



Now the important point when we create the package of compiles code at form which we are going to build the war we can decide what file we can keep and what file we can remove from the packages using
<configuration>tag .

So here for ex.  If we havedifferent profile folder in our resources like
ü  Profile/Prod/files
ü  Profile/Pre-Prod/files
Then we can select the needful files based for our profile using below tasks<task>

<move file="folder/filename"  tofile="classpath/filename"/>

This <move> will move the required file on the classpath.

<deleteincludeEmptyDirs="true">
<filesetdir="target/classes"casesensitive="false">
<includename="**/profiles/**"/>
</fileset>
</delete>

<delete> will remove the rest unwanted files from that profile folder.

Below is the sample profile from pom.xml


<profile>
       <id>PROD</id>
       <activation>
       <activeByDefault>false</activeByDefault>
       </activation>
       <build>
       <plugins>
              <plugin>
                     <artifactId>maven-antrun-plugin</artifactId>
                     <executions>
                           <execution>
                                  <phase>prepare-package</phase>
                                  <goals>
                                  <goal>run</goal>
                                  </goals>
                                  <configuration>
                                  <tasks>
                     <movefile="target/classes/profiles/Prod/db.properties"
                                  tofile="target/classes/db.properties"/>
                                   <movefile="target/classes/profiles/Prod/production-url.xml"
                                  tofile="target/classes/production-url.xml"/>
                                  <deleteincludeEmptyDirs="true">
                                  <filesetdir="target/classes"casesensitive="false">
                                  <includename="**/profiles/**"/>
                                  </fileset>
                                  </delete>
                                  </tasks>
                                  </configuration>
                           </execution>
                     </executions>
              </plugin>
       </plugins>
       </build>
</profile>

Now once code is compile and packaged. We are ready to create the war file,
 now here also we can include or exclude certain files/jars from the WAR file, 
by using the <packagingIncludes> and <packagingExcludes> configuration parameters. 
They each take a comma-separated list of Ant file set patterns. 
You can use wildcards such as ** to indicate multiple directories and * to
indicate an optional part of a file or directory name.Sometimes even such
wildcards are not enough. In these cases you can use regular expressions with the %regex[] syntax


<profile>
                <id>PROD</id>
      <activation>
      <activeByDefault>false</activeByDefault>
      </activation>
      <build>
      <plugins>
            <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-war-plugin</artifactId>
                  <version>2.3</version>
                  <configuration>
                  <webXml>src/main/webapp/WEB-INF/custom/web_prod.xml</webXml>
                  <warSourceExcludes>**/custom/**</warSourceExcludes>
                  <packagingExcludes>
WEB-INF/lib/commons-logging-*.jar,
            %regex[WEB-INF/lib/log4j-(?!over-slf4j).*.jar]
</packagingExcludes>
                  </configuration>
            </plugin>
      </plugins>
      </build>
</profile>





2 comments:

  1. How will I include a jar and exclude a class file in that jar?

    ReplyDelete
  2. The King of Dealer - The King of Dealer
    › the_kingofdealer- › the_kingofdealer- Oct 24, 2021 — kirill-kondrashin Oct 24, 2021 All the online gambling sites have made it 우리카지노 to be one of the best casinos online to date with slot machines, table games, and live dealer games. The King of Dealer casino is

    ReplyDelete