如何更好的管理maven依赖包的版本

1、如果你的pom.xml 文件内有很多相同的groupId(这现象spring等大型框架非常常见),你应该用pom中的properties部分,然后引用它们。

比如:


		UTF-8
		UTF-8
		1.8
		1.8
		true
		true
		UTF-8

		3.4
		2.4
		1.0

		4.1.6.RELEASE
		3.1.4.RELEASE
		4.0.0.RELEASE

		3.1

		1.1.3
		1.2.17
		1.7.12

	


		
			
				org.springframework
				spring-framework-bom
				${spring.version}
				pom
				import
			
		
	
	
		
			org.opencredo.esper
			esper-template
			2.1
			
				
					com.espertech
					esper
				
				
					org.slf4j
					slf4j-api
				
				
					org.springframework
					spring-context
				
			
		
		
			com.espertech
			esper
			5.2.0
			
				
					log4j
					log4j
				
				
					commons-logging
					commons-logging
				
			
		

		
			com.alibaba
			fastjson
			1.1.43
		

		
			com.google.guava
			guava
			18.0
		

		
			org.apache.commons
			commons-lang3
			${commons-lang3.version}
		

		
			commons-io
			commons-io
			${commons-io.version}
		
		
			commons-primitives
			commons-primitives
			${commons-primitives.version}
		
		
			org.springframework
			spring-context
			
				
					commons-logging
					commons-logging
				
			
		

		
			org.springframework
			spring-context-support
		

		
			org.springframework
			spring-aspects
		

		
			org.springframework
			spring-asm
			${spring-asm.version}
		

		
			com.alibaba
			druid
			1.0.14
		
		
			postgresql
			postgresql
			9.1-901-1.jdbc4
		

		
			org.slf4j
			slf4j-api
			${slf4j-api.version}
		
		
			ch.qos.logback
			logback-classic
			${logback-classic.version}
		

		
			ch.qos.logback
			logback-core
			${logback-classic.version}
		

		
			org.slf4j
			jcl-over-slf4j
			${slf4j-api.version}
		
		
			org.slf4j
			log4j-over-slf4j
			${slf4j-api.version}
		

		
			org.springframework
			spring-test
		
		
			junit
			junit
			4.11
			test
		

	

2、可视化观察依赖关系

现在的ide,比如spring官方的sts,就自带了可视化查看依赖。

如何更好的管理maven依赖包的版本_第1张图片


我们也可以用maven dependency插件查看,

[doctor@localhost esper-2015]$ mvn dependency:tree
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building esper-2015 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ esper-2015 ---
[INFO] com.doctor:esper-2015:jar:1.0.0-SNAPSHOT
[INFO] +- org.opencredo.esper:esper-template:jar:2.1:compile
[INFO] +- com.espertech:esper:jar:5.2.0:compile
[INFO] |  +- org.antlr:antlr4-runtime:jar:4.1:compile
[INFO] |  |  \- org.abego.treelayout:org.abego.treelayout.core:jar:1.0.1:compile
[INFO] |  \- cglib:cglib-nodep:jar:3.1:compile
[INFO] +- com.alibaba:fastjson:jar:1.1.43:compile
[INFO] +- com.google.guava:guava:jar:18.0:compile
[INFO] +- org.apache.commons:commons-lang3:jar:3.4:compile
[INFO] +- commons-io:commons-io:jar:2.4:compile
[INFO] +- commons-primitives:commons-primitives:jar:1.0:compile
[INFO] +- org.springframework:spring-context:jar:4.1.6.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:4.1.6.RELEASE:compile
[INFO] |  |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  +- org.springframework:spring-beans:jar:4.1.6.RELEASE:compile
[INFO] |  +- org.springframework:spring-core:jar:4.1.6.RELEASE:compile
[INFO] |  \- org.springframework:spring-expression:jar:4.1.6.RELEASE:compile
[INFO] +- org.springframework:spring-context-support:jar:4.1.6.RELEASE:compile
[INFO] +- org.springframework:spring-aspects:jar:4.1.6.RELEASE:compile
[INFO] |  \- org.aspectj:aspectjweaver:jar:1.8.5:compile
[INFO] +- org.springframework:spring-asm:jar:3.1.4.RELEASE:compile
[INFO] +- com.alibaba:druid:jar:1.0.14:compile
[INFO] |  +- com.alibaba:jconsole:jar:1.8.0:system
[INFO] |  \- com.alibaba:tools:jar:1.8.0:system
[INFO] +- postgresql:postgresql:jar:9.1-901-1.jdbc4:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.7.12:compile
[INFO] +- ch.qos.logback:logback-classic:jar:1.1.3:compile
[INFO] +- ch.qos.logback:logback-core:jar:1.1.3:compile
[INFO] +- org.slf4j:jcl-over-slf4j:jar:1.7.12:compile
[INFO] +- org.slf4j:log4j-over-slf4j:jar:1.7.12:compile
[INFO] +- org.springframework:spring-test:jar:4.1.6.RELEASE:compile
[INFO] \- junit:junit:jar:4.11:test
[INFO]    \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.904 s
[INFO] Finished at: 2015-06-13T09:55:09+08:00
[INFO] Final Memory: 15M/205M
[INFO] ------------------------------------------------------------------------
[doctor@localhost esper-2015]$ 

上面都可以观察依赖,主要解决jar包依赖冲突现象。排除低版本的jar本。sts支持可视化管理排除。

传递性依赖我们可以用, exclusions and optional aven either by the depending 排除。

你可能感兴趣的:(maven,maven学习)