------ Creating Skinny WARs ------ Mike Perham ------ 2008-08-03 ------ ~~ Licensed to the Apache Software Foundation (ASF) under one ~~ or more contributor license agreements. See the NOTICE file ~~ distributed with this work for additional information ~~ regarding copyright ownership. The ASF licenses this file ~~ to you under the Apache License, Version 2.0 (the ~~ "License"); you may not use this file except in compliance ~~ with the License. You may obtain a copy of the License at ~~ ~~ http://www.apache.org/licenses/LICENSE-2.0 ~~ ~~ Unless required by applicable law or agreed to in writing, ~~ software distributed under the License is distributed on an ~~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY ~~ KIND, either express or implied. See the License for the ~~ specific language governing permissions and limitations ~~ under the License. ~~ NOTE: For help with the syntax of this file, see: ~~ http://maven.apache.org/doxia/references/apt-format.html Creating Skinny WARs In a typical J2EE environment, a WAR is packaged within an EAR for deployment. The WAR can contain all its dependent JARs in <<>> but then the EAR can quickly grow very large if there are multiple WARs, due to the presence of duplicate JARs. Instead the J2EE specification allows WARs to reference external JARs packaged within the EAR via the <<>> setting in their <<>>. The Maven WAR and EAR Plugins do not directly support this mode of operation but we can fake it through some POM and configuration magic. First you need to tell Maven to exclude the dependent JARs and add references to them in the <<>> instead. This goes into your WAR project's <<>>: +-----------------+ ... maven-war-plugin ${project.version} WEB-INF/lib/*.jar true lib/ ... +-----------------+ Here's another variant of the above example, but this time we use <<<\>>> to select a few JARs to be included in the WAR. This is useful when there is a need to package a small, but non-empty, subset of JARs into the WAR. When making an EAR of skinny WARs, one wants to package all of the JARs into the EAR. Sometimes a list of JARs must be packaged into the WAR though in order for it to work properly, like with tag libraries. +-----------------+ ... maven-war-plugin ${project.version} WEB-INF/lib/my-tag-library.jar,**/*.xml,**/*.properties,**/*.class,**/*.png,**/*.css,**/*.js,**/*.jsp true lib/ ... +-----------------+ Next we need to change the EAR project's <<>> to package those dependent JARs in the EAR. Notice that we package everything into a <<>> directory within the EAR. This is just my own personal preference to distinguish between J2EE modules (which will be packaged in the root of the EAR) and Java libraries (which are packaged in <<>>). +-----------------+ ... maven-ear-plugin 2.3.2 lib/ ... +-----------------+ Now the painful part. Your EAR project's <<>> needs to list every dependency that the WAR has. This is because Maven assumes fat WARs and does not include transitive dependencies of WARs within the EAR. +-----------------+ .... com.acme shared-jar 1.0.0 com.acme war1 1.0.0 war com.acme war2 1.0.0 war ... +-----------------+ Your EAR will contain something like this: +-----------------+ . |-- META-INF | `-- application.xml |-- lib | `-- shared-jar-1.0.0.jar |-- war1-1.0.0.war `-- war2-1.0.0.war +-----------------+ * Alternatives Our users have submitted alternatives to the above recipe on {{{http://docs.codehaus.org/display/MAVENUSER/Solving+the+Skinny+Wars+problem}the Wiki}}.