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

No comments:

Post a Comment