1/10/12

Maven distinct dependencies ?

I wonder if it is possible, to stop Maven downloading old libraries and use only distinct versions (prefering the local repo and the versions where i downloaded Javadoc and sources manually).

For example using JUnit dependency versions 3.8.2, 4.8.1 and the newest 4.x, but never <3.8.1, 4.5, 4.6-Snapshot etc.

Background: I have my actual projects and local repo on first drive (SSD) with limited space.
If i try an maven archetype to start a new project (often for learning a new theme), maven likes downloading i.e. junit 4.5
but i don't like to edit the pom manually looking over my local versions, thinking about compatibilties
and i don't like to purge and download the prefered dependencies again and again.

Are there any tools, plugins, settings or best practices to get this going ?

What i tried so far...
  • going Offline doesn't help cause my preferred IDE offer download manually only for doc+src

        => "Could not resolve dependencies... "

        "The repository system is offline but the artifact ... is not available in the local repository."
        Don't try this at work / production !
        My dependencies were not loaded after turning Online again :-( restart of netbeans doesn't fix until i reboot.

  • One practice is (Netbeans) in context menu on project explorer "Remove dependecy" and "Add as Dependecy.." from context menu in windows with local repository tree:

    Use menu Window | Other | Maven repository browser to get this :

    Then in window "Project explorer" i remove the disliked dependency not necessary

    In window "Maven repo..." select the prefered version from local repo. and add:

    So finally i get the version i prefere in the project with javadoc and src:




     
  • Best practice (Small solution) for me is now this:
    - create the pom using an archetype
    - add <dependencyManagement../> decribing the prefered dependency in the top of the pom
    - add <scope>provided</scope>
    - optionaly you can add the <exclusions.. />
    - remove the conflicting <version../> tags in the generated pom
    - the <dependencyManagement../> section i will use for copy/paste in other projects

    Example for junit:
    <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <!-- http://maven.apache.org/plugins/maven-enforcer-plugin/rules/versionRanges.html -->
                    <!-- <version>[4.8.2,)</version> use 4.8.2 or higher version -->
                    <version>4.8.2</version>
                    <scope>provided</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>

    Remove the conflicting dependencies by editing the pom manually in the lines marked by Netbeans ;-)



    Or when use "Add Dependency" you then see the tab "Dependency Management" offering the prefered distinct dependency and version:


  • Greater solution (hopefully) ?

    - create a parent/master pom with <dependencyManagement../> of prefered artifacts
    - use the from archetype generated pom as module or child-project
    - use the master pom for grouping dependencies with <packaging>pom</packaging>
Some of the stuff i find usefull for this theme: :
Maven-Model (look for dependencyManagement under project)
Maven: The Complete Reference, Chapter 3.6.1. Grouping Dependencies

Long time no post...

Things at work changed the last year. So priorities changed.

Becoming a member of a new team that is using Linux, C, Java, jsp, little php and much Oracle pl/sql, there were plenty of things to learn for me, coming from Microsoft (old world) C++.
So there is a little time to read the daily feeds that i prefered.
Less time to learn Scala and other interessting things like Android.
Lesser time and motivation (does anybody ever read the last posts ?) to write on the blog.

But Scala (language) has getting greater, mature and stable this last year i think.
There are some themes were it is possible to mix up the Java know how,
i need for work, with new technics i like to use with Scala.

Another point is english is not my first language. Like to guess what it is ?
(Are comments working here ? Hmmm ~:-|, i'm still learning Wordpress...)

Reading, hearing means no problem, having eight years of english at school and several lessons on university,
but if you have no great traveler background and not some years of study or practice in foreign countries it is difficult to
find the right words speaking and of cause writing in english.
But being a long time it-professional (hopefully) i'm used to read books in english.
When reading on the internet, i don't distinguish between english and native language.

So i will retry to write something useful (i hope) here. You can beat me, if i get ready to activate the comments one time...

But now i think it's better going one step back and install a local version of Wordpress and explore first ?