------ Adding and Filtering External Web Resources ------ Pete Marvin King Dennis Lundberg ------ 2012-09-28 ------ ~~ 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 Adding and Filtering External Web Resources The default resource directory for all Maven projects is <<>> which will end up in <<>> and in <<>> in the WAR. The directory structure will be preserved in the process. The WAR Plugin is also capable of including resources not found in the default resource directory through the <<>> parameter. *Adding web resources +-------+ ... org.apache.maven.plugins maven-war-plugin ${project.version} resource2 ... +-------+ Using our sample project in the {{{../usage.html}usage section}} with an added external resource, like this: +----------+ . |-- pom.xml |-- resource2 | |-- external-resource.jpg | `-- image2 | `-- external-resource2.jpg `-- src `-- main |-- java | `-- com | `-- example | `-- projects | `-- SampleAction.java |-- resources | `-- images | `-- sampleimage.jpg `-- webapp |-- WEB-INF | `-- web.xml |-- index.jsp `-- jsp `-- websource.jsp +----------+ would end up in the WAR as: +----------+ documentedproject-1.0-SNAPSHOT.war |-- META-INF | |-- MANIFEST.MF | `-- maven | `-- com.example.projects | `-- documentedproject | |-- pom.properties | `-- pom.xml |-- WEB-INF | |-- classes | | |-- com | | | `-- example | | | `-- projects | | | `-- SampleAction.class | | `-- images | | `-- sampleimage.jpg | `-- web.xml |-- external-resource.jpg |-- image2 | `-- external-resource2.jpg |-- index.jsp `-- jsp `-- websource.jsp +----------+ <<>> and <<>> are copied to the root of the WAR, preserving the directory structure. *Configuring web Resources <<>> is a list of resources. All options of resource are supported. A web resource * can have includes/excludes * can be filtered * is not limited to the default destination - the root of the WAR **Includes/Excludes To include all jpgs in the WAR we can add the following to our POM configuration from above: +----------+ ... resource2 **/*.jpg ... +----------+ To exclude the <<>> directory from the WAR add this: +----------+ ... resource2 **/image2 ... +----------+ Be careful when mixing includes and excludes, excludes will have a higher priority. Includes can not override excludes if a resource matches both. Having this configuration will exclude all jpgs from the WAR: +----------+ ... resource2/ image2/*.jpg **/*.jpg ... +----------+ Here's another example of how to specify include and exclude patterns: +----------+ ... resource2 **/pattern1 *pattern2 *pattern3/pattern3 pattern4/pattern4 ... +----------+ **Filtering Using our example above, we can also configure filters for our resources. We will add a hypothetical <<>> directory to our project: +----------+ . |-- configurations | |-- config.cfg | `-- properties | `-- config.prop |-- pom.xml |-- resource2 | |-- external-resource.jpg | `-- image2 | `-- external-resource2.jpg `-- src `-- main |-- java | `-- com | `-- example | `-- projects | `-- SampleAction.java |-- resources | `-- images | `-- sampleimage.jpg `-- webapp |-- WEB-INF | `-- web.xml |-- index.jsp `-- jsp `-- websource.jsp +----------+ To prevent corrupting your binary files when filtering is enabled, you can configure a list of file extensions that will not be filtered. +----------+ ... properties/config.prop pdf resource2 false configurations true **/properties ... +----------+ *** <<>> +----------+ interpolated_property=some_config_value +----------+ *** <<>> +----------+ ${interpolated_property} +----------+ The resulting WAR would be: +----------+ documentedproject-1.0-SNAPSHOT.war |-- META-INF | |-- MANIFEST.MF | `-- maven | `-- com.example.projects | `-- documentedproject | |-- pom.properties | `-- pom.xml |-- WEB-INF | |-- classes | | |-- com | | | `-- example | | | `-- projects | | | `-- SampleAction.class | | `-- images | | `-- sampleimage.jpg | `-- web.xml |-- config.cfg |-- external-resource.jpg |-- image2 | `-- external-resource2.jpg |-- index.jsp `-- jsp `-- websource.jsp +----------+ and the content of <<>> would be: +----------+ some_config_value +----------+ <> In versions 2.2 and earlier of this plugin the platform encoding was used when filtering resources. Depending on what that encoding was you could end up with scrambled characters after filtering. Starting with version 2.3 this plugin respects the property <<>> when filtering resources. One notable exception to this is that <<<.xml>>> files are filtered using the encoding specified inside the xml-file itself. **Overriding the default destination directory By default web resources are copied to the root of the WAR, as shown in the previous example. To override the default destination directory, specify the target path. +----------+ ... ... configurations WEB-INF true **/properties ... +----------+ Using the sample project the resulting WAR would look like this: +----------+ documentedproject-1.0-SNAPSHOT.war |-- META-INF | |-- MANIFEST.MF | `-- maven | `-- com.example.projects | `-- documentedproject | |-- pom.properties | `-- pom.xml |-- WEB-INF | |-- classes | | |-- com | | | `-- example | | | `-- projects | | | `-- SampleAction.class | | `-- images | | `-- sampleimage.jpg | |-- config.cfg | `-- web.xml |-- external-resource.jpg |-- image2 | `-- external-resource2.jpg |-- index.jsp `-- jsp `-- websource.jsp +----------+