Ensode.net
Google
 

Home
Blog
Guides
Tips
Articles
Utilities
Reviews
About Us
Contact us


Facebook profile

XML

Adding "Maven-Like" Dependency Management To NetBeans Projects with Ivy
Bookmark and Share

I am a Maven fan. Sure it has its problems, but its benefits outweigh them, as far as I am concerned. There are three maven features I really like:

I am also a NetBeans fan, as a matter of fact, what made me a NetBeans fan in the first place was its outstanding Maven support. With the help of the Mevenide plugin, NetBeans supports Maven projects very well, the project's pom.xml file "drives" the project, without need to "import" Maven settings into NetBeans, modifying the pom.xml is all that is necessary in order to add dependencies and such. Unfortunately, by using Maven we lose some NetBeans functionality, such as Visual Web, JSF CRUD Generation, and the ability to create JPA entities from existing database schemas.

When using "standard" (not Maven) NetBeans projects consistently, two of the benefits of using Maven are kept: consistent directory structure, and a consistent set of build commands (for those that don't know, NetBeans by default creates ANT build scripts for every project, the NetBeans created build script use consistent build commands and a consistent directory structure) but we lose the third benefit, dependency management.

There is a dependency management tool out there for ANT, it is called Ivy. I googled around and found some information on using Ivy with NetBeans, although the entry is a bit old, I tried the provided steps in NetBeans 6.5 beta and they worked fine. I made some slight modifications to the provided XML files:

The document suggests modifying project.properties so that the javac.classpath property is set as follows:

javac.classpath=${ivy.classpath}

What this does is completely replace the CLASSPATH that NetBeans uses to compile with the one provided by Ivy, what I would like to do is to use the libraries provided by NetBeans, such as the Woodstock component libraries in a Visual Web JSF project, in addition to some additional dependencies specified in ivy, therefore I modified the javac.classpath property as follows:

javac.classpath=${libs.jsf12-support.classpath}\:${libs.woodstock-components.classpath}\:${libs.woodstock-theme-default.classpath}\:${ivy.classpath}

Leaving the entries that were added automatically by NetBeans, and appending the Ivy classpath to it. Doing this allows to simply add additional libraries without losing the ones that NetBeans automatically adds to the project.

At this point I added two dependencies to my project, Sitemesh and iText. I had read that Ivy by default uses the ibiblio maven repository, therefore I thought this would be very simple:

<dependencies>
    <dependency org="opensymphony" name="sitemesh" rev="2.2.1"/>
    <dependency org="itext" name="itext" rev="1.3"/>
</dependencies>
      

I derived the values of the org, name and rev attributes from the corresponding values in the pom.xml for each dependency. Full of anticipation, I tried to build my project and ..., it couldn't find the dependencies.

I examined the build output:

                module not found: [ opensymphony | sitemesh | 2.2.1 ]
                        local: tried /home/heffel/java_libs/ivy-1.4.1/local/opensymphony/sitemesh/2.2.1/ivys/ivy.xml
                        local: tried artifact [ opensymphony | sitemesh | 2.2.1 ]/sitemesh.jar[jar]:
                                /home/heffel/java_libs/ivy-1.4.1/local/opensymphony/sitemesh/2.2.1/jars/sitemesh.jar
                        shared: tried /home/heffel/java_libs/ivy-1.4.1/shared/opensymphony/sitemesh/2.2.1/ivys/ivy.xml
                        shared: tried artifact [ opensymphony | sitemesh | 2.2.1 ]/sitemesh.jar[jar]:
                                /home/heffel/java_libs/ivy-1.4.1/shared/opensymphony/sitemesh/2.2.1/jars/sitemesh.jar
                        public: tried http://ivyrep.jayasoft.org/opensymphony/sitemesh/ivy-2.2.1.xml
                        public: tried artifact [ opensymphony | sitemesh | 2.2.1 ]/sitemesh.jar[jar]:
                                http://www.ibiblio.org/maven/sitemesh/jars/sitemesh-2.2.1.jar
                ::::::::::::::::::::::::::::::::::::::::::::::
                ::          UNRESOLVED DEPENDENCIES         ::
                ::::::::::::::::::::::::::::::::::::::::::::::
                :: [ opensymphony | sitemesh | 2.2.1 ]: not found
                ::::::::::::::::::::::::::::::::::::::::::::::
      

and noticed that by default Ivy is using the Maven 1 ibiblio repository, the telltale line was

http://www.ibiblio.org/maven/sitemesh/jars/sitemesh-2.2.1.jar

which is using a path to the Maven 1 repository in ibiblio. Again I googled around to find out how to make it use the Maven 2 repository, and found this blog entry by Kohsuke Kawaguchi. Turns out I have to explicitly add the ibiblio Maven 2 repository so that Ivy can find it, in order to do that I had to add a new file to the project, ivy-settings.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ivysettings>
    <conf defaultResolver="chained"/>
    <resolvers>
        <chain name="chained">
            <ibiblio name="ibiblio" root="http://mirrors.ibiblio.org/pub/mirrors/maven2/" m2compatible="true"/>
        </chain>
    </resolvers>
</ivysettings>          
        

Once I did this, I had to tell Ivy about this new configuration file by modifying build.xml by adding the following line inside the "-ivy-retrieve" target:

      
        <ivy:configure file="ivysettings.xml" />
      
    

Once I did this I was able to properly build and deploy my project. After building, I could see that the dependencies added by Ivy were automatically added to the NetBeans project.

netbeans_ivy_dependencies

The steps described in this article allowed me to gain "maven-like" transitional dependency management to my NetBeans project without losing NetBeans specific functionality, I was able to build pages visually by dragging and dropping components into a page using the NetBeans Visual Web JSF functionality, as well as generate JPA entities from an existing schema, and in turn generate JPA controllers from the generated entities, all very nice NetBeans features that are not available when using Maven projects.

Resources


Java EE 6 Development With NetBeans 7
Java EE 6 Development With NetBeans 7


Java EE 6 with GlassFish 3 Application Server
Java EE 6 with GlassFish 3 Application Server


JasperReports 3.5 For Java Developers
JasperReports 3.5 For Java Developers