Using Maven overlays to build customized applications
![]() on April 9, 2009 IntroductionIn this tutorial we'll see two things.First we'll do a very simple customization of Portofino: changing the logo and css stylesheet. Second we'll use Maven to create a new mywebapp.war which bundles, in a single .war, the standard Portofino distribution, the customizations and the portofino-custom.properties configuration. In this way, the mywebapp.war that contains the customized application will be self-contained, professional and easy to deploy. The problemIf I had to create and distribute an application based on Portofino I would:
Maven overlaysAll the manual steps mentioned above can be automated using Maven overlays. The process becomes repeatable, less error-prone, and overall simpler.Maven is a build/dependency/project management tool. For an overview, see the Maven in 5 minutes guide. Overlays are a specific, advanced use of Maven. Fundamentally they allow you to build a target war (mywebapp.war) as a "merge" between an original war (the Portofino standard distribution) and other code/files that you develop. To visualize how this "merge" works, see Manipulating WAR Overlays. Installing MavenFrom a shell/command line, check if Maven is already installed on your system:
$ mvn -version
Maven version: 2.0.10 Any version of Maven since 2.0.6 is suitable for our purposes. To download and install Maven, follow the instructions here: http://maven.apache.org/download.html. Installing Portofino in the local repositoryThis is a preliminary step that you need to do once on your system. Repeat it only when a new version of Portofino becomes available.Download Portofino. Expand the archive and check that the file "portofino-war-3.1.0.war" is present. Then type the following command:
mvn install:install-file -DgroupId=com.manydesigns \
-DartifactId=portofino-war -Dversion=3.1.0 -Dpackaging=war \ -Dfile=/path/to/portofino-war-3.1.0.war Notice that you must change "/path/to/portofino-war-3.1.0.war" to the location where you've actually downloaded Portofino. Creating the maven overlay projectTo kick-start the new maven project we'll use an archetype, a kind of template for various types of project. We choose the "maven-archetype-webapp", which is an archetype suitable for any war-based applications.Before you start, you need to decide the groupId and artifactId for the customized application, roughly equivalent to an identifier of your organization and an identified of your webapp. We'll use:
mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=mywebapp \
-DarchetypeArtifactId=maven-archetype-webapp Maven will run and log several messages. Make sure the last few lines look like this:
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------ [INFO] Total time: 4 seconds [INFO] Finished at: Mon Apr 06 14:56:39 CEST 2009 [INFO] Final Memory: 8M/14M A new directory called "my-webapp" has been created. Let's have a look at it.
$ cd mywebapp
$ find . . ./pom.xml ./src ./src/main ./src/main/resources ./src/main/webapp ./src/main/webapp/index.jsp ./src/main/webapp/WEB-INF ./src/main/webapp/WEB-INF/web.xml As you can see, a standard directory structure has been created (for more information on this, see the Introduction to the standard directory layout). Now edit the pom.xml file and add the section in bold:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>mywebapp</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>mywebapp Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>com.manydesigns</groupId> <artifactId>portofino-war</artifactId> <version>3.1.0</version> <type>war</type> <scope>compile</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>mywebapp</finalName> </build> </project> Now we're ready to run our first build. Type:
mvn package
And make sure you get the BUILD SUCCESSFUL message. Have a look at a folder called "target", which has just been created. It should contain a file called "mywebapp.war" as well as an expanded version of it in the "mywebapp" directory. Well, that is the overlaid war target. It still doesn't do what we want, but we're getting closer. Cleaning up some files we don't needBefore proceeding, make sure you delete two files that were automatically created by the archetype:
Adding the portofino-custom.properties
The portofino-custom.properties can be easily bundled inside mywebapp.war. Create the file: |
