create directory when needed in maven - maven-2

I am using maven-exec-plugin to generate java sources of Thrift. It invokes the external Thrift compiler and using -o to specify the output directory, "target/generated-sources/thrift".
The problem is neither maven-exec-plugin nor Thrift compiler automatically create the output directory, I have to manually create it.
Is there a decent/portable way use create missing directories when needed? I don't want to define a mkdir command in the pom.xml, since my project need to be system independent.

Instead of the exec plugin, use the antrun plugin to first create the directory and then invoke the thrift compiler.
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<tasks>
<mkdir dir="target/generated-sources/thrift"/>
<exec executable="${thrift.executable}">
<arg value="--gen"/>
<arg value="java:beans"/>
<arg value="-o"/>
<arg value="target/generated-sources/thrift"/>
<arg value="src/main/resources/MyThriftMessages.thrift"/>
</exec>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
You may also want to take a look at the maven-thrift-plugin.

You can define an ant task to do the job. Put the plugin declaration into your project's pom.xml. This will keep your project system-independent:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>createThriftDir</id>
<phase>process-resources</phase>
<configuration>
<tasks>
<delete dir="${thrift.dir}"/>
<mkdir dir="${thrift.dir}"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

If you would like to prepare such folder structure somewhere in your project and then copy to place you want, use maven-resource plugin to do that:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-folder</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
<resources>
<resource>
<filtering>false</filtering>
<directory>${project.basedir}/src/main/resources/folders</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

Related

Pom.xml - tar task

Is it possible to add a task to the pom.xml file that will create a tar.gz / .zip file.
for eample:
<tar type="tar.gz" source="resources/sql" tofile="target/sql.tar.gz"/>
Thanks
Use the maven-assembly-plugin
Create a src/main/assembly/bin.xml as detailed at http://maven.apache.org/plugin-developers/cookbook/generate-assembly.html and http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html#bin
Put your resources sql files in the includes and give the format of output as tar.gz
Next, in your pom.xml put the reference to this plugin
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/bin.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
http://maven.apache.org/plugins/maven-assembly-plugin/usage.html
Last, call this using
mvn package

Exporting Maven properties from Ant code

I've embedded the following code within my POM:
<plugin name="test">
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>validate</phase>
<configuration>
<tasks>
<pathconvert targetos="unix" property="project.build.directory.portable">
<path location="${project.build.directory}"/>
</pathconvert>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
I then reference ${project.build.directory.portable} from the run project action but it comes back as null. Executing <echo> within the Ant block shows the correct value. What am I doing wrong?
For completeness, the mentioned feature was implemented in the maven-antrun-plugin in October 2010.
The configuration parameter you are looking for is exportAntProperties.
Example of use:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7-SNAPSHOT</version>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<exec outputproperty="svnversion"
executable="svnversion">
<arg value=".." />
</exec>
</target>
<exportAntProperties>true</exportAntProperties>
</configuration>
</execution>
</executions>
</plugin>
As a side note, at the time of this post (2011-10-20), the official plugin documentation didn't have this option documented. To get the help for 'versionXYZ' of the plugin:
mvn help:describe -Dplugin=org.apache.maven.plugins:maven-antrun-plugin:versionXYZ -Ddetail
The version 1.7 of the maven-antrun-plugin worked for me to pass a property from ant to maven (and from mvn to ant). Some sample code that calculates an md5 checksum of a file and later stores it into a property that is accessed by mvn at a later time:
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>ant-md5</id>
<phase>initialize</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<property name="compile_classpath" refid="maven.compile.classpath"/>
<property name="outputDir" value="${project.build.outputDirectory}"/>
<property name="sourceDir" value="${project.build.sourceDirectory}"/>
<checksum file="${sourceDir}/com/blah/db/blah.java" property="blah.md5db"/>
</target>
<exportAntProperties>true</exportAntProperties>
</configuration>
</execution>
</executions>
The property is accessible in later with ${blah.md5db} in a java file.
From the plugin documentation here:
Try to add the maven prefix, so you have
<path location="${maven.project.build.directory}"/> instead
If that doesn't work, you may need to explictly redefine the property yourself:
<property name="maven.project.build.dir" value="${project.build.directory}"/>
<path location="${maven.project.build.directory}"/>
I don't think you can set a property from Ant that will be visible from Maven. You should write a Mojo.

Execute Ant task with Maven

I'm trying to execute with Maven some test written using Ant tasks. I generated the files required to import the task into Maven, but I can't execute them.
My POM is defined this way:
<build>
<plugins>
<plugin>
<artifactId>maven-ant-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<tasks>
<echo message="Hello, maven"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I try to execute that message, but I get an error with run:
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] 'run' was specified in an execution, but not found in the plugin
But, if I run: "mvn antrun:run", I know that this can not run the task.
An if I've different targets, how do I call them from Maven? I've the pom.xml, and build.xml with the ant tasks.
Thanks.
Gonzalo
To run Ant tasks from within Maven 2, you need to use the Maven AntRun Plugin:
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<tasks>
<echo message="Hello, maven"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
The Maven Ant Plugin is something else, it is used to generate build files for Ant from the POM.
Try this one..This will be on the validate phase.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo message="Hello world" />
<echo message="${env.M2_HOME}" ></echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

Maven write to stdout

Anyone familiar with a way how can I can i write something to the stdout from maven.
For instance i would like to write a line everytime i start a new module.
I would use the Maven AntRun plugin to echo the message and bind it on the validate phase (the first phase) of the default lifecyle in the parent pom:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>entering-module</id>
<phase>validate</phase>
<configuration>
<tasks>
<echo>Entering module: ${pom.artifactId}</echo>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
This is just an example of course, the message I'm writing here doesn't really add value to what Maven is already providing/doing.

Maven - extract /test/resources/my.zip in /target during the Test Phase

I've some test resources (that are specific for a particular task) zipped in /test/resources/my.zip.
I want to extract the zip content to /target during the maven Test Phase.
Do you know what should I specify in the pom.xml to achieve this?
One solution is to use the maven-antrun-plugin to run the unzip Ant task. The following configuration in the build section of your POM should be pretty much what you need (but I haven't tested it):
<build>
<plugins>
<!-- ... -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>process-test-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<unzip src="test/resources/my.zip" dest="target/" overwrite="true"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<!-- ... -->
</plugins>
</build>