Using Maven Shade Plug-in in Portal Development
The Scenario
In some portal development projects, when the functionalities are common for a set of portlets and custom filters, we put them in the jar file and call them as common libraries or provider jars. It increases the modularity of the code, which in return helps in maintainability of the code. Usually these functionalities do not require any externalization, so we don’t require any .properties files. Even if we have any .properties files, the portlets would work fine, because the jar files can be bundled in the “ WEB-INF “ folder of the portlets.
But if you use these provider jars in custom filters or any other jar files, this would result in class loading errors, because the class loader would not be able to load the .properties files of the providers.
The Challenge
The Challenge is to make the custom filters or other jar files (the consumers) to work with the jars with .properties files( providers) .
The solution
The proposed solution is to bundle the consumers with providers as a single jar so that the class loading issue can be resolved. For this we can use the maven shade plug-in. When you add maven shade plug-in, in the build part of the pom file, it scans the given dependencies , if the scope of a dependency is not “provided” , (i.e <scope>provided</scope> ) it bundles the classes and .properties of that dependency jar(provider) as part of the jar, that is being built(consumer).
At the end of the build, we would be having a jar file, that consists of consumer classes as well as providers classes and their .properties files. This would resolve the class loading issues, because the .properties files are part of the jar itself.
You can customize the shade plug-in as per your requirement. If you are using spring jars in your application, the shade plug-in, overrides your spring handlers and schemas, with its own version. To avoid this you can apply transformers as shown in the sample code snippet.
For more information on customization you can look in to http://maven.apache.org/plugins/maven-shade-plugin/
Sample Code snippet.
<build>
<finalName>${project.name}</finalName>
<plugins>
<plugin>
<groupId>com.ibm.tools.plugin</groupId>
<artifactId>portal-deployment-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>*:*</include>
</includes>
</artifactSet>
<transformers>
<transformer
implementation=”org.apache.maven.plugins.shade.resource.AppendingTransformer”>
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation=”org.apache.maven.plugins.shade.resource.AppendingTransformer”>
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>