使用Maven

1.使用属性properties和exclusions

如果要阻断一些传递的依赖,可以添加:exclusions,这样可以不在构建的时候往我们的构件离添加太多无用的jar,前提是经过测试,阻断依赖后程序功能无异常,或添加了其它相同功能的依赖,如阻断收费版,换成依赖免费版。

Xml代码   收藏代码
  1. <dependencies>  
  2.   <dependency>  
  3.     <groupId>org.spring.framework</groupId>  
  4.     <artifactId>spring-core</artifactId>  
  5.     <version>${spring.version}</version>  
  6.   </dependency>  
  7.   <dependency>  
  8.     <groupId>org.spring.framework</groupId>  
  9.     <artifactId>spring-beans</artifactId>  
  10.     <version>${spring.version}</version>  
  11.   </dependency>  
  12.   <dependency>  
  13.     <groupId>org.spring.framework</groupId>  
  14.     <artifactId>spring-web</artifactId>  
  15.     <version>${spring.version}</version>  
  16.   </dependency>  
  17.   <dependency>  
  18.     <groupId>org.spring.framework</groupId>  
  19.     <artifactId>spring-mock</artifactId>  
  20.     <version>${spring.version}</version>  
  21.   </dependency>  
  22. </dependencies>  
  23.   
  24. <properties>  
  25.   <spring.version>2.5</spring.version>  
  26. </properties>  

这里我们定义了一个Maven属性,其名称为spring.version,值是2.5。在这个POM中,我们就能用${spring.version}的方式来引用该属性。我们看到,所有spring相关的依赖的version元素现在都成了${spring.version},当Maven运行的时候,它会自动用值2.5来替换这个引用。

当我们需要升级spring的时候,只要更改一个地方便可,而且,你现在能很高的保证所有的spring依赖包都是同一个版本。

 

2.依赖范围(scope)

Xml代码   收藏代码
  1. <dependency>  
  2.   <groupId>junit</groupId>  
  3.   <artifactId>junit</artifactId>  
  4.   <version>4.4</version>  
  5.   <scope>test</test>  
  6. </dependency>  
Xml代码   收藏代码
  1. <dependency>  
  2.   <groupId>javax.servlet</groupId>  
  3.   <artifactId>servlet-api</artifactId>  
  4.   <version>2.4</version>  
  5.   <scope>provided</scope>  
  6. </dependency>  

主要的依赖范围以及作用:

 

依赖范围(scope)主源码classpath可用测试源码classpath可用会被打包
compile 缺省值TRUETRUETRUE
testFALSETRUEFALSE
runtimeFALSETRUETRUE
providedTRUETRUEFALSE

 

 

 

 

 

 

 

 

3.分类器(classifer)

GAV(groupid、artifactid、version)是Maven坐标最基本最重要的组成部分,但GAV不是全部。还有一个元素叫做分类器(classifier),90%的情况你不会用到它,但有些时候,分类器非常不可或缺的,因为有写jar的同一个gae会根据环境的不同如jdk版本不同会有差异,例如:json-lib 的2.2.2版本有jdk13的版本,也有jdk15的发布板,如果不再依赖上添加<classifier>jdk15</classifier> 就会报jar找不到的情况,可以查看依赖库,发现它是由这个分类的。

我们也可以为自己的jar分类,比如按系统或jdk版本分类等待。

同样的如果要阻止一些依赖传递,可以添加:exclusions,这样可以不在构建的时候往我们的构件离添加太多无用的jar,前提是经过测试,阻断依赖后程序功能无异常,或添加了其它相同功能的依赖,如阻断收费版,换成依赖免费版。



4.依赖管理(dependencyManagement)

实际的项目中,你会有一大把的Maven模块,而且你往往发现这些模块有很多依赖是完全项目的,A模块有个对spring的依赖,B模块也有,它们的依赖配置一模一样,同样的groupId, artifactId, version,或者还有exclusions, classifer。细心的分会发现这是一种重复,重复就意味着潜在的问题,Maven提供的dependencyManagement就是用来消除这种重复的。

正确的做法是:

1. 在父模块中使用dependencyManagement配置依赖

2. 在子模块中使用dependencies添加依赖

dependencyManagement实际上不会真正引入任何依赖,dependencies才会。但是,当父模块中配置了某个依赖之后,子模块只需使用简单groupId和artifactId就能自动继承相应的父模块依赖配置。

这里是一个来自于《Maven权威指南》的例子:

父模块中如此声明:

Xml代码   收藏代码
  1. <project>  
  2.   <modelVersion>4.0.0</modelVersion>  
  3.   <groupId>org.sonatype.mavenbook</groupId>  
  4.   <artifactId>a-parent</artifactId>  
  5.   <version>1.0.0</version>  
  6.   ...  
  7.   <dependencyManagement>  
  8.     <dependencies>  
  9.       <dependency>  
  10.         <groupId>mysql</groupId>  
  11.         <artifactId>mysql-connector-java</artifactId>  
  12.         <version>5.1.2</version>  
  13.       </dependency>  
  14.       ...  
  15.     <dependencies>  
  16.   </dependencyManagement>  

子模块中如此声明:

Xml代码   收藏代码
  1. <project>  
  2.   <modelVersion>4.0.0</modelVersion>  
  3.   <parent>  
  4.     <groupId>org.sonatype.mavenbook</groupId>  
  5.     <artifactId>a-parent</artifactId>  
  6.     <version>1.0.0</version>  
  7.   </parent>  
  8.   <artifactId>project-a</artifactId>  
  9.   ...  
  10.   <dependencies>  
  11.     <dependency>  
  12.       <groupId>mysql</groupId>  
  13.       <artifactId>mysql-connector-java</artifactId>  
  14.     </dependency>  
  15.   </dependencies>  
  16. </project>  

你依赖配置越复杂,依赖管理所起到的作用就越大,它不仅能够帮助你简化配置,它还能够帮你巩固依赖配置,也就是说,在整个项目中,对于某个构件(如mysql)的依赖配置只有一种,这样就能避免引入不同版本的依赖,避免依赖冲突。


 5.插件管理<pluginManagement>

它的功能与作用与dependencyManagement类似,一般也是在父pom中定义插件的版本等配置信息,子pom只需要声明groupid和artifactid就可以继承使用所有的配置信息,在多模块中这是非常有用的,所以良好的pom编写习惯都应用在父pom中定义这些依赖和插件的管理,他们都不会引入依赖或插件,而只是定义了一些配置模板而已。


6.资源(resources)和java属性文件application.properties

我们知道maven的pom中定义了很多内置和自定义的properties如${project.name} ${project.baseDir} ${project.baseDir} ${project.artifactId} ${project.build.testSourceDirectory} 等,那么我们如何在java代码中使用呢?

maven已经帮我们做了,在main/resources下建立application.properties,内容如下:

[plain]  view plain copy
  1. # in ava code, you can read this application.properties for getting the properties difined in maven pom  
  2. application.name=${pom.name}  
  3. application.version=${pom.version}  
  4. message=${my.filter.value}  
  5.   
  6. # do mvn process-resources "-Dcommand.line.prop=hello again" can get init the value of key mycommandkey!!!  
  7. mycommandkey=${command.line.prop}  

需要在pom中添加:

[plain]  view plain copy
  1. <build>  
  2.         <resources>  
  3.             <resource>  
  4.                 <directory>src/main/resources</directory>  
  5.                 <filtering>true</filtering>  
  6.             </resource>  
  7.         </resources>  
  8.     </build>  

目的是告诉maven,这个文件下的资源需要处理,即将资源文件中的 pom 变量引用替换成实际的值,我们可以运行mvn process-resources然后再编译好的classes文件夹中查看转换后的application.properties,java代码中只需要读取这个属性文件就可以了。也可以往属性文件中添加需要命令行指定的参数,如上面的 mycommandkey=${command.line.prop},赋值方式:mvn process-resources "-Dcommand.line.prop=hello"


7.使用Profile,不同环境不同配置

这块内容详细:http://haohaoxuexi.iteye.com/blog/1900568

profile可以让我们定义一系列的配置信息,然后指定其激活条件。这样我们就可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果。比如说,我们可以通过profile定义在jdk1.5以上使用一套配置信息,在jdk1.5以下使用另外一套配置信息;或者有时候我们可以通过操作系统的不同来使用不同的配置信息,比如windows下是一套信息,linux下又是另外一套信息,等等,示例:

[plain]  view plain copy
  1. <profiles>    
  2.         <profile>    
  3.              <id>profileTest1</id>    
  4.              <properties>    
  5.                     <hello>world</hello>    
  6.              </properties>    
  7.              <activation>    
  8.                     <activeByDefault>true</activeByDefault>    
  9.              </activation>    
  10.         </profile>    
  11.             
  12.         <profile>    
  13.              <id>profileTest2</id>    
  14.              <properties>    
  15.                     <hello>andy</hello>    
  16.              </properties>    
  17.         </profile>    
  18.  </profiles>    

定义在pom.xml中的profile可以定义更多的信息。主要有以下这些:

l  <repositories>

l  <pluginRepositories>

l  <dependencies>

l  <plugins>

l  <properties>

l  <dependencyManagement>

l  <distributionManagement>

l  还有build元素下面的子元素,主要包括:

<defaultGoal>

<resources>

<testResources>

<finalName>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值