0

I am struggling with maven-ant build with eclipse.

I did work like below steps.

  • [GUI] new java project
  • add build.xml in project top folder
  • run ant file and SUCCEED!
  • trying to code, but somehow auto completion does not work.(guessing eclipse can not read maven-ant dependency.path)

So I tried.

  • add ~/.m2/repository in build path as a External class folder - does not work - It looks weird to me to include whole this folder. My current project, I need little libraries, but it has whole libraries that I uses in other projects.
  • add builders with build.xml like Want an eclipse java project to run ant build files automatically - does not work neither.

How can I add this maven-ant libraries properly? Thanks for sharing your experiences and answers XD

=========== Extra Information ====================

This is my build.xml.

<?xml version="1.0" encoding="UTF-8"?>
<project name="HibernateEx2" default="db" basedir="."
    xmlns:artifact="antlib:org.apache.maven.artifact.ant">

    <property name="source.root" value="src"/>
    <property name="class.root" value="classes"/>
    <property name="data.dir" value="data"/>

    <artifact:dependencies pathId="dependency.classpath">
        <dependency groupId="hsqldb" artifactId="hsqldb" version="1.8.0.10"/>

        <dependency groupId="org.hibernate" artifactId="hibernate-core" version="4.3.10.Final">
            <exclusion groupId="javax.transaction" artifactId="jta"/>
        </dependency>

        <!-- 3.2.4.GA - After hibernate4 need upgrade hibernate-tools -->
        <dependency groupId="org.hibernate" artifactId="hibernate-tools" version="4.3.1.CR1"/>
        <dependency groupId="org.apache.geronimo.specs" artifactId="geronimo-jta_1.1_spec" version="1.1.1"/>

        <!-- java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory -->
        <dependency groupId="commons-logging" artifactId="commons-logging" version="1.2"/>
        <dependency groupId="log4j" artifactId="log4j" version="1.2.17"/>
        <!-- java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder -->
        <dependency groupId="org.slf4j" artifactId="slf4j-log4j12" version="1.7.12"/>   

    </artifact:dependencies>

    <path id="project.class.path">
        <pathelement location="${class.root}"/>
        <path refid="dependency.classpath" />
    </path>

    <!-- Explaining how to use hibernate -->
    <taskdef name="hibernatetool" 
        classname="org.hibernate.tool.ant.HibernateToolTask"
        classpathref="project.class.path"/>

    <target name="db" description="Run HSQLDB database management UI against the database file -- use when application is not running">
        <java classname="org.hsqldb.util.DatabaseManager" fork="yes">
            <classpath refid="project.class.path"/>
            <arg value="-driver"/>
            <arg value="org.hsqldb.jdbcDriver"/>
            <arg value="-url"/>
            <arg value="jdbc:hsqldb:${data.dir}/music/"/>
            <arg value="-user"/>
            <arg value="sa"/>
        </java>
    </target>

    <target name="print-classpath" description="Show the dependency class path">
        <property name="class.path" refid="dependency.classpath"/>
        <echo>${class.path}</echo>
    </target>

    <!-- Generate java code -->
    <target name="codegen" description="Generate Java source from the OR mapping files">
        <hibernatetool destdir="${source.root}">
            <configuration configurationfile="${source.root}/hibernate.cfg.xml"/>
            <hbm2java/>
        </hibernatetool>
    </target>

    <!-- Creating Sub drectories -->
    <target name="prepare" description="Set up build structures">
        <mkdir dir="${class.root}"/>
        <copy todir="${class.root}">
            <fileset dir="${source.root}">
                <include name="**/*.properties"/>
                <include name="**/*.xml"/>
            </fileset>
        </copy>
    </target>

    <!-- Creating Schema for mapping files -->
    <target name="schema" depends="prepare" description="Generate DB schema from the OR mappinf files">
        <hibernatetool destdir="${source.root}">
            <configuration configurationfile="${source.root}/hibernate.cfg.xml"/>
            <hbm2ddl drop="yes"/>
        </hibernatetool>
    </target>


    <!-- Compile Java -->
    <!-- added includeantruntime="false" to javac, since terminal compile warning -->
    <target name="compile" depends="prepare">
        <javac srcdir="${source.root}" destdir="${class.root}" 
            debug="on" optimize="off" deprecation="on" includeantruntime="false">
            <classpath refid="project.class.path"/>
        </javac>
    </target>

    <target name="ctest" depends="compile">
        <java classname="org.owls.ht.CreateTest" fork="true">
            <classpath refid="project.class.path"/>
        </java>
    </target>
</project>

and This is what my project looks like.

src
-- source codes (includes hibernate.cfg.xml)
classes
-- compiled classes
data
-- logs and queries
build.xml

FYI, I am doing this with a book named [[Harness Hibernate]] written by James Elliot from O'reilly.

Thanks again b

Community
  • 1
  • 1
Juneyoung Oh
  • 7,318
  • 16
  • 73
  • 121

1 Answers1

1

For what you are trying to do, you need the filesetId and versionsId="dependency.versions" in your declaration of:

 <artifact:dependencies filesetId="dependency.fileset" versionsId="dependency.versions"

Then add a copy task like so:

 <copy todir="${lib.dir}">
   <fileset refid="dependency.fileset" />
   <mapper classpathref="maven-ant-tasks.classpath"
      classname="org.apache.maven.artifact.ant.VersionMapper"
      from="${dependency.versions}" to="flatten" />
 </copy>

The to="flatten" will flaten your dependencies into a single folder, then you can include that folder on the classpath of eclipse project or wherever you need it.

niken
  • 2,499
  • 3
  • 33
  • 56