简体   繁体   中英

What is a Maven Dependency exactly?

I have just started to learn Spring using Maven. Can somebody clearly explain?

In your codebase, you will have a multitude of packages. Each of those packages will have a pom.xml file which have maven dependencies in there. Those are the dependencies that get pulled in when doing an 'mvn install' on that particular package. Eg one of your packages which uses spring will probably have this :

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

Further, your package will also depend on other packages, and so will have dependencies on those packages. Each package gets a .jar file of its own when built (which contains .class files). A certain package doesn't rely on all other packages in the codebase, so it just pulls in the ones needed. These packages can be published and pulled in from a locally hosted Artifactory, and in the case of spring it probably gets pulled in from an online maven repo.

The fetched artifacts (.jar files) get put into a hidden repository folder (mine's called .m2/repository) which you can configure in your IDE, and the fetching is done smartly. If it exists already, it won't do the effort to pull in a new one. If you however do want to override the currently fetched artifact, look at this question I asked when I was struggling to understand maven myself.

Notice the < version > tag. This tells maven the version to fetch, and if it sees that version already exists (I'm not sure how it checks, it probably looks at the folder name or some file inside like MANIFEST.MF) it doesn't bother fetching it. In case you have a dependency which has frequent updates, changing this version field all the time can be bothersome, you can make it such that it fetches the latest all the time.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM