2011-05-11 38 views
4

我有一个build.xml(基于ant),它需要一些来自nexus的jar来复制到现有的lib文件夹中。即当它建立它应该从一个版本定义&复制从nexus罐子然后复制在lib &做编译。 就像发生在maven中我们定义了神器&它的版本。如果更改将自动从Maven回购下载。 我如何在基于蚂蚁的构建中做到这一点?从使用ant构建工具下载jar从maven中自动完成

专家请指教。

+1

详细了解可以直接使用Maven存储库的Ivy。 http://ant.apache.org/ivy/ – khmarbaise 2011-05-11 12:30:48

回答

3

尽管确实有具体的方法来组合ant和maven,但最简单的事情(如果您知道nexus URL和构件下载URL的工件参数)只是使用ant Get任务。

<project name="MyProject" default="resolveDependencies" basedir="."> 
    <target name="resolveDependencies"> 
    <mkdir dir="lib" /> 
    <get src="http://search.maven.org/remotecontent?filepath=log4j/log4j/1.2.9/log4j-1.2.9.jar" dest="lib/log4j-1.2.9.jar" usetimestamp="true" /> 
    </target> 
</project> 
4

您可能会感兴趣Ivy。它是依赖管理的Ant的子项目。它非常适合您的情况,因为它可以读取Maven存储库并提供Ant任务以下载发布的工件,从中构建类路径等。它支持您获取最新版本的依赖项的用例,如果您配置它以询问用于模块的“latest.release”版本。

7

我已经将此线程中列出的示例进一步化了一步,并创建了一个macrodef来清理一些重复使用的内容。请参阅下面从nexus下载两个工件(一个快照,一个发布)。

<project> 
<target name="get-all"> 
    <mkdir dir="lib" /> 
    <nexus-get 
     groupId="foo.bar" 
     artifactId="some-artifact" 
     version="1.0.28" 
     repo="releases" 
     extension="jar" 
     dest="lib" 
    /> 

    <nexus-get 
     groupId="foo.bar" 
     artifactId="another-artifact" 
     version="1.0.0-SNAPSHOT" 
     repo="snapshots" 
     extension="jar" 
     dest="lib" 
    /> 
</target> 

<macrodef name="nexus-get"> 
    <attribute name="groupId"/> 
    <attribute name="artifactId"/> 
    <attribute name="version"/> 
    <attribute name="repo"/> 
    <attribute name="extension"/> 
    <attribute name="dest"/> 

    <sequential> 
     <get src="http://my-nexus:9999/nexus/service/local/artifact/maven/[email protected]{repo}&amp;[email protected]{groupId}&amp;[email protected]{artifactId}&amp;[email protected]{version}&amp;[email protected]{extension}" dest="@{dest}/@{artifactId}[email protected]{extension}" usetimestamp="true" /> 
    </sequential> 
</macrodef> 

+1

Nexus重定向307,直到ant 1.9才支持,所以一定要升级。 – homaxto 2013-08-06 11:22:06

+1

@Lance Java当您的sonatype nexus需要授权时,您如何登录。我将用户名和密码存储在用户主目录中的属性文件中。 – TechCrunch 2015-02-22 00:17:00

+0

顺便说一句,如果存储库是公共的,这是一个很好的宏。需要扩展才能添加授权。 – TechCrunch 2015-02-22 00:28:02

0

也许使用Maven Ant Tasks

http://maven.apache.org/ant-tasks/examples/dependencies.html 显示可列出依赖性的蚂蚁,也做这样的事情复制他们

我认为部分使用文件集和版本映射涵盖你的需要

您可以使用是filesetId ,它将为您提供一个文件集引用,可用于将文件复制到特定位置。例如,要用您的依赖项填充WEB-INF/lib,您可以使用以下内容:

<artifact:dependencies filesetId="dependency.fileset" useScope="runtime"> 
    <dependency groupId="junit" artifactId="junit" version="3.8.2" scope="test"/> 
</artifact:dependencies> 
<copy todir="${webapp.output}/WEB-INF/lib"> 
    <fileset refid="dependency.fileset" /> 
    <!-- This mapper strips off all leading directory information --> 
    <mapper type="flatten" /> 
</copy>