Can I use Maven Ant tasks to download libraries for my project from a Maven repository?

Yes, it is possible, though you have to edit some configuration files to set it up (the IDE's GUI does not support it directly). Try this as an example:

1. Download Maven Ant tasks

http://maven.apache.org/download.html (This FAQ entry was tested with version 2.0.7)

2. Create a new Java application project.

3. Create a Maven pom file pom.xml, preferably in the nbproject subdirectory of your project. That's where other metadata of your project resides as well.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">



Note that you can verify the syntax using Alt-Shift-F9.

4. Add to your build.xml some custom logic:

<project name="JavaApplicationUsingMavenAntTask" default="default" basedir="."
    <description>Builds, tests, and runs the project JavaApplicationUsingMavenAntTask.</description>
    <import file="nbproject/build-impl.xml"/>
    <path id="maven-ant-tasks.classpath" path="nbproject/maven-ant-tasks-2.0.7.jar" />
    <typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="urn:maven-artifact-ant"
           classpathref="maven-ant-tasks.classpath" />  
    <target name="-maven-retrieve">
        <artifact:pom file="${basedir}/nbproject/pom.xml" id="maven.project"/>
        <artifact:dependencies fileSetId="maven-ant-tasks.dependency.fileset">
              <pom refid="maven.project"/>
        <pathconvert property="maven-ant-tasks.dependency.path" dirsep="/" pathsep=":">
                <fileset refid="maven-ant-tasks.dependency.fileset"/>
        <propertyfile file="nbproject/project.properties">
            <entry operation="=" key="maven.dependency.classpath" value="${maven-ant-tasks.dependency.path}"/>
    <target name="-pre-compile" depends="-maven-retrieve"/>
    <target name="-pre-compile-single" depends="-maven-retrieve"/>

Please note that the script snippet assumes the pom file and also maven-ant-tasks jar are located in the nbproject subdirectory. That will atomatically store the jar file in cvs or subversion for you. If you prefer to have the ant tasks outside the versioned sourcebase, update the script accordingly. (see Ivy Faq entry for relevant code.)

In order for your maven dependencies in pom to be recognized by the IDE infrastructure, we need to update the project.properties file from the script. That means that rebuilding the project is required wheneven one changes the pom file. Otherwise the IDE complains about missing classes in the editor.

5. For additional configuration of the Maven ant tasks, see the Apache Maven website

6. EDIT 2009-May-04 I just wanted to add that I also needed one more change to get this working. As in the Ivy Faq entry I edited nbproject/project.properties and changed one definition:


7. EDIT 2013-Nov-27 Take into account that you will requiere additional changes to add librarys only for testing purposes in 'Test Libraries' Netbeans folder.

A working sample j2se project is attached.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo