Maven笔记总结

1.Maven依赖总结

1.依赖元素

[html]  view plain copy
  1. <dependency>  
  2.     <groupId>org.springframework</groupId>  
  3.     <artifactId>spring-core</artifactId>  
  4.     <version>${springframework.version}</version>  
  5.     <type>jar</type>  
  6.     <scope>compile</scope>  
  7. </dependency>  

groupId,必选,实际隶属项目

artifactId,必选,其中的模块

version必选,版本号

type可选,依赖类型,默认jar

scope可选,依赖范围,默认compile

optional可选,标记依赖是否可选,默认false

exclusion可选,排除传递依赖性,默认空

2.依赖范围

maven项目又三种classpath(编译,测试,运行)

scope用来表示与classpath的关系,总共有五种

compile:编译,测试,运行

test:测试

provided:编译,测试

runtime:运行

system:编译,测试,同provided,但必须指定systemPath,慎用

3.传递性依赖

顾名思义,你懂的,但是传递的范围会发生改变,这个由maven自身处理,只要理解下即可

第一列为第一依赖,第二列为第二依赖,单元格为传递范围

 compiletestprovidedruntime
compilecompile__runtime
testtest__test
providedprovided_providedprovided
runtimeruntime__runtime

4.依赖调解

传递路径长度取最短原则,传递路径长度相等时,采取最先申明原则

5.可选依赖

尽量少用,可选依赖不会被传递,需要显式申明

6.排除依赖

发现依赖包里有些包不稳定,可以排除依赖,显式的申明文档的包

[html]  view plain copy
  1. <dependency>  
  2.     <groupId>javax.mail</groupId>  
  3.     <artifactId>mail</artifactId>  
  4.     <version>1.4.1</version>  
  5.     <exclusions>  
  6.         <exclusion>  
  7.             <groupId>javax.activation</groupId>  
  8.             <artifactId>activation</artifactId>  
  9.         </exclusion>  
  10.     </exclusions>  
  11. </dependency>  
  12. <dependency>  
  13.     <groupId>javax.activation</groupId>  
  14.     <artifactId>activation</artifactId>  
  15.     <version>1.1</version>  
  16. </dependency>  

7.分类依赖

当同一个模块,所依赖的几个模块版本都相同时,可以使用maven里的属性做分类依赖,依赖版本升级时改一处即可

[html]  view plain copy
  1. <properties>  
  2.     <springframework.version>2.5.6</springframework.version>  
  3. </properties>  
  4. <dependencies>  
  5.     <dependency>  
  6.         <groupId>org.springframework</groupId>  
  7.         <artifactId>spring-core</artifactId>  
  8.         <version>${springframework.version}</version>  
  9.         <type>jar</type>  
  10.         <scope>compile</scope>  
  11.     </dependency>  
  12.     <dependency>  
  13.         <groupId>org.springframework</groupId>  
  14.         <artifactId>spring-beans</artifactId>  
  15.         <version>${springframework.version}</version>  
  16.         <type>pom</type>  
  17.         <scope>compile</scope>  
  18.     </dependency>  
  19.     <dependency>  
  20.         <groupId>org.springframework</groupId>  
  21.         <artifactId>spring-context</artifactId>  
  22.         <version>${springframework.version}</version>  
  23.         <type>jar</type>  
  24.         <scope>compile</scope>  
  25.     </dependency>  
  26.     <dependency>  
  27.         <groupId>org.springframework</groupId>  
  28.         <artifactId>spring-context-support</artifactId>  
  29.         <version>${springframework.version}</version>  
  30.         <type>jar</type>  
  31.         <scope>compile</scope>  
  32.     </dependency>  
  33. </dependencies>  

8.优化依赖

可概括为三个命令

mvn dependency:list

表示依赖列表,maven eclipse插件已经实现,有图形化显示,在pom.xml的dependencies页


mvn dependency:tree

表示依赖列表,maven eclipse插件已经实现,有图形化显示,在pom.xml的dependency hierarchy页

mvn dependency:analyze

查找出在编译和测试中未使用但显示声明的依赖


2.Maven项目站点生成


1.项目信息

[html]  view plain copy
  1. <!-- 版本控制 -->  
  2.     <scm>  
  3.         <connection>scm:git:git@github.com:lastsweetop/account.git</connection>  
  4.         <developerConnection>scm:git:git@github.com:lastsweetop/account.git</developerConnection>  
  5.         <url>https://github.com/lastsweetop/account/blob/master</url>  
  6.     </scm>  
  7.   
  8.     <!-- 持续集成 -->  
  9.     <ciManagement>  
  10.         <system>Hudson</system>  
  11.         <url>http://${distribution.repository}:8080/hudson</url>  
  12.     </ciManagement>  
  13.   
  14.     <!-- 项目团队 -->  
  15.     <developers>  
  16.         <developer>  
  17.             <id>sweetop</id>  
  18.             <name>sweetop</name>  
  19.             <email>sweetop@189.cn</email>  
  20.             <timezone>8</timezone>  
  21.         </developer>  
  22.     </developers>  
  23.   
  24.     <!-- 项目授权 -->  
  25.     <licenses>  
  26.         <license>  
  27.             <name>Apache License,Version 2.0</name>  
  28.             <url>http://www.apache.org/licenses/LICENSE-2.0</url>  
  29.         </license>  
  30.     </licenses>  
  31.   
  32.     <!-- 问题跟踪 -->  
  33.     <issueManagement>  
  34.         <system>urltracker</system>  
  35.         <url>http://10.18.96.90/URTracker/Accounts/Login.aspx</url>  
  36.     </issueManagement>  


2.项目站点插件

[html]  view plain copy
  1. <span style="white-space:pre">          </span><plugin>  
  2.                 <!-- 站点生成插件 -->  
  3.                 <groupId>org.apache.maven.plugins</groupId>  
  4.                 <artifactId>maven-site-plugin</artifactId>  
  5.                 <version>3.2</version>  
  6.                 <configuration>  
  7.                     <!-- 支持国际化 -->  
  8.                     <locales>zh_CN</locales>  
  9.                     <reportPlugins>  
  10.                         <plugin>  
  11.                             <!-- 生成javadoc文件 -->  
  12.                             <groupId>org.apache.maven.plugins</groupId>  
  13.                             <artifactId>maven-javadoc-plugin</artifactId>  
  14.                             <version>2.9</version>  
  15.                         </plugin>  
  16.                         <plugin>  
  17.                             <!-- 项目信息过滤 -->  
  18.                             <groupId>org.apache.maven.plugins</groupId>  
  19.                             <artifactId>maven-project-info-reports-plugin</artifactId>  
  20.                             <version>2.6</version>  
  21.                             <reportSets>  
  22.                                 <reportSet>  
  23.                                     <reports>  
  24.                                         <!-- index一定要生成,否则链接问题 -->  
  25.                                         <report>index</report>  
  26.                                         <report>dependencies</report>  
  27.                                         <report>project-team</report>  
  28.                                         <report>issue-tracking</report>  
  29.                                         <report>license</report>  
  30.                                         <report>scm</report>  
  31.                                         <report>cim</report>  
  32.                                         <report>modules</report>  
  33.                                         <report>plugins</report>  
  34.                                         <report>help</report>  
  35.                                         <report>summary</report>  
  36.                                     </reports>  
  37.                                 </reportSet>  
  38.                             </reportSets>  
  39.                         </plugin>  
  40.                         <plugin>  
  41.                             <!-- 源码查看生成 -->  
  42.                             <groupId>org.apache.maven.plugins</groupId>  
  43.                             <artifactId>maven-jxr-plugin</artifactId>  
  44.                             <version>2.3</version>  
  45.                             <configuration>  
  46.                                 <!-- 多模块聚合 -->  
  47.                                 <aggregate>true</aggregate>  
  48.                             </configuration>  
  49.                         </plugin>  
  50.                         <plugin>  
  51.                             <!-- 代码风格检查 -->  
  52.                             <groupId>org.apache.maven.plugins</groupId>  
  53.                             <artifactId>maven-checkstyle-plugin</artifactId>  
  54.                             <version>2.10</version>  
  55.                             <configuration>  
  56.                                 <!-- 使用maven社区代码风格 -->  
  57.                                 <configLocation>config/maven_checks.xml</configLocation>  
  58.                             </configuration>  
  59.                         </plugin>  
  60.                         <plugin>  
  61.                             <!-- 代码更改日志 -->  
  62.                             <groupId>org.apache.maven.plugins</groupId>  
  63.                             <artifactId>maven-changelog-plugin</artifactId>  
  64.                             <version>2.2</version>  
  65.                         </plugin>  
  66.                         <plugin>  
  67.                             <!-- 代码隐藏bug检查 -->  
  68.                             <groupId>org.apache.maven.plugins</groupId>  
  69.                             <artifactId>maven-pmd-plugin</artifactId>  
  70.                             <version>3.0</version>  
  71.                             <configuration>  
  72.                                 <!-- 多模块聚合 -->  
  73.                                 <aggregate>true</aggregate>  
  74.                             </configuration>  
  75.                         </plugin>  
  76.                         <plugin>  
  77.                             <!-- 测试覆盖率 -->  
  78.                             <groupId>org.codehaus.mojo</groupId>  
  79.                             <artifactId>cobertura-maven-plugin</artifactId>  
  80.                             <version>2.5.2</version>  
  81.                         </plugin>  
  82.                     </reportPlugins>  
  83.                 </configuration>  
  84.             </plugin>  


3.项目站点生成命令

[html]  view plain copy
  1. mvn site  
如果本地想看一下,多个模块链接会出现问题,可以用下面命令将生成文件聚合在一处
[html]  view plain copy
  1. clean site site:stage -DstagingDirectory=/Users/apple/site  
但是如想使用这个命令必须配置下面的发布管理配置
[html]  view plain copy
  1. <span style="white-space:pre">  </span><distributionManagement>  
  2.         <site>  
  3.             <id>website</id>  
  4.             <url>file:///Users/apple/site/</url>  
  5.         </site>  
  6.     </distributionManagement>  


4.项目站点自定义

在src/site下创建site.xml文件
[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project name="Account" xmlns="http://maven.apache.org/DECORATION/1.0.0"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://maven.apache.org/DECORATION/1.0.0 http://maven.apache.org/xsd/decoration-1.0.0.xsd">  
  5.     <bannerLeft>  
  6.         <!-- 左上角图片 -->  
  7.         <name>maven</name>  
  8.         <src>images/apache-maven-project.png</src>  
  9.         <href>http://projects.apache.org/</href>  
  10.     </bannerLeft>  
  11.     <bannerRight>  
  12.         <!-- 右上角图片 -->  
  13.         <name>java</name>  
  14.         <src>images/java_logo.jpg</src>  
  15.         <href>http://www.java.com</href>  
  16.     </bannerRight>  
  17.     <!-- 版本信息放在右边 -->  
  18.     <version position="right" />  
  19.     <!-- 发布信息放在右边 -->  
  20.     <publishDate position="right" />  
  21.     <body>  
  22.         <breadcrumbs>  
  23.             <!-- 横条链接 -->  
  24.             <item name="lastsweetop" href="http://blog.csdn.net/lastsweetop" />  
  25.             <item name="account" href="http://192.168.115.5:8080/account/index.html"/>  
  26.         </breadcrumbs>  
  27.         <menu ref="parent"/>  
  28.         <!-- 项目信息报告 inherit表示子模块继承父模块-->  
  29.         <menu ref="reports" inherit="top"></menu>  
  30.     </body>  
  31.     <skin>  
  32.         <!-- 谷歌皮肤 -->  
  33.         <groupId>com.googlecode.fluido-skin</groupId>  
  34.         <artifactId>fluido-skin</artifactId>  
  35.         <version>1.3</version>  
  36.     </skin>  
  37. </project>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值