Integrating ant resource generation targets into a Maven build - maven

I'm currently working on a fairly large project that has been migrated from Ant to Maven. There are no problems with the actual build process (it compiles and packages the source code fine).
The problem is that I also have a lot of targets that generate additional resources for the project (compile LessCSS, generate & upload documentation, generate tld files for custom tags and functions etc.). I am not sure how I should handle these tasks. Let's take the target that builds CSS&JS as an example (the others are more or less similar, but not connected). It looks like this (simplified):
<target name="build.css_js">
<concat destfile="${webapp.dir}/scripts/main.js">
<fileset dir="${webapp.dir}/scripts/src" includes="*.js"/>
</concat>
<!-- build css files from less sources -->
<taskdef name="lesscss" classname="com.asual.lesscss.LessEngineTask" classpathref="libraries" />
<lesscss input="${webapp.dir}/syles/src/input.less" output="${webapp.dir}/styles/output.css" />
</target>
In the pom.xml I have the following plugin set up:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<configuration>
<tasks>
<echo message="Hello World from pom.xml"/>
<ant target="build.css_js"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
The dependencies I'm using are no longer in our SVN repository (since they are managed by Maven), so I switched the libraries variable to point to the Maven repo:
<property name="lib.dir" location="${env.HOMEPATH}/.m2/repository" />
This is not good, as that path may be valid only on my machine. I don't know any other way to reference the libraries from the Maven repository and I need them to run the ant targets.
Is my approach ok or is there a better way of doing things?
How do I get over the library problem?
Some resources are needed when packaging the project but some are not. Is there a lifecycle phase that is out of scope for compile/package? I found the site lifecycle which I think fits my needs.
Ideally, I should give up on the ant build file altogether, but I'm not sure it's worth the effort of making the scripts run as maven plugins (I currently have no idea how to do that). What do you think about this?
I'm new to Maven so any suggestions are appreciated.

Generally embedding antrun calls is not ideal, but if you've not found a suitable plugin to do what you need then I wouldn't worry about it. If the processing is fairly simple it is actually quite easy to embed it in a Maven plugin yourself, see this example for help getting started.
If you are going with antrun, and the dependency jars have already been installed to your Maven repository, you can configure the antrun plugin to use those jars in its execution by adding them as dependencies of the plugin configuration. This means the dependencies will be resolved and available for use, but not be visible to your project (useful to help avoid accidental inclusion). To then access them in a portable way you can use:
<property name="lib.dir" location="${settings.localRepository}" />
Alternatively you can use some of the other properties available to expose the Maven classpaths to the antrun plugin, for example ${maven.compile.classpath} See the antrun documentation for more details.
If you have multiple discrete executions for ant, you can configure them individually in the antrun plugin and specify a suitable id for each one. The example below shows two executions, both bound to the process-resources phase. Of course you need to supply some actual goals.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>build-css</id>
<phase>generate-resource</phase>
<configuration>
<target>
...
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>build-js</id>
<phase>generate-resource</phase>
<configuration>
<target>
...
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>some.group.id</groupId>
<artifactId>artifactId</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>another.group.id</groupId>
<artifactId>anotherId</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>

Related

Are directions available for the maven plugin com.alexnederlof jasperreports-plugin?

I have read up on the limited information regarding com.alexnederlof jasperreports-plugin and I'm looking to convert my current ant build to use this maven plugin, but there doesn't seem to be any documentation available.
My biggest concern is run-time: If I use this plugin at build-time, what version of jasper-reports do I need to use at run-time?
Am I missing a reference somewhere? As the old adage goes, "If there isn't any documentation, then I guess I'll have to write it."
I am not sure of what you are after but, I am using this plugin in maven to generate the source .jrxml files to .jasper files and the configuration in pom goes like this:
<plugin>
<groupId>com.alexnederlof</groupId>
<artifactId>jasperreports-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>jasper</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- These are the default configurations: -->
<compiler>net.sf.jasperreports.engine.design.JRJdtCompiler</compiler>
<sourceDirectory>src/main/jasperreports</sourceDirectory>
<outputDirectory>${basedir}/src/main/webapp</outputDirectory>
<outputFileExt>.jasper</outputFileExt>
<xmlValidation>true</xmlValidation>
<verbose>false</verbose>
<numberOfThreads>4</numberOfThreads>
<failOnMissingSourceDirectory>true</failOnMissingSourceDirectory>
<sourceScanner>
org.codehaus.plexus.compiler.util.scan.StaleSourceScanner
</sourceScanner>
</configuration>
</plugin>
Hope this helps

Best practice in deploying a parent pom with "supporting files" (ie an external ant file)

I have a parent pom, deployed to my local artifactory server. This is a parent to several War projects. It uses maven ant run plugin and runs Ant targets defined in an external file called pombuild.xml like this:
<execution>
<id>compile</id>
<phase>deploy</phase>
<configuration>
<target if="project.is.war">
<property name="compile_classpath" refid="maven.compile.classpath"/>
<ant antfile="${ant.file}" target="compileJsp"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
The ant.file property is defined as
<ant.file>${user.home}/.m2/repository/${pom.parent.groupId}/${pom.parent.artifactId}/${pom.parent.version}/pombuild.xml</ant.file>
So I am looking for a way to have maven automatically download pombuild.xml at the same location as the parent pom.
The question is : Is this possible? And if so, is this best practice? Should I be doing it differently?
Some may say that my problem has been answered here but this is not exactly what I want.

Create Symlink for dependencies in Maven assembly

I have a Maven assembly that after unpacking the tar, creates three directories
each containing a /lib directory. So e.g.
folder1/lib
folder2/lib
folder3/lib
Currently, I am packing a same .jar in each of these /lib directories. Since this is a waste of space, I was wondering if I could have just one copy of that .jar and create something like a symlink for other two locations that could reference that .jar?
Thanks!
Here is my solution with maven-antrun-plugin for very similar situation. Zookeeper dependencies are previously placed into ${basedir}/target/package/lib by maven-dependency-plugin. Now I'm creating symlinks for all libraries into ${basedir}/target/package/lib/zookeeper/lib which point 2 dirs upper.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>prepare-delivery</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<!-- Prepare zookeeper layout. -->
<mkdir dir="${basedir}/target/package/lib/zookeeper/lib"/>
<apply executable="ln" dir="${basedir}/target/package/lib/zookeeper/lib" relative="true">
<arg value="-s"/>
<srcfile prefix="../../"/>
<targetfile/>
<fileset dir="${basedir}/target/package/lib" includes="**"/>
<mapper type="identity"/>
</apply>
</target>
</configuration>
</execution>
</executions>
</plugin>
Plugin management for antrun is set as following:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
</plugin>
I believe this is unsupported in Maven packaging and assembly. Another Stack Overflow question from just a year ago asked the same thing and got the "unsupported" answer. And there have been two JIRAs on this feature (at least) that have been open for quite some time: one for the assembly plugin and one for Plexus components. I would say this the odds of this ever being directly supported are not good.
Just FYI, if I say "source project," I'll be refering to the project that assembled the tar. If I say "destination project," I'll mean the one unpacking the tar. This is assuming you have Maven projects on both sides- if that assumption is wrong, you should rely on command line utilities to do the tar-ing or untar-ing for you.
Since it sounds to me like you are unpacking the tar in the context of a Maven build, there are ways of working around this. As far as I know, the best solution if symlinking is important to you is to use either the junction plugin, the exec-maven-plugin, or the maven-antrun-plugin.
The junction plugin has the advantage of being portable, even on Windows. The problem is the project doing the unpacking must have explicit knowledge of the structure of the original structure of the tar, which is generally not desirable as it will now have to be updated should the intended symlinking that is changed. The plugin also seems to be pretty unmaintained, so there's that.
The exec plugin will allow you to call commandline utilities or scripts to do your linking, but you will have to toy with profiles to get crossplatform capabilities. The best benefit of doing it this way is that the project doing the unpacking is completely agnostic of the original structure of the tar. The details of the mechanism are in the question from a year ago that I mentioned above.
For my project, I am probably going to use antrun for Ant's symlink task- it is possible to have Ant record all symlinks to a file on the source side, and then package that file along with Maven. The receiving project can then check for the file and recreate its symlinks. This allows the symlinking intended for the tar distribution to be changed from its source project with no changes on the destination project. My project only supports OS X and Linux, so this is acceptable- you will need to decide which is best for you.
In either case, you will unfortunately have a situation where the project doing the unpacking must have knowledge about the way the tar was before being packed.
You can use symlink task/goal of ant-run plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>package</phase>
<configuration>
<target>
<symlink link="folder2/lib/${project.artifactId}-${project.version}.jar"
resource="folder1/lib/${project.artifactId}-${project.version}.jar"/>
<symlink link="folder3/lib/${project.artifactId}-${project.version}.jar"
resource="folder1/lib/${project.artifactId}-${project.version}.jar"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
If you want the symlinks to have relative path, you can give relative path in resource like below
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>package</phase>
<configuration>
<target>
<symlink link="folder2/lib/${project.artifactId}-${project.version}.jar"
resource="../../folder1/lib/${project.artifactId}-${project.version}.jar"/>
<symlink link="folder3/lib/${project.artifactId}-${project.version}.jar"
resource="../../folder1/lib/${project.artifactId}-${project.version}.jar"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

Maven: Using different plugin configurations on e.g. deploy

For an android maven project (consisting out of a parent project that consists out of the main apk project and a test project), I would like to be able to use different plugin configurations for building the whole project.
I know that I can do this with profiles, but are there any other options?
The thing I would like to achieve is to execute a deploy with "mvn deploy" and to use a different plugin configuration, that should only be used if a deploy (or release) is taking place.
A concrete example would be to increase the android version code only if a deploy takes place. Binding the increase of the version code directly to the deploy phase does not work as the increased version code is needed before the process-resources phase to work properly.
I'm afraid maven profiles are your only option.
You could add an enforcer check on deploy phase to fail the build if a profile is not active:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<phase>deploy</phase> <!-- enfoce rules on `mvn deploy` -->
<goals>
<goal>enforce</goal>
</goals>
</execution>
</executions>
<configuration>
<rules>
<requireActiveProfile>
<profiles>prod</profiles> <!-- require `-Pprod` -->
</requireActiveProfile>
</rules>
</configuration>
</plugin>

how to wildcard attach multiple files to an artifact in maven?

In my maven project, the ant plugin generate multiple war files and I want to attach them all in the same artifact. I tried the build-helper-maven-plugin like this
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${project.build.directory}/*.war</file>
<type>war</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
I don't want to specify each war file separately because the ant plugin is dynamic. Is there a way to do that?
Thanks,
Providing wildcards for single artifacts does not seem to be supported by the Build Helper plugin (btw, if it did, it would likely use the includes/excludes configuration used by Resources Plugin).
I've learned that, if you choose to use Maven, it's best to just adjust your build to "the Maven way."
In this case, you should revise your build to not use the ant war plugin, and instead have a multi-module build with a separate module (sub-project) for each war file.
Alternatively, in the past I have accomplished something like you are doing via the Maven Assembly plugin, where the wars are all shipped together in a single tar/gz file. The archive (which contains each of the wars) is then attached to the build.
Note that you should prefer to have your "web apps" module have a artifact type of "pom." The assembly plugin will attach the archives to the final build.
For more information, I've found that Sonatype's online books are a great resource:
http://www.sonatype.com/Support/Books

Resources