Maven Combat (vi) The difference between---dependencies and dependencymanagement

Source: Internet
Author: User

in the previous project encountered a number of jar package conflicts, and then there are many people do not know the difference between dependencies and dependencymanagement, this article summarizes these differences.

1.depencymanagement Application Scenario

when we have a lot of project modules, we use Maven to manage our projects very conveniently and help us manage the methods of build, document, report, rely,scms, publish, distribute. It is easy to compile code, manage dependencies, manage binaries, and more.

since we have a lot of modules, we have abstracted a layer and pulled out a itoo-base-parent to manage the public dependencies of the subproject. In order for the project to run correctly, you must make all of the child projects use a consistent version of the dependencies, and you must ensure that the dependencies and versions of each project are applied consistently to ensure that the same results are tested and published.

in the POM file at the top of our project , we will see the dependencymanagement element. This element is used to manage The version of the jar package, so that the subproject references a list version number that is dependent instead of being displayed. Maven walks up the parent-child hierarchy until it finds a project with the dependencymanagement element, and then it uses the dependencymanagement The version number specified in the element.

Take a look at the applications in our project:

Pom inheritance diagram:

Dependency Relationship:

Itoo-base-parent (Pom.xml)

[Java]View PlainCopy print?
  1. <dependencyManagement>
  2. <dependencies>
  3. <dependency>
  4. <groupId>org.eclipse.persistence</groupId>
  5. <artifactId>org.eclipse.persistence.jpa</artifactId>
  6. <version>${org.eclipse.persistence.jpa.version}</version>
  7. <scope>provided</scope>
  8. </dependency>
  9. <dependency>
  10. <groupId>javax</groupId>
  11. <artifactId>javaee-api</artifactId>
  12. <version>${javaee-api.version}</version>
  13. </dependency>
  14. </dependencies>
  15. </dependencyManagement>

Itoo-base (Pom.xml)

[Java]View PlainCopyprint?
  1. <!--inherit from parent class--
  2. <parent>
  3. <artifactId>itoo-base-parent</artifactId>
  4. <groupId>com.tgb</groupId>
  5. <version>0.0. 1-snapshot</version>
  6. <relativepath>, .... /itoo-base-parent/pom.xml</relativepath>
  7. </parent>
  8. <modelVersion>4.0. 0</modelversion>
  9. <artifactId>itoo-base</artifactId>
  10. <packaging>ejb</packaging>
  11. <!--dependencies--
  12. <dependencies>
  13. <dependency>
  14. <groupId>javax</groupId>
  15. <artifactId>javaee-api</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>com.fasterxml.jackson.core</groupId>
  19. <artifactId>jackson-annotations</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.eclipse.persistence</groupId>
  23. <artifactId>org.eclipse.persistence.jpa</artifactId>
  24. <scope>provided</scope>
  25. </dependency>
  26. </dependencies>
  27. </project>

the benefit of doing this is to manage the version number of the project uniformly, to ensure that the dependencies and versions of the applications are consistent, to ensure that the same results are tested and published, and therefore to define common dependencies in the top-level pom . You can also avoid declaring a version number in each subproject you use, so that when you want to upgrade or switch to another version, you only need to update it in the parent class container, without any modification of the subproject, or if a subproject requires a different version number, only the dependencies You can declare a version number in the Subclasses use the version number declared by the subclass and do not inherit the version number of the parent class.

2,Dependencies

All life dependencies in dependencies are automatically introduced relative to Dependencymanagement and are inherited by all sub-projects by default.

3. Difference

Dependencies even if the dependency is not written in a subproject, the child project will still inherit the dependency from the parent project (inherit all)

dependencymanagement only the declaration relies on, and does not implement the introduction , so the subproject needs to display the claims that need to be relied upon. If a dependency is not declared in a subproject, it is not inherited from the parent project, and the item is inherited from the parent project only if the dependency is written in the subproject and no specific version is specified , and both version and scope are read from the parent Pom; In addition, if a version number is specified in a subproject, the jar version specified in the subproject is used .

4.Maven convention is better than configuration

it proposes this concept to provide a reasonable default behavior for the project without unnecessary configuration. The default directory is provided

SRC -- root directory for > source Code and test code

Source directory for main application code

Java Source Code

resource files for the resources project

source directory for test code

Java Test Code

resource file for the resources test

Target compiled class files,jar files, etc.

for the understanding that MAVEN convention is better than configuration, on the one hand, for small projects basically meet our needs do not need to configure their own things, using Maven has been configured to quickly get started, learning cost reduction, on the other hand, You can also customize settings that don't meet our needs and show flexibility. The configuration is greatly reduced, and as the project becomes more complex, this advantage becomes more pronounced.

http://blog.csdn.net/liutengteng130/article/details/46991829

Maven Combat (vi) The difference between---dependencies and dependencymanagement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.