簡體   English   中英

在Eclipse Java項目中包含Maven-ant構建庫

[英]include maven-ant build libs in eclipse java project

我正在努力與食蝕的Maven-ant構建。

我確實按照以下步驟進行工作。

  • [GUI]新的Java項目
  • 在項目頂部文件夾中添加build.xml
  • 運行ant文件並成功!
  • 試圖代碼,但不知何故自動完成不工作。(猜測日食無法讀取Maven的螞蟻dependency.path

所以我嘗試了。

  • 在構建路徑中將~/.m2/repository添加為External class folder -不起作用-整個文件夾都包含在我看來很奇怪。 在我當前的項目中,我需要很少的庫,但是它具有我在其他項目中使用的整個庫。
  • 使用build.xml添加構建器,例如希望eclipse Java項目自動運行ant構建文件 -均無效。

如何正確添加該Maven-ant庫? 感謝您分享您的經驗和答案XD

===========額外信息====================

這是我的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>

這就是我的項目的樣子。

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

僅供參考,我正在使用O'reilly的James Elliot寫的一本名為[[Harness Hibernate]]的書來做這件事。

再次感謝b

對於您要執行的操作,在以下聲明中需要filesetId和versionsId =“ dependency.versions”:

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

然后添加一個復制任務,如下所示:

 <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>

to =“ flatten”將把依賴關系放到一個文件夾中,然后您可以將該文件夾包括在eclipse項目的類路徑中或任何需要的位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM