maven-dependency-plugin插件的使用

maven-dependency-plugin是处理与依赖相关的插件。它有很多可用的goal,大部分是和依赖构建、分析和解决相关的goal,这部分goal可以直接用maven的命令操作,例如:mvn dependency:tree、mvn dependency:analyze;这类操作在平时的maven应用中很少会用到。这里主要介绍除此之外的、用得最多的几个操作:copy, copy-dependencies和它们对应的unpack, unpack-dependencies

 

首先声明插件:

	
		
			
				org.apache.maven.plugins
				maven-dependency-plugin
				2.8
			
		
	

 

 

copy 和 unpack

copy操作可以用来将某个(些)maven artifact(s)拷贝到某个目录下。添加phase和goal如下:

	
		
			
				org.apache.maven.plugins
				maven-dependency-plugin
				2.8
				
					
						package
						
							copy
						
					
				
			
		
	

然后就是配置,copy可以的配置的项比较多,详细的请参考:copy配置。下面是一些常用项说明:

Name Type Since Description
artifactItems List 1.0 Collection of ArtifactItems to work on. (ArtifactItem contains groupId, artifactId, version, type, classifier, outputDirectory, destFileName and overWrite.) See Usage for details.
outputDirectory File 1.0 Default output location used for mojo, unless overridden in ArtifactItem.
Default value is${project.build.directory}/dependency.
User property isoutputDirectory.
prependGroupId boolean 2.7 Prepend artifact groupId during copy
Default value isfalse.
User property ismdep.prependGroupId.
  • prependGroupId: 用来指示拷出来的library名字需要不需要加上groupId,默认是不加
  • outputDirectory: 用来指定拷出后Libraries的存放地

这里除了artifactItems没有默认值,需要指定外,所有其他的选项都可以被忽略:

				
					
						
							junit
							junit
							4.11
						
					
				
以上配置会将junit包拷到target/dependency目录下,文件名为:junit-4.11.jar。

 

如果想把它拷到lib目录下,可以如下配置:

				
					
						
							junit
							junit
							4.11
						
					
					lib
				
或者:
				
					
						
							junit
							junit
							4.11
							lib
						
					
				
根据上面的说明,artifactItem里可以有以下几个参数:

 

  • groupId 
  • artifactId
  • version
  • type
  • classifier
  • outputDirectory
  • destFileName
  • overWrite

同样的参数,artifactItem里的优先级更高,例如:

				
					
						
							junit
							junit
							4.11
						
						
							org.slf4j
							slf4j-log4j12
							1.7.7
							lib2
						
					
					lib
				
其中junit会拷到lib目录下,因为它没有定义自己的outputDirectory;slf4j-log4j12会拷到lib2下,因为它定义了自己的outputDirectory。 

 

unpack和copy类似,只不过它会把拷来的包解开,例如:

				
					
						package
						
							unpack
						
					
				
				
					
						
							junit
							junit
							4.11
						
						
							org.slf4j
							slf4j-log4j12
							1.7.7
							lib2
						
					
					lib
				
则junit和slf4j-log4j12拷完以后,放到lib和lib2下的不再是Jar包,还是Jar包里的内容。 

 

copy-dependencies 和 unpack-dependencies

上面介绍的copy 和 unpack操作是由要拷某个包,这个包需要具体指定要拷哪个包,与当前工程的依赖没有关系。copy-dependencies和它有点类似,但是它是用来拷当前工程的依赖包的,典型的,例如我们有一个web应用,当打成war包的时候,它所有的依赖也需要被打到应用中。 

 

copy-dependencies的参数有很多,详细的可以参考:copy-dependencies Doc,但是几乎所有都有默认值。所以一个最简单的定义如下:

			
				org.apache.maven.plugins
				maven-dependency-plugin
				2.8
				
					
						package
						
							copy-dependencies
						
					
				
			
这里没有指定任何配置,所有的参数都用默认值,则当前工程的所有依赖(直接、间接的)都会被拷到target/dependency目录下。

 

也可以使用outputDirectory指定存放在。另外,以下几个参数可以控制哪些依赖将被拷出(或排除):

Name Type Since Description
excludeArtifactIds String 2.0 Comma separated list of Artifact names to exclude.
User property isexcludeArtifactIds.
excludeClassifiers String 2.0 Comma Separated list of Classifiers to exclude. Empty String indicates don't exclude anything (default).
User property isexcludeClassifiers.
excludeGroupIds String 2.0 Comma separated list of GroupId Names to exclude.
User property isexcludeGroupIds.
excludeScope String 2.0 Scope to exclude. An Empty string indicates no scopes (default).
User property isexcludeScope.
excludeTransitive boolean 2.0 If we should exclude transitive dependencies
Default value isfalse.
User property isexcludeTransitive.
excludeTypes String 2.0 Comma Separated list of Types to exclude. Empty String indicates don't exclude anything (default).
User property isexcludeTypes.
includeArtifactIds String 2.0 Comma separated list of Artifact names to include.
User property isincludeArtifactIds.
includeClassifiers String 2.0 Comma Separated list of Classifiers to include. Empty String indicates include everything (default).
User property isincludeClassifiers.
includeGroupIds String 2.0 Comma separated list of GroupIds to include.
User property isincludeGroupIds.
includeScope String 2.0 Scope to include. An Empty string indicates all scopes (default). The scopes being interpreted are the scopes as Maven sees them, not as specified in the pom. In summary:
  • runtime scope gives runtime and compile dependencies,
  • compile scope gives compile, provided, and system dependencies,
  • test (default) scope gives all dependencies,
  • provided scope just gives provided dependencies,
  • system scope just gives system dependencies.

User property isincludeScope.
includeTypes       String 2.0 Comma Separated list of Types to include. Empty String indicates include everything (default).
User property isincludeTypes.

 

例如当前工程有以下依赖:

	
		
			junit
			junit
			4.11
			test
		
		
			org.slf4j
			slf4j-log4j12
			1.7.7
			test
		
		
			org.apache.camel
			camel-script
			2.13.2
		
		
			org.apache.camel
			camel-spring
			2.13.2
		
		
			org.apache.camel
			camel-xstream
			2.13.2
		
		
			org.springframework
			spring-jms
			3.2.4.RELEASE
		
		
			org.springframework
			spring-tx
			3.2.4.RELEASE
		
		
			org.apache.activemq
			activemq-all
			5.10.0
		
		
			com.thoughtworks.xstream
			xstream
			1.4.7
		
		
			org.ogce
			xpp3
			1.1.6
		
	
要排除所有scope为test的依赖:
			
				org.apache.maven.plugins
				maven-dependency-plugin
				2.8
				
					
						package
						
							copy-dependencies
						
					
				
				
					compile
				
			
注意:这里不能test,这样会把所有compile级别的也排除。看下图:  

 

Copied From:  Dependencies Scopes
scope/phase-> compile test run assembly
compile U U U U
provided U ! ! !
runtime ! U U U
test ! U ! !

 

说明:最左侧是表示dependency的scope级别,顶行表示maven的阶段,可以看出:compile级别的dependency会在所有阶段都被使用。

 

要排除所有camel的依赖,如下:

				
					org.apache.camel
				
要排除除camel-spring外的所有其他依赖如下:
				
					camel-spring
				

你可能感兴趣的:(Maven)