Cataclysm
Cataclysm

Reputation: 8538

How to compile jars from pom.xml with ant?

I used Maven for project configure and Ant for creating jars for some of my utility classes. When I create jars for my classes from build.xml , I got compile errors because some of third party jars were configure with maven and these jars were not exist in my project. If so , how to fix it ? Am I need to compile pom.xml from build.xml ? Or can I create jar file from Maven also ? Any suggestions ?

Upvotes: 4

Views: 8876

Answers (4)

Cataclysm
Cataclysm

Reputation: 8538

Now I have fully working with Maven ant Task . Now I can get all dependencies jar from ant build.xml. I can get them in two ways as below....

1. Copy all jar files to specific location and add them in classpath

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project name="Ant-Test" default="main" basedir="." xmlns:artifact="antlib:org.apache.maven.artifact.ant">
<path id="maven-ant-tasks.classpath" path="libs/maven-ant-tasks-2.1.3.jar" />
<typedef resource="org/apache/maven/artifact/ant/antlib.xml"
           uri="antlib:org.apache.maven.artifact.ant"
           classpathref="maven-ant-tasks.classpath" />

<artifact:pom id="mypom" file="pom.xml" />

<artifact:dependencies filesetId="mydeps" pomRefId="mypom" />

<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="libs">
        <fileset refid="dependency.fileset" />
        <!-- This mapper strips off all leading directory information -->
        <mapper type="flatten" />
    </copy>
</target>

<property file="build.properties"/>

<path id="build.classpath">
    <fileset dir = ".">
        <include name="**/*.jar" />
    </fileset>
</path>

<target name="clean">
    <delete dir="${build.dir}" />
    <delete dir="${docs.dir}" />
    <delete dir="${dist.dir}" />
</target>

<target name="makedir">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${docs.dir}" />
    <mkdir dir="${dist.dir}" />
</target>

<target name="compile" depends="clean, makedir">
    <javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath">
        <compilerarg value="-Xlint:unchecked"/>
    </javac>
</target>

<target name="main" depends="resolve , compile">
    <description>Main target</description>
</target>

2.Load pom.xml and refrence it as fileset

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project name="Ant-Test" default="main" basedir="." xmlns:artifact="antlib:org.apache.maven.artifact.ant">

<path id="maven-ant-tasks.classpath" path="libs/maven-ant-tasks-2.1.3.jar" />
<typedef resource="org/apache/maven/artifact/ant/antlib.xml"
           uri="antlib:org.apache.maven.artifact.ant"
           classpathref="maven-ant-tasks.classpath" />
<property file="build.properties"/>

<!-- Load pom.xml for dependencies -->
<artifact:pom id="pomfile" file="pom.xml" />
<artifact:dependencies filesetId="mvn-dependencies" pomRefId="pomfile" />

<path id="build.classpath">
    <fileset refid="mvn-dependencies" />
</path>

<target name="clean">
    <delete dir="${build.dir}" />
    <delete dir="${docs.dir}" />
    <delete dir="${dist.dir}" />
</target>

<target name="makedir">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${docs.dir}" />
    <mkdir dir="${dist.dir}" />
</target>

<target name="compile" depends="clean, makedir">
    <javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath">
        <compilerarg value="-Xlint:unchecked"/>
    </javac>
</target>


<target name="main" depends="compile">
    <description>Main target</description>
    <echo> Finish Building for project - ${projectName}</echo>
</target>

Upvotes: 6

Pavel S.
Pavel S.

Reputation: 1224

Consider using Apache Ivy. It is a dependency management designed to be used with Ant, and it is fully compatible with Maven repositories.

You will probably need to convert your pom.xml into ivy.xml (Ivy project descriptor) using convertpom Ant task, and then use Ivy Ant tasks (ivy:resolve, ivy:retrieve) to get all your third party jars from a maven repository.

Upvotes: 1

ppuskar
ppuskar

Reputation: 771

This is what i understood. You want to create a jar(which includes some of your utility classes)

When you take maven as a tool you don't have jars present in your project lib, they are present in your local repository (~/.m2) hence ant is not able find your third party jar.

doing it with maven would be one of the approach. find your solution here here

CHEERS !

Upvotes: 1

Dmitry Ginzburg
Dmitry Ginzburg

Reputation: 7461

You may use Maven Ant Tasks. They're designed just for that purpose.

Upvotes: 2

Related Questions