Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 611
Cataclysm Avatar asked Dec 25 '22 09:12

Cataclysm


1 Answers

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>

like image 83
Cataclysm Avatar answered Jan 08 '23 19:01

Cataclysm