FaqMavenAntTasks

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" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.project</groupId>
  <artifactId>project-model</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
    <dependency>
      <groupId>commons-http</groupId>
      <artifactId>commons-http</artifactId>
      <version>1.1</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.4</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
</project>


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="."
         xmlns:artifact="urn:maven-artifact-ant">
    <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"/>
        </artifact:dependencies>
        <pathconvert property="maven-ant-tasks.dependency.path" dirsep="/" pathsep=":">
            <path>
                <fileset refid="maven-ant-tasks.dependency.fileset"/>
            </path>
        </pathconvert>        
        <propertyfile file="nbproject/project.properties">
            <entry operation="=" key="maven.dependency.classpath" value="${maven-ant-tasks.dependency.path}"/>
        </propertyfile>
    </target>
    <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:

javac.classpath=${maven.dependency.classpath}

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