Overlay BIRT runtime into correct WAR locations - maven-2

I'm trying to get Maven to assemble a WAR with the BIRT runtime in a useful location within the WAR.
The BIRT runtime is in the pom.xml as
<dependency>
<groupId>org.eclipse.birt</groupId>
<artifactId>report-engine</artifactId>
<version>2.3.2</version>
<type>zip</type>
<scope>runtime</scope>
</dependency>
The desired outcome of overlaying this is something like
ReportEngine/lib/* -> WEB-INF/lib
ReportEngine/configuration/* -> WEB-INF/platform/configuration
ReportEngine/plugins/* -> WEB-INF/platform/plugins
My overlay configuration looks like
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<overlays>
<overlay>
<groupId>org.eclipse.birt</groupId>
<artifactId>report-engine</artifactId>
<type>zip</type>
<includes>
<include>ReportEngine/lib/*</include>
</includes>
<targetPath>WEB-INF/lib</targetPath>
</overlay>
<overlay>
<groupId>org.eclipse.birt</groupId>
<artifactId>report-engine</artifactId>
<type>zip</type>
<includes>
<include>ReportEngine/configuration/*</include>
<include>ReportEngine/plugins/*</include>
</includes>
<targetPath>WEB-INF/platform</targetPath>
</overlay>
</overlays>
</configuration>
</plugin>
Of course, when running mvn war:exploded I'm seeing
ReportEngine/lib/* -> WEB-INF/lib/ReportEngine/lib/
ReportEngine/configuration/* -> WEB-INF/platform/configuration/ReportEngine/lib/
ReportEngine/plugins/* -> WEB-INF/platform/plugins/ReportEngine/lib/
This relates, same sort of problem, no answer
http://www.coderanch.com/t/447258/Ant-Maven-Other-Build-Tools/Maven-war-dependencies-moving-files
Bonus points for pointing out how I can tidy this up a bit by having it all work from within WEB-INF/birt-runtime
Edit:
The reason for the locations specified above are that they match those indicated in http://wiki.eclipse.org/Servlet_Example_%28BIRT%29_2.1 and when I fritz with the Tomcat installation to mimic this, it all seems to work. It would be ideal if I could simply overlay the zip into WEB-INF/birt-runtime and then set the engine config appropriately, but I've not found that to work as yet.
Eg:
engineConfig = new EngineConfig();
engineConfig.setEngineHome("WEB-INF/birt-runtime");
engineConfig.setPlatformContext(new PlatformServletContext(servletContext));

Update: On rereading the question I realise I missed out the sub-directories from my test project, so of course it worked for me, sorry for that.
As far as I know there exists no mechanism in either the war overlay or the dependency-plugin to unpack sub-folders of artifacts to a directory and exclude the parent elements of the path, both are going to give you the full relative path.
You can however use the unpack goal to unpack the archive to a temporary folder, then use the antrun-plugin to copy the required subfolders to their final resting places.
The following configuration would do just that (I've not yet tested this so apologies if there are any omissions, see the documentation for exact details). Note the executions are in the same phase, but as long as the dependency-plugin is configured before the antrun-plugin it would be executed first. Note, the prepare-package is new for Maven 2.1, if you're on an older version you'd need to use another phase.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-lib</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.eclipse.birt</groupId>
<artifactId>report-engine</artifactId>
<version>2.3.2</version>
<type>jar</type>
<overWrite>false</overWrite>
</artifactItem>
</artifactItems>
<!--unpack all three folders to the temporary location-->
<includes>ReportEngine/lib/*,ReportEngine/configuration/*,ReportEngine/plugins/*</includes>
<outputDirectory>${project.build.directory}/temp-unpack</outputDirectory>
<overWriteReleases>false</overWriteReleases>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<configuration>
<tasks>
<!--now copy the configuration and plugin sub-folders to WEB-INf/platform-->
<copy todir="${project.build.directory}/WEB-INF/platform">
<fileset dir="${project.build.directory}/temp-unpack/ReportEngine/configuration"/>
<fileset dir="${project.build.directory}/temp-unpack/ReportEngine/plugins"/>
</copy>
<!--copy the lib sub-folder to WEB-INf/lib-->
<copy todir="${project.build.directory}/WEB-INF/lib">
<fileset dir="${project.build.directory}/temp-unpack/ReportEngine/lib"/>
</copy>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

Not really answering my own question, responding to Rich Seller above :)
Trying to get this to work with mvn dependency:unpack, the docs say to take it out of the executions node. Not sure if that is the cause of the result, but this ends up with
WEB-INF/lib/ReportEngine/lib
WEB-INF/platform/ReportEngine/configuration
WEB-INF/platform/ReportEngine/plugins
basically the same as my initial war-plugin attempt.
I don't see anything in the docs for depedency-unpack to assist. I'll try again tmrw.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.eclipse.birt</groupId>
<artifactId>report-engine</artifactId>
<version>2.3.2</version>
<type>zip</type>
<overWrite>false</overWrite>
<!--may not be needed, need to check-->
<outputDirectory>${project.build.directory}/WEB-INF/lib</outputDirectory>
<includes>ReportEngine/lib/*</includes>
</artifactItem>
<artifactItem>
<groupId>org.eclipse.birt</groupId>
<artifactId>report-engine</artifactId>
<version>2.3.2</version>
<type>zip</type>
<overWrite>false</overWrite>
<!--may not be needed, need to check-->
<outputDirectory>${project.build.directory}/WEB-INF/platform</outputDirectory>
<includes>ReportEngine/configuration/*,ReportEngine/plugins/*</includes>
</artifactItem>
</artifactItems>
</configuration>
</plugin>

Related

How can I strip the version from the output directory of maven-dependency-plugin:unpack?

I need to unpack an artifact, and I need to use it's unpacked location in several places (in multiple files). I don't want to have to update all the copies of that location every time I change versions. Is there a way I can strip the version from the output directory? It doesn't look like stripVersion is supported with the unpack mojo.
Does the plugin you are using to create the artifact provide a xyzName property? For example the jar-plugin provides the finalName and the war-plugin has the warName.
If so you can set the property to some fixed value.
Try this:
<properties>
<extracted-artifact-location>${project.build.directory}/extracted-artifact</extracted-artifact-location>
</properties>
...
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId><fixme></groupId>
<artifactId><fixme></artifactId>
<version><fixme></version>
<outputDirectory>${extracted-artifact-location}</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

How can I test for spaces in the path and give an appropriate error?

I am trying to write an integration test POM for our project. We've got the project downloading and installing/unzipping appropriately (using the maven-dependency-plugin), but one caveat of our project is that it can't yet run in paths that have spaces in them. I'm looking for a very simple way to evaluate ${project.build.directory} and throw a human readable error if it contains spaces. I'd like this to happen BEFORE downloading dependencies, since this takes quite a bit of time.
This will do it with the help of antrun plugin and <contains> condition.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<fail message="project.build.directory(${project.build.directory}) contains spaces">
<condition>
<contains string="${project.build.directory}" substring=" "/>
</condition>
</fail>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

How to copy specified files whitout folder structure using overlays?

I would like copy just "file.xml" without folder structure using overlays like this:
<overlays>
<overlay>
<groupId>com.mygroup</groupId>
<artifactId>my_comp</artifactId>
<includes>
<include>WEB-INF/folder1/folder2/file.xml</include>
</includes>
<targetPath>WEB-INF/otherFolder</targetPath>
<type>war</type>
</overlay>
</overlays>
In other words: copy file.xml from WEB-INF/folder1/folder2/ and place to the WEB-INF/otherFolder
Any ideas?
I didn't find how to resolve the issue via overlays. So I had to use two plugins maven-dependency-plugin and maven-war-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>copy</id>
<phase>process-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.mygroup</groupId>
<artifactId>my_comp</artifactId>
<type>war</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/tmp</outputDirectory>
<includes>WEB-INF/folder1/folder2/file.xml</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1-beta-1</version>
<configuration>
<webResources>
<resource>
<directory>${project.build.directory}/tmp/WEB-INF/folder1/folder2</directory>
<targetPath>WEB-INF/otherFolder</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
The first plugin attached to the process-resources phase. The second invoked on phase package when all overlays combined. Overlays are applied with a first-win strategy (hence if a file has been copied by an overlay, it won't be copied anymore) If I've copy my file.xml (via plugin) then it not be overwritten by any overlay.
It's so complicated!
To my knowledge, this is not possible with overlays, the content of the overlay is added "as is" to the targetPath (that defaults to the root structure of the webapp).
If you want to make file.xml available at another location, you'll have to tweak its location in my_comp WAR before the overlay.

Maven-assembly-plugin: custom jar filenames

I use the assembly plugin to create several jars with some classes in it. I need custom names for the resulting jars: app_business.jar app_gui.jar core.jar etc.
Currently I have to following configuration:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<finalName>app_business</finalName>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<attach>true</attach>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
an the assembly.xml file:
<assembly>
<id>app_business</id>
<formats>
<format>jar</format>
</formats>
<baseDirectory>target</baseDirectory>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>org/xyz/**</include>
</includes>
</fileSet>
</fileSets>
</assembly>
this creates a file app_business.jar which is perfect. But i have no idea how to create my other files. The option appendAssemblyId doesn't help me, as it creates filenames in the format AppName-app_business.jar. I really need the exact filesname app_business.jar.
Any Idea? Thank you very much!
You can move the configuration element below the execution element of the plugin declaration. This means the configuration will only be applied to that execution. You can then add additional executions of the assembly plugin for your other assemblies.
Here is an example of the modified configuration with two executions, each referencing a different assembly:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-business-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<finalName>app_business</finalName>
<descriptors>
<descriptor>src/main/assembly/business-assembly.xml</descriptor>
</descriptors>
<attach>true</attach>
</configuration>
</execution>
<execution>
<id>make-gui-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<finalName>app_gui</finalName>
<descriptors>
<descriptor>src/main/assembly/gui-assembly.xml</descriptor>
</descriptors>
<attach>true</attach>
</configuration>
</execution>
</executions>
</plugin>
With this configuration, two additional jars (app _business.jar and app _gui.jar) will be created in the target directory, though be aware if you install the project, only the last artifact assembled will be installed (this could of course be a problem).
To avoid this you would need to change the appendAssemblyId properties to true. The closest you can get in this case is to change the finalNames to "app" and the IDs to "gui" and "business", resulting in app-gui.jar and app-business.jar being packaged and all artifacts being installed.
The standard maven plugins are meant for general, repetitive work. They gather all information from the POM and they are smart (in the sense that you don't need to configure much).
If you need special tasks, then I suggest to use the ant plugin which allows you to embed a piece of Ant code in the POM. This allows you to run the jar task.

Can maven sign not only produced jar, but also dependencies

I managed to create main jar, copy dependencies to a single directory, the only step left is to sign all jars.
I can sign my own produced jar as a part of jar:sign, but how do i sign dependencies?
Thanks
Here are a couple of options:
Use the Maven ant task to run jarsigner from the JDK against all the dependencies.
Use the webstart plugin which can sign all your JARs, even if you aren't using it for the purpose of JNLP-izing your app. I'm using it to actually JNLPize one app.
Look at what the webstart plugin source is doing to iterate over all dependencies and sign them and start a new Maven Plugin/Mojo that does the same thing, sans JNLP.
Onejar your app and its dependencies and just sign that.
add to plug-in config <archiveDirectory>target</archiveDirectory>
If you are using maven-jar-plugin, you can specify which single jar to sign using the "jarPath" setting. The following configuration causes the jar-with-dependencies file to be signed instead of the dependency-less jar file:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>sign</goal>
</goals>
</execution>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- NOTE: The secret key is in shared version control. The
password is in shared version control. This IS NOT
SECURE. It's intended to help avoid accidentally
loading the wrong class, nothing more. -->
<jarPath>${project.build.directory}/${project.build.FinalName}-${project.packaging}-with-dependencies.${project.packaging}</jarPath>
<keystore>${basedir}/keystore</keystore>
<alias>SharedSecret</alias>
<storepass>FOO</storepass>
</configuration>
</plugin>
If you want to sign both, I don't know how to do that with maven-jar-plugin, so you may need to look into the other options mentioned above.
One can also create a single JAR using the maven-assembly-plugin.
Together with the other suggestion by Eric Anderson (of signing another JAR) one can then sign this assembled JAR (instead of the original JAR). Note that the order of the plugin definitions matters here.
It is assumed that sign.keystore.file etc are set elsewhere (e.g. in a profile).
<build>
<plugins>
<!-- It seems that maven-assembly-plugin must be declared before the maven-jar-plugin,
so that it is executed first in the package phase,
and then the signing of the packaged jar can succeed. -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifestEntries>
<!-- ... -->
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
</execution>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>sign</goal>
</goals>
<configuration>
<jarPath>${project.build.directory}/${project.build.FinalName}-${project.packaging}-with-dependencies.${project.packaging}</jarPath>
<keystore>${sign.keystore.file}</keystore>
<type>${sign.keystore.type}</type>
<storepass>${sign.keystore.storepass}</storepass>
<alias>${sign.keystore.alias}</alias>
<verify>true</verify>
<verbose>false</verbose>
<removeExistingSignatures>true</removeExistingSignatures>
</configuration>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<!-- <addClasspath>true</addClasspath> -->
</manifest>
<manifestEntries>
<!-- ... -->
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>