1.Maven依赖总结
1.依赖元素
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${springframework.version}</version>
- <type>jar</type>
- <scope>compile</scope>
- </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自身处理,只要理解下即可
第一列为第一依赖,第二列为第二依赖,单元格为传递范围
compile | test | provided | runtime | |
compile | compile | _ | _ | runtime |
test | test | _ | _ | test |
provided | provided | _ | provided | provided |
runtime | runtime | _ | _ | runtime |
4.依赖调解
传递路径长度取最短原则,传递路径长度相等时,采取最先申明原则
5.可选依赖
尽量少用,可选依赖不会被传递,需要显式申明
6.排除依赖
发现依赖包里有些包不稳定,可以排除依赖,显式的申明文档的包
- <dependency>
- <groupId>javax.mail</groupId>
- <artifactId>mail</artifactId>
- <version>1.4.1</version>
- <exclusions>
- <exclusion>
- <groupId>javax.activation</groupId>
- <artifactId>activation</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>javax.activation</groupId>
- <artifactId>activation</artifactId>
- <version>1.1</version>
- </dependency>
7.分类依赖
当同一个模块,所依赖的几个模块版本都相同时,可以使用maven里的属性做分类依赖,依赖版本升级时改一处即可
- <properties>
- <springframework.version>2.5.6</springframework.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${springframework.version}</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-beans</artifactId>
- <version>${springframework.version}</version>
- <type>pom</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>${springframework.version}</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>${springframework.version}</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- </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.项目信息
- <!-- 版本控制 -->
- <scm>
- <connection>scm:git:git@github.com:lastsweetop/account.git</connection>
- <developerConnection>scm:git:git@github.com:lastsweetop/account.git</developerConnection>
- <url>https://github.com/lastsweetop/account/blob/master</url>
- </scm>
- <!-- 持续集成 -->
- <ciManagement>
- <system>Hudson</system>
- <url>http://${distribution.repository}:8080/hudson</url>
- </ciManagement>
- <!-- 项目团队 -->
- <developers>
- <developer>
- <id>sweetop</id>
- <name>sweetop</name>
- <email>sweetop@189.cn</email>
- <timezone>8</timezone>
- </developer>
- </developers>
- <!-- 项目授权 -->
- <licenses>
- <license>
- <name>Apache License,Version 2.0</name>
- <url>http://www.apache.org/licenses/LICENSE-2.0</url>
- </license>
- </licenses>
- <!-- 问题跟踪 -->
- <issueManagement>
- <system>urltracker</system>
- <url>http://10.18.96.90/URTracker/Accounts/Login.aspx</url>
- </issueManagement>
- <span style="white-space:pre"> </span><plugin>
- <!-- 站点生成插件 -->
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-site-plugin</artifactId>
- <version>3.2</version>
- <configuration>
- <!-- 支持国际化 -->
- <locales>zh_CN</locales>
- <reportPlugins>
- <plugin>
- <!-- 生成javadoc文件 -->
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-javadoc-plugin</artifactId>
- <version>2.9</version>
- </plugin>
- <plugin>
- <!-- 项目信息过滤 -->
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-project-info-reports-plugin</artifactId>
- <version>2.6</version>
- <reportSets>
- <reportSet>
- <reports>
- <!-- index一定要生成,否则链接问题 -->
- <report>index</report>
- <report>dependencies</report>
- <report>project-team</report>
- <report>issue-tracking</report>
- <report>license</report>
- <report>scm</report>
- <report>cim</report>
- <report>modules</report>
- <report>plugins</report>
- <report>help</report>
- <report>summary</report>
- </reports>
- </reportSet>
- </reportSets>
- </plugin>
- <plugin>
- <!-- 源码查看生成 -->
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-jxr-plugin</artifactId>
- <version>2.3</version>
- <configuration>
- <!-- 多模块聚合 -->
- <aggregate>true</aggregate>
- </configuration>
- </plugin>
- <plugin>
- <!-- 代码风格检查 -->
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-checkstyle-plugin</artifactId>
- <version>2.10</version>
- <configuration>
- <!-- 使用maven社区代码风格 -->
- <configLocation>config/maven_checks.xml</configLocation>
- </configuration>
- </plugin>
- <plugin>
- <!-- 代码更改日志 -->
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-changelog-plugin</artifactId>
- <version>2.2</version>
- </plugin>
- <plugin>
- <!-- 代码隐藏bug检查 -->
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-pmd-plugin</artifactId>
- <version>3.0</version>
- <configuration>
- <!-- 多模块聚合 -->
- <aggregate>true</aggregate>
- </configuration>
- </plugin>
- <plugin>
- <!-- 测试覆盖率 -->
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>cobertura-maven-plugin</artifactId>
- <version>2.5.2</version>
- </plugin>
- </reportPlugins>
- </configuration>
- </plugin>
3.项目站点生成命令
- mvn site
- clean site site:stage -DstagingDirectory=/Users/apple/site
- <span style="white-space:pre"> </span><distributionManagement>
- <site>
- <id>website</id>
- <url>file:///Users/apple/site/</url>
- </site>
- </distributionManagement>
4.项目站点自定义
- <?xml version="1.0" encoding="UTF-8"?>
- <project name="Account" xmlns="http://maven.apache.org/DECORATION/1.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/DECORATION/1.0.0 http://maven.apache.org/xsd/decoration-1.0.0.xsd">
- <bannerLeft>
- <!-- 左上角图片 -->
- <name>maven</name>
- <src>images/apache-maven-project.png</src>
- <href>http://projects.apache.org/</href>
- </bannerLeft>
- <bannerRight>
- <!-- 右上角图片 -->
- <name>java</name>
- <src>images/java_logo.jpg</src>
- <href>http://www.java.com</href>
- </bannerRight>
- <!-- 版本信息放在右边 -->
- <version position="right" />
- <!-- 发布信息放在右边 -->
- <publishDate position="right" />
- <body>
- <breadcrumbs>
- <!-- 横条链接 -->
- <item name="lastsweetop" href="http://blog.csdn.net/lastsweetop" />
- <item name="account" href="http://192.168.115.5:8080/account/index.html"/>
- </breadcrumbs>
- <menu ref="parent"/>
- <!-- 项目信息报告 inherit表示子模块继承父模块-->
- <menu ref="reports" inherit="top"></menu>
- </body>
- <skin>
- <!-- 谷歌皮肤 -->
- <groupId>com.googlecode.fluido-skin</groupId>
- <artifactId>fluido-skin</artifactId>
- <version>1.3</version>
- </skin>
- </project>