Using Maven from Ant - maven-2

Are there ant plugins that wrap maven so that I can make use of its dependency management features to download jars for me and place them in my ant build's lib folder?
My specific problem is that I'm using the Crap4j plugin for Hudson, but it doesn't, as of yet, support Maven. Since it's a small project, maven is overkill, but I don't want to go without mvn dependency:copy-dependcies if I don't have to.
Any suggestions? (other than suck it up)

There is a new set of Ant tasks that use Mercury. Mercury is the refactored code that will be the basis of way that Maven 3 interacts with Maven (and OSGi) repositories that is being implemented by Oleg Gusakov. Mercury is well tested, and you can start using it in Ant projects today. Take a look at some of the How-to documents that Oleg has written:
http://people.apache.org/~ogusakov/sites/mercury-ant/mercury-ant-tasks/howto.html
Here's a simple example of using Mercury in an Ant build.xml file. The following build file creates a classpath that depends on verion 3.0 of the asm artifact:
<javac srcdir="src/main/java"
destdir="target/classes">
<classpath>
<deps>
<dependency name="asm:asm:3.0"/>
</deps>
</classpath>
</javac>
There are a lot of advanced features such as support for verifying PGP signatures or MD5 digests. You can also start to define different repositories that Mercury depends on. This XML allows you to define a reference to a repository such as Nexus in addition to using a local directory as a repository:
<repo id="myCentral"
url="http://localhost:8081/nexus/contengs/groups/public"/>
<repository dir="/my/local/repo"/>
<javac srcdir="src/main/java"
destdir="target/classes">
<classpath>
<deps>
<dependency name="asm:asm:3.0"/>
</deps>
</classpath>
</javac>
If you need to reference a repository that requires authentication Mercury has support for storing a username and password:
<repo id="myCentral"
url="http://localhost:8081/nexus/contengs/groups/public">
<auth name="foo" pass="bar"/>
</repo>
<javac srcdir="src/main/java"
destdir="target/classes">
<classpath>
<deps>
<dependency name="asm:asm:3.0"/>
</deps>
</classpath>
</javac>
Most compelling is the ability to publish an artifact to a repository from an Ant build file. If you work in an organization of any scale, you'll want to start thinking about deploying artifacts to a repository manager like Nexus. With Mercury, you can start deploying artifacts to a repository manager without having to adopt Maven. Here's a build file that defines an authenticated repository and writes an artifact:
<repo id="myCentral"
url="http://localhost:8081/nexus/contengs/groups/public">
<auth name="foo" pass="bar"/>
</repo>
<write repoid="myCentral"
name="t:t:1.0"
file="${basedir}/target/t.jar"/>
Mercury is ready to use, and you can expect a lot of developments from Oleg going forward. If you want to start using it, the best place to look is at Oleg's How-to Page. (Note: This information will soon be integrated into the Definitive Guide)

Whilst the mercury tasks work, I haven't used them. I have had good success with their predecessors, the maven-ant-tasks. They're fairly simple to get going, if you already have a POM handy.
<project name="blah" xmlns:artifact="antlib:org.apache.maven.artifact.ant">
<!-- If you drop the maven-ant-tasks in ~/.ant/lib, you don't need these two bits. -->
<taskdef uri="antlib:org.apache.maven.artifact.ant"
resource="org/apache/maven/artifact/ant/antlib.xml"
classpathref="ant.classpath" />
<path id="ant.classpath">
<fileset dir="${ant.tasks.dir}">
<include name="*.jar" />
</fileset>
</path>
<target name="resolve" description="--> retrieve dependencies with maven">
<!-- Resolve dependencies -->
<artifact:dependencies filesetId="dependency.fileset">
<pom file="pom.xml" />
</artifact:dependencies>
<!-- Copy all dependencies to the correct location. -->
<copy todir="${web.dir}/WEB-INF/lib">
<fileset refid="dependency.fileset" />
<!-- This mapper strips off all leading directory information -->
<mapper type="flatten" />
</copy>
</target>
</project>
I like to keep my ant task jars inside the project, so I've added the taskdef and path. But if you want to put maven-ant-tasks-2.0.9.jar in ~/.ant/lib, then you don't need to declare this stuff. I think.

If you think that Maven is overkill in your project, you could/should try Apache Ivy: It's a very powerful dependency management library similar to the Maven one.
If you're hosting a project on the web, take also a look at Ivy Roundup, it's a repository of Ivy definitions for various libraries.

Just use the Maven Ant Tasks. They can be downloaded at the normal maven download page.

Refer this: Why you should use the Maven Ant Tasks instead of Maven or Ivy
I wouldn't really recommend Ivy for reasons given in the link above.

It is very simple to run Maven goal from Ant
<target name="buildProject" description="Builds the individual project">
<exec dir="${source.dir}\${projectName}" executable="cmd">
<arg value="/c"/>
<arg value="${env.MAVEN_HOME}\bin\mvn.bat"/>
<arg line="clean install" />
</exec>
</target>
Using this you are allow to run any kind of Maven goal from Ant...
Enjoy....

In my case i just want an ejb jar to be at the repository so i could use it on another project with maven as dependency so:
<target name="runMaven" depends="deploy" description="LLama al maven.">
<exec executable="cmd">
<arg value="/c"/>
<arg value="mvn.bat install:install-file -DgroupId=com.advance.fisa.prototipo.camel -DartifactId=batch-process -Dversion=1.0 -Dpackaging=jar -Dfile=${jarDirectory}\batch-process.jar"/>
</exec>
</target>

Download Maven Ant Tasks then use this:
<target name="getDependencies">
<path id="maven-ant-tasks.classpath" path="${basedir}${file.separator}maven${file.separator}lib${file.separator}maven-ant-tasks.jar" />
<typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="antlib:org.apache.maven.artifact.ant" classpathref="maven-ant-tasks.classpath" />
<artifact:dependencies filesetId="dependency.fileset" type="jar">
<pom file="pom.xml" />
</artifact:dependencies>
<!--TODO take care of existing duplicates in the case of changed/upgraded dependencies-->
<copy todir="lib">
<fileset refid="dependency.fileset" />
<mapper type="flatten" from="${dependency.versions}" />
</copy>
</target>

I am working on the same problem right now. I installed all necessary libs in my local Maven repo and from there I put it into our Company Maven Repo.
It is not working quite right yet. Some of the tests fail that work nicely in my Maven test run, but since the outcome of the test is not important for the coverage data, I am quite satisfied.
Here is my Maven snippet. I hope that helps.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<inherited>false</inherited>
<executions>
<execution>
<phase>site</phase>
<configuration>
<tasks>
<property name="compile_classpath" refid="maven.compile.classpath"/>
<property name="runtime_classpath" refid="maven.runtime.classpath"/>
<property name="test_classpath" refid="maven.test.classpath"/>
<property name="plugin_classpath" refid="maven.plugin.classpath"/>
<property name="CRAP4J_HOME" value="${user.home}/Projects/crap4j"/>
<taskdef name="crap4j" classname="org.crap4j.anttask.Crap4jAntTask">
<classpath>
<fileset dir="${CRAP4J_HOME}/lib">
<include name="**/*.jar"/>
</fileset>
</classpath>
</taskdef>
<crap4j projectdir="${project.basedir}/alm-jar-server"
outputDir="${project.basedir}/crap4jReports"
dontTest="false"
debug="true">
<classes>
<pathElement location="${project.basedir}/target/classes"/>
</classes>
<srces>
<pathElement location="${project.basedir}/src/main/java"/>
</srces>
<testClasses>
<pathElement location="${project.basedir}/target/test-classes"/>
</testClasses>
<libClasspath>
<fileset dir="${user.home}/.m2/repository">
<include name="**/*.jar"/>
</fileset>
</libClasspath>
</crap4j>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.crap4j</groupId>
<artifactId>crap4j</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>org.kohsuke</groupId>
<artifactId>args4j</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>com.agitar</groupId>
<artifactId>asmlib</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>com.agitar</groupId>
<artifactId>coverage</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</plugin>

Related

How do I use AWS CodeArtifact with Apache Ivy?

I have set up an AWS CodeArtifact repository, and used Maven to publish a jar to it.
I want to use that jar in an Ant task, and I'm trying to use Apache Ivy to download the dependency.
I've successfully used Ivy to download public libraries from the Maven central repository, but I don't know how to add my AWS CodeArtifact repository to Ivy. The official Ivy tutorials are very hard for me to understand.
The solution was to switch from Ivy to the Maven Resolver Ant Tasks - much better documented and easier to understand.
I can now load AWS CodeArtifact dependencies from my ant task with something like this (after doing the Maven setup instructions from AWS CodeArtifact). This downloads the Maven resolver jar as part of the process:
<property name="maven.install.version" value="1.3.0"/>
<property name="ant.jar.dir" value="${user.home}/.ant/lib"/>
<property name="maven.jar.file" value="${ant.jar.dir}/maven-resolver-ant-tasks.jar"/>
<target name="load-maven-dependencies" depends="init-maven">
<resolver:resolve>
<dependencies>
<dependency coords="group:artifact:version"/>
</dependencies>
<path refid="main.classpath" classpath="compile"/>
</resolver:resolve>
</target>
<target name="init-maven" depends="download-maven">
<path id="maven.lib.path">
<fileset dir="${ant.jar.dir}" includes="${maven.jar.file}"/>
</path>
<taskdef resource="org/apache/maven/resolver/ant/antlib.xml" uri="antlib:org.apache.maven.resolver.ant" classpathref="maven.lib.path"/>
</target>
<target name="download-maven" unless="offline">
<mkdir dir="${ant.jar.dir}" />
<get src="https://search.maven.org/remotecontent?filepath=org/apache/maven/resolver/maven-resolver-ant-tasks/${maven.install.version}/maven-resolver-ant-tasks-${maven.install.version}-uber.jar" dest="${maven.jar.file}" usetimestamp="true"/>
</target>
You can then use main.claspath in your taskdef, like this:
<taskdef name="customTask" classname="com.prosc.CustomAntTask" classpathref="main.classpath" />

apache ant ivy conditionally build a module

I have a usecase where in I had to create a new module in out project. Our main project has multiple modules and each module is a java project. We are using ivy for dependency resolution. Now the probkem is that in the new module , I had to use java 1.7 API (WatchService) which is not there in java 1.6. Now in build.xml I can check for the java version and accordingly build this new module depending on the java -version. The problem comes in ivy.xml of or main web project where I have to mention the jar file of the new module as a dependency to include in the generated war file. If the java version is 1.7 , then in that case problem wont be there as the jar will be build and the its dependency will be resolved. the problem arises when the java version is 1.6. The jar file wont be created and when its time to generate the war file, ivy wont be able to resolve the dependency as the jar file is not there. Maybe the approach that I am trying to apply here is not fine. Please advice me on how to work around this particular use case.
rampal
In ivy you can use configurations to maintain different sets of depenedencies:
<ivy-module version="2.0">
<info organisation="com.myspotontheweb" module="demo"/>
<configurations>
<conf name="compile_jdk7" description="Java JDK7 compile dependencies"/>
<conf name="compile_jdk6" description="Java JDK6 compile dependencies"/>
</configurations>
<dependencies>
<!-- JDK7 dependencies -->
<dependency org="org.myorg" name="module1" rev="latest.integration" conf="compile_jdk7->default"/>
<dependency org="org.myorg" name="module2" rev="latest.integration" conf="compile_jdk7->default"/>
<dependency org="org.myorg" name="module3" rev="latest.integration" conf="compile_jdk7->default"/>
<!-- JDK6 dependencies -->
<dependency org="org.myorg" name="module1" rev="latest.integration" conf="compile_jdk6->default"/>
<dependency org="org.myorg" name="module3" rev="latest.integration" conf="compile_jdk6->default"/>
</dependencies>
</ivy-module>
and in the build file use a condition task to choose which configuration is used at run-time to populate the classpath, using the cachepath task:
<project name="demo" default="compile" xmlns:ivy="antlib:org.apache.ivy.ant">
<condition property="compile.config" value="compile_jdk7">
<equals arg1="${ant.java.version}" arg2="1.7"/>
</condition>
<condition property="compile.config" value="compile_jdk6">
<equals arg1="${ant.java.version}" arg2="1.6"/>
</condition>
<target name="resolve" description="Use ivy to resolve classpaths">
<ivy:cachepath pathid="compile.path" conf="${compile.config}"/>
</target>
<target name="compile" depends="resolve" description="Compile code">
<javac ...... classpathref="compile.path"/>
</target>
</project>

How to take two revisions of the same jar?

I have two versions of the same jar (3.2 and 2.2.1) I need to use both of them but ivy evicts older revision. How to configure ivy to take two versions?
<dependency org="asm" name="asm-all" rev="3.2">
<artifact name="asm-all" type="jar"/>
</dependency>
<dependency org="asm" name="asm-all" rev="2.2.1">
<artifact name="asm-all" type="jar"/>
</dependency>
You need to use ivy configurations. This is a very flexible mechanism to manage arbitrary groups of dependencies.
The example below places each version of the jar onto a separate configuration. This can be used later to create two classpaths, using the ivy cachepath task.
Example
ivy.xml
<ivy-module version="2.0">
<info organisation="com.myspotontheweb" module="demo"/>
<configurations>
<conf name="compile1" description="Required to compile application1"/>
<conf name="compile2" description="Required to compile application2"/>
</configurations>
<dependencies>
<!-- compile1 dependencies -->
<dependency org="asm" name="asm-all" rev="3.2" conf="compile1->master"/>
<!-- compile2 dependencies -->
<dependency org="asm" name="asm-all" rev="2.2.3" conf="compile2->master"/>
</dependencies>
</ivy-module>
Notes:
Version 2.2.1 does not exist in Maven Central
Note the configuration mapping "??? -> master". In Maven the remote master configuration mapping resolves to the main module artifact without dependencies. (See)
build.xml
<project name="demo" default="init" xmlns:ivy="antlib:org.apache.ivy.ant">
<target name="init" description="Use ivy to resolve classpaths">
<ivy:resolve/>
<ivy:report todir='build/ivy' graph='false' xml='false'/>
<ivy:cachepath pathid="compile1.path" conf="compile1"/>
<ivy:cachepath pathid="compile2.path" conf="compile2"/>
</target>
<target name="clean" description="Clean built artifacts">
<delete dir="build"/>
</target>
<target name="clean-all" depends="clean" description="Additionally purge ivy cache">
<ivy:cleancache/>
</target>
</project>
Notes:
Always a good idea to generate an ivy report. It will tell you which dependencies exist on which ivy configuration.
This example shows ivy managing ANT paths. You can also use ivy configurations with the ivy retrieve task to populate a local "lib" directory when assembling something like a webapp WAR file.

Ivy/Maven Resolve: Don't pull transitive "provided" jars

I'm using Ivy for my projects, but we're using Artifactory as our jar repository. I actually use <ivy:makepom> Ant task to create a Maven pom.xml, so I can deploy the jars and wars back to my Maven repository via the Maven deploy:deploy workflow.
I build a big jar called common-all.jar that requires about 30 jars for its compilation. I specify about 10 jars, and Ivy pulls down the dependencies. As part of the compile process, I specify the log4j jar, and some JBoss jars. These jars, of course, will be provided by our environment.
With this Jar, I also a bunch of wars. I specify the common-all.jar as part of my dependency, and the 30 jars that common-all.jar requires are also pulled down. All is well and good.
The problem is when I build the war. I do not want the JBoss jars or the log4j jars included as part of the war. These will be provided by the environment. I've marked them as provided in the pom.xml file. when I build common-all.jar.
Now, the question is how do I specify that I want these when I compile the code for the war, but I don't want to include them in my war itself.
Here's a sample of my ivy.xml file.
How can I specify that the common-all.jar requires certain specific jars for compilation, but when I build it in a war, I don't want all of these jars
<ivy-module version="1.0">
<info
organisation="com.travelclick"
module="TC-AppUtil"
revision="4.1"
status="release"/>
<configurations>
<conf name="default" visibility="public"
description="The single built artifact. Nothing else"/>
<conf name="compile" visibility="public"
description="The master module and transitive dependencies"/>
<conf name="provided" visibility="public"
description="Needed for compile. Will be provided outside or war"/>
<conf name="runtime" visibility="public"
description="Not required for compile, but for runtime"
extends="compile"/>
<conf name="default" visibility="public"
description="The default configuration"
extends="runtime"/>
<conf name="test" visibility="private"
description="Required for testing" extends="runtime"/>
</configurations>
<dependencies>
<!-- Normal Compile Dependencies -->
<dependency org="ximpleware" name="vtd-xml"
rev="2.5" conf="compile->default"/>
<dependency org="com.travelclick" name="common-all"
rev="4.1" conf="compile->compile,runtime"/>
<!-- Testing -->
<dependency org="junit" name="junit"
rev="4.10" conf="test->default"/>
</dependencies>
</ivy-module>
You haven't demonstrated how you declare the common-all dependency, so I'll make up the following example:
<dependency org="mygroup" name="common-all" rev="1.0" conf="compile->default;provided"/>
The magic is the configuration mapping:
The local "compile" configuration is mapped to the common module and its default (compile) scope dependencies, and
The local "provided" configuration is mapped to the common module and its provided scope dependencies.
Inside your build file the configurations are used as follows:
<project name="demo" default="build" xmlns:ivy="antlib:org.apache.ivy.ant">
<target name="resolve">
<ivy:resolve/>
<ivy:cachepath pathid="compile.path" conf="compile"/>
<ivy:cachepath pathid="provided.path" conf="provided"/>
</target>
<target name="compile" depends="resolve">
<javac ...
<classpath>
<path refid="compile.path"/>
<path refid="provide.path"/>
</classpath>
</javac>
</target>
<target name="build" depends="compile">
<ivy:retrieve pattern="build/lib/[artifact].[ext]" conf="runtime"/>
<war ...
<lib dir="build/lib"/>
</war>
</target>
<target name="clean">
<delete dir="build"/>
<ivy:cleancache/>
</target>
</project>

How to execute tasks conditionally using the maven-antrun-plugin?

I need to execute some ant commands depending on an environment variable passed in as a parameter to the maven build command.
At the moment I have 3 tasks blocks and only the tasks block with no condition is being executed.
<tasks name="isProdCheck">
<condition property="isProd">
<equals arg1="${environment}" arg2="PROD" />
</condition>
</tasks>
<tasks if="isProd" depends="isProdCheck">
...
</tasks>
<tasks>
... I am the only block executed
</tasks>
What am I doing wrong, is there a better way to do this?
First, according to Maven 1.x website, the current stable release for maven 1.x is version 1.1, not 1.4. Second, there is no AntRun Plugin version 1.7 and, to my knowledge, this is a Maven 2 plugin. Third, the syntax you are using seems very similar to Using Attributes which, again, is about Maven 2.
So, I may be missing something but, this is very confusing and you should maybe clarify these points in your question.
Anyway, as you explicitly mentioned Maven 1, I'll try to answer. If I remember well, I would write a custom goal and use Jelly's core:if or core:when. To do so, provide something like this in maven.xml:
<project xmlns:j="jelly:core" xmlns:ant="jelly:ant">
<goal name="my-goal">
<j:if test="${environment == 'PROD'}">
<ant:xxx .../>
</j:if>
</goal>
</project>
I'm really not sure of the syntax, all this Maven 1 stuff is just too far away, and I didn't test it (I'm too lazy to install Maven 1). But I guess you will. The scripting reference may help you.
To be honest, I really hope you have a good reason to prefer Maven 1.x over Maven 2.x :)
UPDATE: It appears that the OP is actually using Maven 2 so I'll update my question accordingly. To implement the desired behavior, you could use Ant-contrib's if task as shown below:
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<taskdef resource="net/sf/antcontrib/antcontrib.properties"
classpathref="maven.plugin.classpath" />
<if>
<equals arg1="${foo}" arg2="bar" />
<then>
<echo message="The value of property foo is bar" />
</then>
<else>
<echo message="The value of property foo is not bar" />
</else>
</if>
</tasks>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>ant-contrib</groupId>
<artifactId>ant-contrib</artifactId>
<version>20020829</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
And then call mvn compile -Dfoo=bar (this is just an example).
But, all this is not the "maven way" to do things. Now that I understand a bit better what you are trying to do (but not entirely as you didn't explain your ultimate goal), I think that using build profiles would be more appropriate and, having read your own answer, I think that you are over complicating things (and that you are on the wrong path).
I understand that you are a Maven beginner but I'd suggest to try to use it though instead of falling back on Ant or you won't get the benefits of it. Also, when opening a question, instead of asking for a specific solution, you should rather explain your general problem, you'll get better answers. Here, I can't provide more guidance as I don't know what you are really trying to achieve.
You don't need to use AntContrib after maven-antrun-plugin 1.5 that uses <target> instead of <tasks> according to plugin usage. This tag works in the same way as , but in this one you can add conditions like the example below.
<properties>
<execute.my.target>true</execute.my.target>
</properties>
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>config</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<!-- this target will be executed always -->
<target>
<echo message="Hello there, I'm a simple target" />
</target>
<!--
This target will be executed if and only if
the property is set to true
-->
<target name="my-target" if="execute.my.target">
<echo message="Conditional target..." />
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
The code above always executes the first target, but the second target depends on a property value. You can configure it for a parent and a sub-module project too, defining the plugin on <pluginsManagement> tag and calling some properties at the sub-modules, then calling the plugin.
Update:
Be sure to use the if parameter without ${}, since it can cause troubles mentioned in the comments section.
Resolved this issue by creating multiple named targets with "if" attributes and a condition property in a build.xml file in the project root as follows.
<target name="prod" if="isProd" depends="isProdCheck">
// do something
</target>
Passed properties of the command line switches I required, and called the ant targets in the build.xml file from the tasks section in the Maven POM as follows:
<tasks>
<ant antfile="${basedir}/build.xml">
<property name="environment" value="${environment}"/>
<target name="prod"/>
</ant>
</tasks>
runable example here https://www.surasint.com/run-ant-with-if-from-maven/
Another solution would be: keep the ant-contrib-1.0b3.jar to a path and then define it like this
<property name="runningLocation" location="" />
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${runningLocation}/ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>
then
<target name="doSomething">
<if>
<equals arg1="${someProp}" arg2="YES" />
<then>
<echo message="It is YES" />
</then>
<else>
<echo message="It is not YES" />
</else>
</if>
</target>
I put full code example here which you can download https://www.surasint.com/2017/04/09/run-ant-with-if-from-maven/