DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Maven Dependency Scope Applied
  • A Maven Story
  • Ensuring Reliable Microservice Deployment With Spring Boot Build Info Maven Plugin
  • Integrate Cucumber in Playwright With Java

Trending

  • Evals in CI: How to Catch Agent Regressions Before Production Does
  • How to Build Your Own AI SRE From Scratch
  • Why Kotlin or Java Is the Wrong Question For Enterprises
  • Beyond the Endpoint: How Agent Cards Enable Discovery, Interoperability, and Trust

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. Using Maven Profiles and Assembly Build Customized Applications

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.

By 
Mandar Dharurkar
·
Jul. 30, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
14.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

The 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
Profile (engineering) Apache Maven application Build (game engine) WAR (file format) Assembly (CLI)

Opinions expressed by DZone contributors are their own.

Related

  • Maven Dependency Scope Applied
  • A Maven Story
  • Ensuring Reliable Microservice Deployment With Spring Boot Build Info Maven Plugin
  • Integrate Cucumber in Playwright With Java

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook