Upload file via Ant FTP task in Maven - ant

I'm trying to upload a file using an Ant task. If I use Ant directly the file is uploaded, but if I call the ant task via Maven (using the maven-antrun-plugin) I get the following error:
An Ant BuildException has occured: The following error occurred while executing this line:
/home/me/proj/build.xml:15: Problem: failed to create task or type ftp
Cause: the class org.apache.tools.ant.taskdefs.optional.net.FTP was not found.
This looks like one of Ant's optional components.
Action: Check that the appropriate optional JAR exists in
-ANT_HOME/lib
ant-commonsnet.jar is clearly available to Ant:
$ ls $ANT_HOME/lib | grep ant-commons-net
ant-commons-net.jar
Is the Ant classpath defined separately for maven-antrun-plugin, or am I missing something?

ant-commons-net.jar is clearly available to Ant
Yes, but Maven and the maven-antrun-plugin is not using your local Ant install.
Is the Ant classpath defined separately for maven-antrun-plugin, or am I missing something?
The way to use Ant Tasks not included in Ant's default jar is documented in Using tasks not included in Ant's default jar (which should definitely help):
To use Ant tasks not included in the
Ant jar, like Ant optional or custom
tasks you need to add the dependencies
needed for the task to run to the
plugin classpath and use the
maven.plugin.classpath reference if
needed.
<project>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-test-app</artifactId>
<groupId>my-test-group</groupId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>ftp</id>
<phase>deploy</phase>
<configuration>
<target>
<ftp action="send" server="myhost" remotedir="/home/test" userid="x" password="y" depends="yes" verbose="yes">
<fileset dir="${project.build.directory}">
<include name="*.jar" />
</fileset>
</ftp>
<taskdef name="myTask" classname="com.acme.MyTask" classpathref="maven.plugin.classpath"/>
<myTask a="b"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>ant</groupId>
<artifactId>ant-commons-net</artifactId>
<version>1.6.5</version>
</dependency>
<dependency>
<groupId>ant</groupId>
<artifactId>ant-nodeps</artifactId>
<version>1.6.5</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

As Pascal has mentioned, the maven-antrun-plugin is not using the ant specified by your $ANT_HOME environment variable, and the configuration that he's mentioned is probably the best way to do it consistently from a pure maven perspective.
However, the jar can be stored in $USER_HOME/.ant/lib instead of $ANT_HOME/lib, these jars should be available on the classpath for any instance of ant that is run by that user.
Note that your ant script cannot assume that the jars are present, and that the jars are only placed on the classpath at startup, so if the script defines a setup target to download the jars into $USER_HOME/.ant/lib, then this target would have to be run in a "separate-ant-session", before and is invoked again to execute the task that depends on the jar.
The only potential benefit that you may derive from this approach is that the Ant script may be runnable from maven and Ant.

There is a classpath property which can be set in <tasks> section of maven-antrun-plugin.
For instance,
<property name="classpath" refid="maven.compile.classpath"/>

Related

Untar / Unzip *.tar.xz with ANT

I want to extract a tarball-file with *.tar.xz with ant.
But I can only find, bunzip2, gunzip, unzip and untar as goals in ant and none of it seems to work.
So how can I expand a tarball-file with *.tar.xz with ant?
The XZ format is not supported by the default Ant distribution, you'll need the Apache Compress Antlib.
Download the full Antlib with all the dependencies from here (add the three jars ant-compress,common-compress,xz in the lib directory of your ant), and use this task:
<target name="unxz">
<cmp:unxz src="foo.tar.xz" xmlns:cmp="antlib:org.apache.ant.compress"/>
<untar src="foo.tar" dest="."/>
<delete file="foo.tar"/>
</target>
You have to use this two-steps process because even with the additional ant library, the "xz" value is still not supported by the compression attribute of the untar task, the task you'll normally use to extract compressed tars.
In case somebody else like myself wants to do the same with maven managing ant.
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<dependencies>
<!-- Optional: May want to use more up to date commons compress, add this dependency
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.10</version>
</dependency> -->
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-compress</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>unpack</id>
<phase>generate-sources</phase>
<goals><goal>run</goal></goals>
<configuration>
<target name="unpack">
<taskdef resource="org/apache/ant/compress/antlib.xml" classpathref="maven.plugin.classpath"/>
<unxz src="${xz.download.file}" dest="${tar.unpack.directory}" />
<untar src="${tar.unpack.file}" dest="${final.unpack.directory}"/>
</target>
</configuration>
</execution>
</executions>
</plugin>

Grails: add a build task for compilation

I'm using grails 2.4.4.
Some of my classes are annotated and a APT (annotation processing tool) has to process these annotations during compilation to generate some sources.
I was able to get everything done with the workaround of creating a maven pom.xml by running grails generate-pom and from there add specific plugins and configure them.
Is there a possibility to use the built-in grails compiler config BuildConfig.groovy to reach the same goal without the detour via maven pom.xml?
To be more specific, I'm creating a workflow with AWS SWF. And SWF uses annotations that should generate some client classes. Therefor in the pom.xml I added this. It works perfectly when I run mvn compile:
<build>
...
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>src/generated</outputDirectory>
<processor>com.amazonaws.eclipse.simpleworkflow.asynchrony.annotationprocessor.AsynchronyDeciderAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-flow-build-tools</artifactId>
<version>1.9.34</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.21</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
</dependencies>
</plugin>
...
</build>
I found out my self how to to it within grails.
Basically I created a script that you can call with the grails command. This script is nothing else but a gant script with which you can interfere with ant. Plus I had to add libraries to the libs folder in grails in order to have them in the classpath.
These are the steps:
Create a new grails script
grails create-script generateSources This will create a script called GenerateSources.groovy into your grails scripts directory
Edit the GenerateSources.groovy file
includeTargets << grailsScript("_GrailsCompile")
target(generateSources: "Generates sources for SWF workflow") {
ant.delete(dir:"src/generated/java")
ant.delete(dir:"target/generated-classes")
depends(compile)
ant.mkdir(dir:"src/generated/java")
ant.mkdir(dir:"target/generated-classes")
ant.javac(destdir:"target/generated-classes", classpathref:"grails.compile.classpath", source:"1.7", target:"1.7"){
compilerarg(value:"-proc:only")
compilerarg(value:"-s")
compilerarg(value:"src/generated/java")
src(path:"src/java")
}
}
setDefaultTarget(generateSources)
This includes references and tasks from the GrailsCompile script, deletes some folders (in case you re-run this script), calls the compile task from the native grails gant, creates the necessary target folders
Copy the necessary jars to the grails lib folder. In my case I had to copy the aws-java-sdk-flow-build-tools-1.9.34.jar which includes the Annotation processor and another dependence freemarker-2.3.18.jar. Javac will automatically call the correct APT in order to process the annotations in the the folder src/java
Run the script
grails generateSources which will run the compile task and your new task
Open:
I'd like to hook up this task automatically when grails compile is called. I'm still working on that thoug.

Turn Off Logging Ant Copy Task in AntRun

I'm copying some files in an Ant Task within Maven and I cannot figure out how to tell ant not to log the fact that it is copying some files.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>generateContext</id>
<phase>compile</phase>
<configuration>
<target>
<copy file="src/main/context/context.xml"
toDir="target/context/"
overwrite="true" verbose="false">
<filterset>
<filter token="deploy.path" value="${deploy.path}" />
<filter token="build.env.deployment.war.name"
value="${build.env.deployment.war.name}" />
</filterset>
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
Every time I execute the task I see the following in the console:
[INFO] --- maven-antrun-plugin:1.7:run (generateContext) # jive-web ---
[INFO] Executing tasks
main:
[copy] Copying 1 file to /Users/jmajors/turbinetrunk/jive/jive-web/target/context
Based upon the ant copy task documentation I wouldn't have thought that I would even need to worry about it, because it appears that logging is turned off by default, but that doesn't appear to be impacting whether or not something is logged. Is there another setting that I should be paying attention to?
Thanks,
Jeremy
When running Ant standalone, you would add the -quiet command line argument.
Here within Maven, it is the Maven AntRun plugin which is controlling the verbosity of Ant. As far as I can see, the verbosity of Ant is equal to the setup of the log of the Maven AntRun plugin.
I don't know much of Maven, if you can figured out how to set the level of log of the Maven AntRun plugin to "WARN", it will become quieter.
You could use the -q command line argument of Maven, but everything in Maven will become quieter.
According the ant manual-->running apache ant, you can add the -quiet command line flag to ant. Update the run configuration to use this flag. You could also change out the default logger for a log4j ant logger and suppress unwanted log entries in a log4j configuration: ant manual-->loggers & listeners.

Maven: mavent--ant-run does not support attribute

Currently I'm working on a maven build script and my next issue is to copy source files into a target folder. I found this thread and it works fine unless I don't use the 'flattern' attribute. I know that the computer makes all things right, but I wonder why the build will fail.
Here my code using the maven antrun plugin:
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>process-resources</phase>
<configuration>
<target>
<copy todir="${project.basedir}/target" flattern="true" overwrite="true">
<fileset dir="${project.basedir}/src/main"/>
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
The error message I get is
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.7:run (default) on project setup_core: An Ant BuildException has occured: copy doesn't support the "flattern" attribute
[ERROR] around Ant part ...<copy todir="C:\Projekte\CQ5_Migration\setup\core_upload/target" overwrite="true" flattern="true">... # 4:101 in C:\Projekte\CQ5_Migration\setup\core_upload\target\antrun\build-main.xml: The <copy> type doesn't support the "flattern" attribute.
[ERROR] -> [Help 1]
Have I overseen somthing and if so what is/are the missing fact(s)?
Thanks again for your help :-)
It should be "flatten" not "flattern".
Remove the 'r'.

Create pom.xml file

Could anayone give me some sugestions on how to create a pom.xml file for a multimodules project, that is build with ant? I need to create this pom.xml file in order to analyze the project with Sonar.
I suggest to follow the instructions from the Sonar documentation. See Analyzing Java Projects:
Project with multiple sources directories
If your non-maven project contains
more than one sources directory, you
can specify which sources directories
to analyse by adding a new section
about the Build Helper Maven Plugin
into your pom.xml file :
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>[YOUR.ORGANIZATION]</groupId>
<artifactId>[YOUR.PROJECT]</artifactId>
<name>[YOUR PROJECT NAME]</name>
<version>[YOUR PROJECT VERSION]</version>
<build>
<sourceDirectory>[YOUR SOURCE DIRECTORY]</sourceDirectory>
<outputDirectory>[YOUR CLASSES/BIN DIRECTORY</outputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<excludes>
<exclude>**/*.*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>[YOUR SOURCE DIRECTORY 2]</source>
<source>[YOUR SOURCE DIRECTORY 3]</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<sonar.dynamicAnalysis>false</sonar.dynamicAnalysis>
<sonar.phase>generate-sources</sonar.phase>
</properties>
</project>
Replace the parameters :
...
And execute the maven2 plugin as explained in the installation guide :
mvn sonar:sonar
There is now a Sonar Ant Task that you can use, or there is also the Sonar Runner
What you put in the pom.xml is going to depend what dependencies you need to use and what plugins you need to run. Check out the Intro to POM to see what it is made up of.
I think you can try to use the builder-helper-maven-plugin, currently, latest version is 1.5.
as documented http://docs.codehaus.org/display/SONAR/Analyzing+Java+Projects. However, just change the plugin version to 1.5 and use mvn sonar3:sonar. Most importantly, dont forget <sonar.phase>generate-sources</sonar.phase>, without this, it doesn't work.
as for the output directory, if using eclipse, you can specify the output directory for each module, and make them point to the same folder. Use this folder as the outputdirectory for pom.xml. remember to disable scrub, if using eclipse.

Resources