使用Ant的Maven

时间:2009-03-31 18:54:57

标签: maven-2 ant

是否有包含maven的ant插件,以便我可以利用其依赖管理功能为我下载jar并将它们放在我的ant build的lib文件夹中?

我的具体问题是我正在使用Hudson的Crap4j插件,但它还没有支持Maven。由于这是一个小项目,maven有点矫枉过正,但如果我不需要,我不想没有mvn dependency:copy-dependcies

有什么建议吗? (除了吮吸它)

9 个答案:

答案 0 :(得分:13)

有一组使用Mercury的新Ant任务。 Mercury是重构代码,它将成为Maven 3与Oleg Gusakov正在实施的Maven(和OSGi)存储库交互的方式的基础。 Mercury经过了充分测试,您可以立即开始在Ant项目中使用它。看一下Oleg编写的一些How-to文档:

http://people.apache.org/~ogusakov/sites/mercury-ant/mercury-ant-tasks/howto.html

以下是在Ant build.xml文件中使用Mercury的简单示例。以下构建文件创建一个依赖于asm工件的verion 3.0的类路径:

<javac srcdir="src/main/java"
       destdir="target/classes">
  <classpath> 
    <deps>
      <dependency name="asm:asm:3.0"/>
    </deps>
  </classpath>
</javac>

有许多高级功能,例如支持验证PGP签名或MD5摘要。您还可以开始定义Mercury所依赖的不同存储库。除了使用本地目录作为存储库之外,此XML还允许您定义对存储库的引用,例如Nexus

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

如果您需要引用需要身份验证的存储库,Mercury支持存储用户名和密码:

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

最引人注目的是能够从Ant构建文件将工件发布到存储库。如果您在任何规模的组织中工作,您将需要开始考虑将工件部署到像Nexus这样的存储库管理器。使用Mercury,您可以开始将工件部署到存储库管理器,而无需采用Maven。这是一个构建文件,它定义了一个经过身份验证的存储库并编写了一个工件:

<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已经可以使用了,你可以期待Oleg的很多发展。如果您想开始使用它,最好看的地方是Oleg's How-to Page。 (注意:此信息很快就会整合到Definitive Guide

答案 1 :(得分:11)

虽然汞任务有效,但我还没有使用它们。我和他们的前辈maven-ant-tasks取得了很大的成功。如果你已经拥有POM,它们就很容易上手。

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

我喜欢将我的ant任务jar放在项目中,所以我添加了taskdef和path。但是如果你想将maven-ant-tasks-2.0.9.jar放在~/.ant/lib中,那么你不需要声明这些东西。我想。

答案 2 :(得分:10)

如果您认为Maven在您的项目中过度,您可以/应该尝试Apache Ivy:它是一个非常强大的依赖管理库,类似于Maven。

如果您在网络上托管项目,请查看Ivy Roundup,它是各种库的常春藤定义的存储库。

答案 3 :(得分:5)

只需使用Maven Ant Tasks即可。可以在normal maven download page下载它们。

答案 4 :(得分:4)

请参阅:Why you should use the Maven Ant Tasks instead of Maven or Ivy

我不会真的推荐Ivy,原因在于上面的链接。

答案 5 :(得分:2)

从Ant

运行Maven目标非常简单
<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>

使用此功能,您可以从Ant运行任何类型的Maven目标...

享受....

答案 6 :(得分:2)

在我的情况下,我只是想要一个ejb jar在存储库中,所以我可以在另一个项目中使用maven作为依赖,所以:

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

答案 7 :(得分:1)

下载Maven Ant Tasks,然后使用:

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

答案 8 :(得分:0)

我正在研究同样的问题。我在我当地的Maven回购中安装了所有必要的库,然后我将它放入我们的公司Maven回购中。 它还没有正常工作。有些测试失败,在我的Maven测试运行中很好地工作,但由于测试结果对于覆盖数据并不重要,我非常满意。

这是我的Maven代码段。我希望有所帮助。

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