0

I am not sure I really understand the strongest feature of Maven - Dependency Management.

When I am developing a project I need to add all dependencies while I am writing the code. Therefore when building the project with Maven I already have all dependencies downloaded.

What is the point of the Dependency Management then?

CuriousGuy
  • 1,545
  • 3
  • 20
  • 42
  • What would you do without that feature? – Seelenvirtuose Feb 24 '17 at 10:08
  • @Seelenvirtuose do you mean that we need it just because when we build with Maven, we can't somehow add path to our dependencies and therefore Maven just needs them so it can do its own job of building? – CuriousGuy Feb 24 '17 at 10:11
  • No. I mean: If you do not have a dependency management tool, how would you handle dependencies (libraries) that your project has? Think about it a while. And consider several projects that might have some dependencies in common. Now consider that projects should be put into a source repository (GIT, SVN, ...), so that other developers can work on it, too. Considering that all, a mechanism to only declare dependencies (which is text, by the way), and a tool that does the magic, is a good way. – Seelenvirtuose Feb 24 '17 at 10:15
  • @Selelenvirtouse So, you download all libraries with Maven and you add the dir where they are downloaded to your classpath? – CuriousGuy Feb 24 '17 at 10:31
  • You want to push jar files to your git repo every time and download them whenever you clone/checkout? – Pallav Jha Feb 24 '17 at 10:35
  • @CuriousGuy This is done by Maven automatically. A great improvement, isn't it? – Seelenvirtuose Feb 24 '17 at 12:24

3 Answers3

0

Thanks to Maven you do not need to download the dependencies and put it into the lib on Tomcat for instance. It is done automatically by maven. You can see directly in the pom.xml file all you dependencies and also other settings.

Dependencies management is just one of the main feature of Maven. I think you should ask yourself what are the addotional tasks (external libs, deployment, documentation etc...) you are performing in your project and see if Maven can help you on these tasks with its plugins.

Jiujiu
  • 180
  • 11
  • Okay, but I can't start coding without the dependencies to be installed first. Do you first run maven to download them before starting to code? – CuriousGuy Feb 24 '17 at 10:19
0

Let's assume For your local you have downloaded all the necessary jars and kept in lib.Now you want to move your code into many machines. So now you have to transfer all the files to another machine.

By mistake if you miss some jars while sending or let's say in future you want newer version of jar.So Do you like to send it to all the machines the jar and remove the older version from lib or write a configuration that automatically does all the things in one go.

Once the project gets bigger you will feel the real essence of Maven.It's just the pom.xml you need to take care all the jar management.

  • Okay, but after maven downloads the dependencies in its default directory, how can I use them to run the project from IDE for example? I need to add the dir to my class path? – CuriousGuy Feb 24 '17 at 10:25
  • No by default it will get added to Java BuildPath under the name Maven Dependencies. Create a sample application and try. – Karthik Bharadwaj Feb 24 '17 at 10:31
  • If your IDE is `Eclipse` then you need to create this project as Maven project and `Eclipse` will automatically read your `pom.xml` file and download the dependencies. You also need to take care about the nature of your project: jar, war, etc... – Jiujiu Feb 24 '17 at 10:32
0

Maven's dependency management makes most sense in an environment where you work on multiple projects, across multiple dev machines and more than one dev.

Imagine the simplest of scenarios:

Project_1
   \lib
       \log4j.jar
Project_2
   \lib
       \log4j.jar
Project_3
   \lib
       \log4j.jar

While you are developing you will need to copy paste the log4j.jar file to all of those projects, which translates to extra disk space used locally and on any SCM you may use, and need to go into each project and define it as a library (add it to the classpath). If you want to change the version of the jar you need to repeat the process. If you use Maven, all you need to do is

  1. define an online repository (example Maven online repo)

  2. create a pom for your project:

    <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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>COM.MY.COMPANY</groupId>
    <artifactId>NAME_OF_PROJECT</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    
    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        <version>1.2.17</version>
        </dependency>
    
    </dependencies>
    

Most IDE's already have seamless integration with Maven and no further hassle is needed. And the above steps are only executed once.

Some additional references:

  1. Why maven? What are the benefits?
  2. Why should we use Maven?
  3. Maven Dependency Plugin Example
Community
  • 1
  • 1
Viorel Florian
  • 594
  • 9
  • 29
  • But you still need to change the pom.xml in each project, right? – CuriousGuy Feb 24 '17 at 11:01
  • It does make sense always. It doesn't matter if you are working alone or not...It helps in many aspects of handling dependencies etc. – khmarbaise Feb 24 '17 at 12:10
  • I does make sense everywhere, at the very least as practice. I use it ie even for the proof of concept projects. Ince you set up the repo (in you mavent settings) it is a sinch to a create a new project. Let me edit my answer. – Viorel Florian Feb 25 '17 at 15:07
  • @CuriosityGuy: yes you will have to maintain the pom, which a module descriptor. Common properties and dependencies can be referenced in a super-pom (parent). Either way you look at it you need dependency management, maven or ant and ivy. But with maven it works out of the box and it supports ant scripts – Viorel Florian Feb 25 '17 at 15:12