Using Maven Profiles and Assembly Build Customized Applications
Want to learn more about Maven profiling in Java? Check out this tutorial on how to use Maven profiling and assembly customized applications.
Join the DZone community and get the full member experience.
Join For FreeThe application that my team is working on has multiple projects in it. Each project defines a distinct functionality of the application. We had to maintain distinct functionality that was separately executable and able to be merged with the main application. Because of this, the requirement was to create war files of distinct functionality projects and one deployable main war file that includes all the functionalities. We resolved this by using Maven profiles.
While creating one main deployable war file, the main concern was to create and maintain one session factory on the hibernate part that has to be used by the all dependent components. While creating a war of each distinct functionality, we included the hibernate configurations through the Maven profile, but, at the time of creation, there was one main war with dependent functionalities as a jar file. We added a common hibernate configuration file that has a reference of all dependent hibernate configurations present in each jar file through Maven profiling.
Maven Profiling
Maven profiles allow a parameterization of the build process with the purpose of making the target application portable across several deployment environments.
Project Directory Details
The main web app project includes configuration files for assembling all the dependent projects— Project 1 to 5. Each project represents a special functionality, which can be executed separately as war.
Com
-Sample-
-main web app project (war file)
-project1 (jar file)
-project2 (jar file)
-project3 (jar file)
-project4 (jar file)
-project5 (jar file)
Build Requirement
There are two requirements:
- Build a common war file that includes all the projects
This includes the creation of jar files of a dependent project and war file that includes configurations related to a dependent project.
- Build war of each project to execute the individual project as a separate war
This includes the creation of a war file of each dependent project separately.
Solution
We used Maven profiling to resolve this requirement. We created the tow maven profiles name ‘war’ and ‘jar,' where war profiles are set to default. When dependent projects are built using Maven, then, the war file project will be created.
When the profile is selected as a jar, then, the jar files of a dependent project are created and the war file of the main web app projects is created. This includes jar files of a dependent project. Below is the sample code.
- Creation of Maven profiles:
Depending upon the Maven profile, the packaging type of pom.xml is selected.
A "profiles" section in the pom.xml file can contain one or more profile definitions.
<groupId>com.sample.project </groupId>
<artifactId>sampleproject</artifactId>
<version>1.0.0</version>
<name></name>
<packaging>${packaging.type}</packaging>
<url>http://maven.apache.org</url>
<!--MAVEN PROFILE + SPRING PROFILE STARTS -->
<profiles>
<!-- profile id war : to run this application as separate war -->
<profile>
<id>war</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<packaging.type>war</packaging.type>
<env.use.native.hostids>false</env.use.native.hostids>
<env.spring.profiles>default, standlone-war</env.spring.profiles>
</properties>
</profile>
<!-- profile id jar : to run this application as separate jar -->
<profile>
<id>jar</id>
<properties>
<packaging.type>jar</packaging.type>
</properties>
</profile>
</profiles>
<!--MAVEN PROFILE+SPRING PROFILE ENDS -->
- Modification in Spring Configuration file
- Below are the commands to execute both profiles
- Using the Maven profile: war
Opinions expressed by DZone contributors are their own.
Comments