dependency和dependencyManagement

来源:互联网 发布:linux下的sleep函数 编辑:程序博客网 时间:2024/06/06 12:49

dependencydependencyManagement

问题描述

这个问题是因为在maven clean install时,发现总是提示找不到版本号,查看pom.xml文件,发现在dependency中,确实没有版本号,但在其父maven工程中,发现了dependencyManagement中有

            <dependency>

                <groupId>org.hhc.spring</groupId>

                <artifactId>spring-artifacts</artifactId>

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

                <scope>import</scope>

                <type>pom</type>

            </dependency>

然后查看spring-artifacts的pom.xml,在<dependencyManagement>中有

      <dependency>

        <groupId>${project.groupId}.ping</groupId>

        <artifactId>ping-service</artifactId>

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

      </dependency>

解决办法:重新编译一下spring-artifacts工程就可以解决

 

dependencyManagement

Maven 使用dependencyManagement 元素来提供了一种管理依赖版本号的方式。通常会在一个组织或者项目的最顶层的父POM 中看到dependencyManagement 元素。使用pom.xml 中的dependencyManagement 元素能让

所在子项目中引用一个依赖而不用显式的列出版本号。Maven 会沿着父子层次向上走,直到找到一个拥有dependencyManagement 元素的项目,然后它就会使用在这个dependencyManagement 元素中指定的版本号。

 

例如在父项目里:

Xml代码  

<dependencyManagement>  

<dependencies>  

<dependency>  

<groupId>mysql</groupId>  

<artifactId>mysql-connector-java</artifactId>  

<version>5.1.2</version>  

</dependency>  

...  

<dependencies>  

</dependencyManagement>  

然后在子项目里就可以添加mysql-connector时可以不指定版本号,例如:

Xml代码  

<dependencies>  

<dependency>  

<groupId>mysql</groupId>  

<artifactId>mysql-connector-java</artifactId>  

</dependency>  

</dependencies>  

这样做的好处就是:如果多个子项目都引用同一样依赖,则可以避免在每个使用的子项目里都声明一个版本号,这样当想升级或切换到另一个版本时,只需要在顶层父容器里更新,而不需要一个一个子项目的修改 ;另外如果某个子项目需要另外的一个版本,只需要声明version就可。

 dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显式的声明需要用的依赖。

dependencies

相对于dependencyManagement,所有声明在dependencies里的依赖都会自动引入,并默认被所有的子项目继承。

classifier

如果你要发布同样的代码,但是由于技术原因需要生成两个单独的构件,你就要使用一个分类器(classifier)。例如,如果你想要构建两个单独的构件成JAR,一个使用Java 1.4 编译器,另一个使用Java 6 编译器,你就可以使用分类器

来生成两个单独的JAR构件,它们有同样的groupId:artifactId:version组合。如果你的项目使用本地扩展类库,你可以使用分类器为每一个目标平台生成一个构件。分类器常用于打包构件的源码,JavaDoc 或者二进制集合。

依赖范围

Dependency scope 是用来限制Dependency的作用范围的, 影响maven项目在各个生命周期时导入的package的状态。

自从2.0.9后,新增了1种,现在有了6种scope:

compile

默认的scope,表示 dependency 都可以在生命周期中使用。而且,这些dependencies 会传递到依赖的项目中。

provided

compile相似,但是表明了dependency 由JDK或者容器提供,例如Servlet AP和一些Java EE APIs。这个scope 只能作用在编译和测试时,同时没有传递性。

使用这个时,不会将包打入本项目中,只是依赖过来。   

使用默认或其他时,会将依赖的项目打成jar包,放入本项目的Lib里

when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.

 

Xml代码  

<!-- Servlet -->  

        <dependency>  

            <groupId>javax.servlet</groupId>  

            <artifactId>servlet-api</artifactId>  

            <version>2.5</version>  

            <scope>provided</scope>  

        </dependency>  

        <dependency>  

            <groupId>javax.servlet.jsp</groupId>  

            <artifactId>jsp-api</artifactId>  

            <version>2.1</version>  

            <scope>provided</scope>  

        </dependency>  

 

runtime

表示dependency不作用在编译时,但会作用在运行和测试时

test

表示dependency作用在测试时,不作用在运行时。

system

provided 相似,但是在系统中要以外部JAR包的形式提供,maven不会在repository查找它。 例如:

<project>

...

<dependencies>

<dependency>

<groupId>javax.sql</groupId>

<artifactId>jdbc-stdext</artifactId>

<version>2.0</version>

<scope>system</scope>

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

</dependency>

</dependencies>

...

</project>

 

 

import (Maven 2.0.9 之后新增)

它只使用在<dependencyManagement>中,表示从其它的pom中导入dependency的配置,例如:    This scope is only used on a dependency of type pom in the <dependencyManagement> section. It indicates that the specified POM should be replaced with the dependencies in that POM's <dependencyManagement> section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

 

<project>

<modelVersion>4.0.0</modelVersion>

<groupId>maven</groupId>

<artifactId>B</artifactId>

<packaging>pom</packaging>

<name>B</name>

<version>1.0</version>

 

<dependencyManagement>

    <dependencies>

      <dependency>

        <groupId>maven</groupId>

        <artifactId>A</artifactId>

        <version>1.0</version>

        <type>pom</type>

        <scope>import</scope>

      </dependency>

      <dependency>

        <groupId>test</groupId>

        <artifactId>d</artifactId>

        <version>1.0</version>

      </dependency>

    </dependencies>

</dependencyManagement>

</project>

B项目导入A项目中的包配置

参考

http://liugang594.iteye.com/blog/1687781

 

http://uule.iteye.com/blog/2087485

 

 

原创粉丝点击