Download jars from nexus using ant build tool as done automatically in Maven

15,384

Solution 1

I have taken the example listed in this thread one step further and created a macrodef to clean things up a bit for re-use. See below for downloading two artifacts from nexus (one snapshot, one release).

<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/redirect?r=@{repo}&amp;g=@{groupId}&amp;a=@{artifactId}&amp;v=@{version}&amp;e=@{extension}" dest="@{dest}/@{artifactId}.@{extension}" usetimestamp="true" />
    </sequential>
</macrodef>

Solution 2

You would probably be interested in Ivy. It is a sub-project of Ant for dependency management. It is perfect for your situation because it can read Maven repositories and provides Ant tasks for downloading the published artifacts, constructing class paths from them, etc. It supports your use case of getting the most recent version of a dependency if you configure it to ask for the "latest.release" revision of the module.

Solution 3

Although there are surely specific ways to combine ant and maven the simplest thing (if you know the nexus URL and your artifact parameters to construct the download URL) would be just to use the ant Get task.

<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>
Share:
15,384
usercm
Author by

usercm

Updated on June 24, 2022

Comments

  • usercm
    usercm almost 2 years

    I have a build.xml(ant based) which requires some jar from nexus to get copied in existing lib folder. i.e when it builds it should copy the jar from nexus with some version defined & then copy in lib & do compilation. like happen in maven we define the artifact & its version . If changed will automatically download it from maven repo. how can i do this in ant based builds?

    experts pls advice.

  • homaxto
    homaxto almost 11 years
    Nexus redirects with 307 which is not supported until ant 1.9, so be sure to upgrade.
  • TechCrunch
    TechCrunch about 9 years
    @Lance Java How do you login when your sonatype nexus needs authorization. I've the username and password stored in a properties file in user home directory.
  • TechCrunch
    TechCrunch about 9 years
    BTW, this is an excellent macro if the repository is public. Needs extension to add authorizaiton.
  • lance-java
    lance-java about 9 years
    I'm guessing the nexus rest api supports http authentication so you can probably specify a username/password on the ant get task. You could also look at the nexus rest api documentation, perhaps you need to hit this first?