------ Resource Transformers ------ Mauro Talevi ------ 2008-07-21 ------ ~~ 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. Resource Transformers Aggregating classes/resources from several artifacts into one uber JAR is straight forward as long as there is no overlap. Otherwise, some kind of logic to merge resources from several JARs is required. This is where resource transformers kick in. *-----------------------------------------+------------------------------------------+ | {{ApacheLicenseResourceTransformer}} | Prevents license duplication | *-----------------------------------------+------------------------------------------+ | {{ApacheNoticeResourceTransformer}} | Prepares merged NOTICE | *-----------------------------------------+------------------------------------------+ | {{AppendingTransformer}} | Adds content to a resource | *-----------------------------------------+------------------------------------------+ | {{ComponentsXmlResourceTransformer}} | Aggregates Plexus <<>> | *-----------------------------------------+------------------------------------------+ | {{DontIncludeResourceTransformer}} | Prevents inclusion of matching resources | *-----------------------------------------+------------------------------------------+ | {{GroovyResourceTransformer}} | Merges Apache Groovy extends modules | *-----------------------------------------+------------------------------------------+ | {{IncludeResourceTransformer}} | Adds files from the project | *-----------------------------------------+------------------------------------------+ | {{ManifestResourceTransformer}} | Sets entries in the <<>> | *-----------------------------------------+------------------------------------------+ | {{PluginXmlResourceTransformer}} | Aggregates Mavens <<>> | *-----------------------------------------+------------------------------------------+ | {{ResourceBundleAppendingTransformer}} | Merges ResourceBundles | *-----------------------------------------+------------------------------------------+ | {{ServicesResourceTransformer}} | Relocated class names in <<>> resources and merges them. | *-----------------------------------------+------------------------------------------+ | {{XmlAppendingTransformer}} | Adds XML content to an XML resource | *-----------------------------------------+------------------------------------------+ Transformers in <<>> * Merging Plexus Component Descriptors with the {ComponentsXmlResourceTransformer} JARs for components targeting the Plexus IoC container contain a <<>> entry that declares the component and its requirements. If the uber JAR aggregates multiple Plexus components, a <<>> needs to be used to merge the XML descriptors: +----- ... org.apache.maven.plugins maven-shade-plugin ${project.version} shade ... +----- Since plugin version 1.3, this resource transformer will also update the descriptor to account for relocation of component interfaces/implementations (if any). * Relocate classes of the Maven Plugin Descriptor with the {PluginXmlResourceTransformer} With {{{http://maven.apache.org/plugin-tools/index.html}Plugin Tools 3.0}} annotations have been introduced. Now references to classes are no longer classnames as String, but the actual Class reference. When you wanted to relocate classes, you had to maintain the <<>> by hand, but now this can be done with the <<>> +----- ... org.apache.maven.plugins maven-shade-plugin ${project.version} shade ... +----- * Concatenating Service Entries with the {ServicesResourceTransformer} JAR files providing implementations of some interfaces often ship with a <<>> directory that maps interfaces to their implementation classes for lookup by the service locator. To relocate the class names of these implementation classes, and to merge multiple implementations of the same interface into one service entry, the <<>> can be used: +----- ... org.apache.maven.plugins maven-shade-plugin ${project.version} shade ... +----- * Merging Content of Specific Files with {AppendingTransformer}, XmlAppendingTransformer and ResourceBundleAppendingTransformer Some jars contain additional resources (such as properties files) that have the same file name. To avoid overwriting, you can opt to merge them by appending their content into one file. One good example for this is when aggregating both the spring-context and plexus-spring jars. Both of them have the <<>> file which is used by Spring to handle XML schema namespaces. You can merge the contents of all the files with that specific name using the <<>> as shown below: +----- ... org.apache.maven.plugins maven-shade-plugin ${project.version} shade META-INF/spring.handlers META-INF/spring.schemas ... +----- For XML files, you can use the <<<{XmlAppendingTransformer}>>> instead: +----- ... org.apache.maven.plugins maven-shade-plugin ${project.version} shade META-INF/magic.xml ... +----- Since plugin version 1.3.1, the <<>> will by default not load DTDs, thereby avoiding network access. The potential downside of this mode is that external entities cannot be resolved which could fail the transformation, e.g. when using the Crimson XML parser as used in some JRE 1.4. If the transformed resource uses external entities, DTD resolution can either be turned back on or a plugin dependency on <<>> is added to the POM. For ResourceBundles properties files, you can use the <<<{ResourceBundleAppendingTransformer}>>> instead, which will respect all available Locales as well: +----- ... org.apache.maven.plugins maven-shade-plugin ${project.version} shade path/to/Messages ... +----- * Excluding Resources with the {DontIncludeResourceTransformer} The <<>> allows resources to be excluded when their name ends in a given value. For example, the following sample excludes all resources ending in <<<.txt>>>. +----- ... org.apache.maven.plugins maven-shade-plugin ${project.version} shade .txt ... +----- Since maven-shade-plugin-3.0 it is also possible to give a list of resources which should not be included, like: +----- .txt READ.me +----- * Adding New Resources with the {IncludeResourceTransformer} The <<>> allows project files to be included in the package under a given name. For example, the following sample includes <<>> in the package as <<>> in the <<>> directory. +----- ... org.apache.maven.plugins maven-shade-plugin ${project.version} shade META-INF/README README.txt ... +----- * Setting Manifest Entries with the {ManifestResourceTransformer} The <<>> allows existing entries in the <<>> to be replaced and new entries added. For example, the following sample sets * the <<>> entry to the value of the <<>> property, * the <<>> entry to the value of the <<>> property and * the <<>> entry to the value of the <<>> property. +----- ... org.apache.maven.plugins maven-shade-plugin ${project.version} shade ${app.main.class} ${maven.compile.source} ${maven.compile.target} ... +----- * Licensing ** Preventing License Duplication with the {ApacheLicenseResourceTransformer} Some open source producers (including the {{{http://www.apache.org} Apache Software Foundation}}) include a copy of their license in the META-INF directory. These are conventionally named either <<>> or <<>>. When merging these dependencies, adding these resources may cause confusion. The <<>> ensures that duplicate licenses (named according to this convention) are not merged. For example, the following prevents the license from a <<>> dependency being merged in +----- ... org.apache.maven.plugins maven-shade-plugin ${project.version} shade ... +----- ** Aggregating Notices with the {ApacheNoticeResourceTransformer} Some licenses (including the {{{http://www.apache.org/licenses/LICENSE-2.0.html} Apache License, Version 2}}) require that notices are preserved by downstream distributors. <<>> automates the assembly of an appropriate <<>>. For example, to simply merge in dependent notices: +----- ... org.apache.maven.plugins maven-shade-plugin ${project.version} shade false ... +----- ** Aggregating Apache Groovy extension modules descriptors with the {GroovyResourceTransformer} The Apache Groovy language provides extension modules located at <<>>, these modules use the property file format. <<>> automates the assembly of Groovy extension modules <<>>. For example, to simply merge the extension modules of several jars: +----- ... org.apache.maven.plugins maven-shade-plugin ${project.version} shade the-aggregated-module 1.0.0 ... +-----