03. Maven Dependency Feature

Source: Internet
Author: User
Tags valid versions xmlns maven central

Original address: http://www.iteye.com/topic/1123232

The jar packages we use in our projects can be introduced in a way that relies on them, and when they are built, they are downloaded from the MAVEN repository.

1. Dependency Configuration
Dependencies can be declared as follows:

XML code

1. <project>

2 .....

3. <dependencies>

4. <dependency>

5. <groupId>group-a</groupId>

6. <artifactId>artifact-a</artifactId>

7. <version>1.0</version>

8. <exclusions>

9. <exclusion>

Ten. <groupId>group-c</groupId>

One. <artifactId>excluded-artifact</artifactId>

</exclusion>

</exclusions>

</dependency>

<dependency>.

<groupId>group-a</groupId>

<artifactId>artifact-b</artifactId>

<version>1.0</version>

<type>bar</type>

<scope>runtime</scope>

</dependency>

</dependencies>

</project>

We met the concept of dependency in Maven Combat (ii) and the test in the project relies on the JUnit jar package, which depends on the following configuration:

XML code

1. <dependencies>

2. <dependency>

3. <groupId>junit</groupId>

4. <artifactId>junit</artifactId>

5. <version>3.8.1</version>

6. <scope>test</scope>

7. </dependency>

8. </dependencies>

Dependencies include elements such as basic GroupID, artifactid,version, and dependencies under the root element project can contain one or more dependency elements to declare one or more dependencies.
Here's a detailed explanation of the elements each dependency can contain:

Groupid,artifactid and version: the basic coordinates of the dependency, the basic coordinates are the most important for any one dependency, and Maven can find the required dependencies based on the coordinates.

Type: The dependent types, corresponding to the packaging defined by the project coordinates. In most cases, the element does not have to be declared, and its default value is the jar

Scope: The range of dependencies, detailed below

Optional: Whether tag dependency is optional

Exclusions: Used to exclude transitive dependencies, which are explained below

Most dependency declarations contain only basic coordinates.


2. Scope of dependence

Maven needs to use a set of classpath when compiling the main code, using a different set of classpath when compiling and executing the test, and using a set of classpath when actually running the project.

The scope of dependency is to control the relationship of dependencies with these three classpath (compile classpath, test classpath, run Classpath), and Maven has the following dependencies:

Compile: Compilation dependency range. If not specified, the dependency scope is used by default. Using this dependency-dependent Maven dependency is valid for compiling, testing, and running three kinds of classpath.

Test: Tests the dependency range. With this dependent maven dependency, only valid for test classpath, this type of dependency cannot be used when compiling the master code or running the project's use. The typical example is JUnit, which is only needed when compiling test code and running tests.

Provided: The range of dependencies has been provided. Use of this dependent MAVEN dependency is valid for compilation and test classpath, but not valid at run time. The typical example is SERVLET-API, which requires this dependency when compiling and testing a project, but when the project is run, it is not necessary for Maven to introduce it repeatedly because the container is already available.

Runtime: Run-time dependent range. Use of this dependent MAVEN dependency is valid for testing and running classpath, but not when compiling the master code. A typical example is the JDBC driver implementation, where the compilation of the project master code requires only the JDBC interface provided by the JDK, and only the specific JDBC driver for the above interface is required to execute the test or run the project.

System: The range of systems dependencies. The dependence is in line with the three kinds of classpath, and the provided depends on the scope. However, you must explicitly specify the path to the dependent file through the Systempath element when using system-wide dependencies. Because such dependencies are not parsed by the MAVEN repository and are often bound to native systems, this can cause the build to be non-portable and should be used with caution. The Systempath element can reference environment variables such as:

XML code

1. <dependency>

2. <groupId>javax.sql</groupId>

3. <artifactId>jdbc-stdext</artifactId>

4. <version>2.0</version>

5. <scope></scope>

6. <systemPath>${java.home}/lib/rt.jar</systemPath>

7. </dependency>

Import (Maven 2.0.9 and above): Imports dependent scopes. The dependency range does not have a real impact on three classpath, which is described later.

3. Transitive dependencies

Below we look at a simple project, the reader can download the source code from the attachment

The Pom.xml configuration is as follows:

XML code

1. <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

2. xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >

3. <modelVersion>4.0.0</modelVersion>

4.

5. <groupId>com.mycompany.app</groupId>

6. <artifactId>my-app-simple</artifactId>

7. <version>0.0.1-SNAPSHOT</version>

8. <packaging>jar</packaging>

9.

Ten. <name>my-app-simple</name>

One. <url>http://maven.apache.org</url>

12.

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>.

16.

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

24.

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>2.5.6</version>

</dependency>

</dependencies>

</project>

We can see that this project introduces dependency JUnit and Spring-core, and we can find the Spring-core widget in the Maven repository, as shown in the figure:


Click on Pom and we'll see that the file contains a commons-logging dependency:

XML code

1. <dependency>

2. <groupId>commons-logging</groupId>

3. <artifactId>commons-logging</artifactId>

4. <version>1.1.1</version>

5. </dependency>


The dependency is then passed to the current project, which is the transitive nature of the dependency, which opens the project to view maven dependencies:



4. Optional dependencies

Sometimes we don't want to let the dependency pass, so we can configure the dependency as an optional dependency and set the element optional to True, for example:

XML code

1. <dependency>

2. <groupId>commons-logging</groupId>

3. <artifactId>commons-logging</artifactId>

4. <version>1.1.1</version>

5. <optional>true<optional>

6. </dependency>

Then another project that relies on the project will not get the pass of this dependency

5. Exclude Dependencies

When we introduce third-party jar packages, we inevitably introduce transitive dependencies, sometimes it's good, but sometimes we don't need some of these transitive dependencies

For example, the project in the previous example, we do not want to introduce transitive dependency commons-logging, we can use the exclusions element declaration to exclude dependencies, exclusions can contain one or more exclusion child elements, One or more transitive dependencies can therefore be excluded. It is important to note that the declaration of exclusions requires only GroupID and Artifactid, not the version element, because only GroupID and Artifactid are required to uniquely locate a dependency in the dependency graph. In other words, in Maven's parsed dependencies, it is unlikely that GroupID and Artifactid will be the same, but version two depends on each other.

Here is an example of an exclusion dependency:

XML code

1. <dependency>

2. <groupId>org.springframework</groupId>

3. <artifactId>spring-core</artifactId>

4. <version>2.5.6</version>

5. <exclusions>

6. <exclusion>

7. <groupId>commons-logging</groupId>

8. <artifactId>commons-logging</artifactId>

9. </exclusion>

Ten. </exclusions>

One. </dependency>

5. Dependency classification

If we had a lot of dependencies on the spring framework in our project, they were org.springframework:spring-core:2.5.6, Org.springframework:spring-beans : 2.5.6,org.springframework:spring-context:2.5.6, they are all different modules from the same project. Therefore, all of these dependent versions are the same, and it is foreseeable that these dependent versions will be upgraded together if the spring Framework needs to be upgraded in the future. Therefore, we should define the version in a unique place and refer to this version in the dependency declaration, which only needs to be modified when the spring framework is upgraded.

XML code

1. <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

2. xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >

3. <modelVersion>4.0.0</modelVersion>

4.

5. <groupId>com.mycompany.app</groupId>

6. <artifactId>my-app-simple</artifactId>

7. <version>0.0.1-SNAPSHOT</version>

8. <packaging>jar</packaging>

9. <name>my-app-simple</name>

Ten. <properties>

One. <springframework.version>2.5.6</springframework.version>

</properties>

13.

<dependencies>

<dependency>.

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

21st.

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>${springframework.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-beans</artifactId>

<version>${springframework.version}</version>

</dependency>

</dependencies>

</project>

6. Managing dependencies in Eclipse

After installing the M2eclipse (2nd lesson in detail) you can use Eclipse to manage dependencies.

As shown in the figure, click Dependency hierarchy in the pom.xml of the project to see the dependency tree:



Click Dependencies to add a new dependency, click on Select a dependency, click Remove to delete, click Add can be added a dependency, as shown:

As shown below, search for the org.springframework (here is the one from the MAVEN central repository), select the module and version you want, click OK:


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.