标签:
A1)groupId:定义当前 maven 项目隶属的实际项目;maven 项目和 实际项目不是一一对应的,如spring framework是一个实际项目,而 maven 项目有很多如 spring-core, spring-context等;A2)artifactId:定义实际项目中的一个 maven 项目(模块),使用实际项目名称作为 artifactId的前缀,以便于寻找实际构件;(其groupId 为spring,而artifaceId 为 spring-aop 或 spring-core 等等 )
A3)packaging:该元素定义 maven 项目的打包方式;打包方式有 jar 和 war ,前者是默认的;A4)classifier:用来帮助定义构建输出的一些附属构件。附属构件 与 主构件 相对应,如主构件 spring-aop-4.2.6.RELEASE.jar ,其附属构件还可以是 spring-aop-4.2.6.RELEASE-javadoc.jar,spring-aop-4.2.6.RELEASE-sources.jar;(干货——引入了主构件和附属构件)
A1)以上坐标的5个元素:前3个是必须的,而 packaging 是可选的,而 classifier 是不能直接定义的;A2)项目构件的文件名和坐标是对应的:规则为 artifactId-version [-classfier].packaging ;
<project> ...... <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.7</version> <type>....</type> <scope>test</scope> <optional>...</optional> <exclusions> <exclusion>...</exclusion> </exclusions> </dependency> </dependencies> ... </project>
A1)type:是依赖类型 ,默认为jar ,不必声明;A2)scope:依赖的范围(test,compile(默认));A3)optional:标记是否可选;A4)exclusions:用来排除传递性依赖;
classpath1)首先:编译项目主代码的时候需要用到 spring-core,该文件以依赖的方式被引入到 classpath中;classpath2)其次:maven 在编译和执行测试的时候会使用另外一套 classpath;classpath3)最后:实际运行maven 项目的时候,又会使用一套 classpath;
scope1)compile:编译依赖范围scope2)test:测试依赖范围:scope3)provided:已提供依赖范围;scope4)runtime:运行时 依赖范围;scope5)system:系统依赖范围;(最好不要使用)scope6)import:导入依赖范围;
1.1)problem:考虑一个基于 spring framework 的项目,如果不使用 maven ,那么在项目中就需要 手动下载相关依赖;1.2)solution:maven 的 传递性依赖就可以很好解决这个问题;
荔枝1)如项目A 有这样的依赖关系: A->B->C->X(1.0) AND A->D->X(2.0),则 依赖调节的第一原则是:路径最近者优先(长度最短者),所以会选择 X(2.0);荔枝2)如项目A 有这样的依赖关系: A->B->X(1.0) AND A->D->X(2.0);则依赖调节的第二原则是:第一声明者优先,在长度相等的case下,在pom 中声明的顺序决定了谁会被使用;(干货——调节依赖的第一和第二原则)
C1)在理想case下,是不应该使用可选依赖的。因为在面向对象设计中,有一个 单一职责性原则,指一个类应该只有一个职责,而不是柔和太多的职责;C2)那所以,在上面的case上,就应该 分别为 mysql 和 oracle 创建一个 maven项目,基于同样的 groupId 不同的 artifactId ,在各自的 pom 中声明对应的 jdbc 驱动依赖,而不是使用可选依赖;
D:\classical_books\java_set\maven_in_action\mycode\chapter3>mvn dependency:list [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for com.maven.chapter3:service:jar:1.0-SNAPSHOT [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 22, column 15 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building service says hello maven. 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-dependency-plugin:2.8:list (default-cli) @ service --- [INFO] [INFO] The following files have been resolved: [INFO] junit:junit:jar:4.7:test [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 7.462 s [INFO] Finished at: 2016-06-18T20:52:40+08:00 [INFO] Final Memory: 13M/153M [INFO] ------------------------------------------------------------------------
D:\classical_books\java_set\maven_in_action\mycode\chapter3>mvn dependency:tree [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for com.maven.chapter3:service:jar:1.0-SNAPSHOT [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 22, column 15 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building service says hello maven. 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ service --- [INFO] com.maven.chapter3:service:jar:1.0-SNAPSHOT [INFO] \- junit:junit:jar:4.7:test // 传递性依赖,该项目会传递依赖于 junit; [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.172 s [INFO] Finished at: 2016-06-18T20:54:26+08:00 [INFO] Final Memory: 13M/217M [INFO] ------------------------------------------------------------------------
D:\classical_books\java_set\maven_in_action\mycode\chapter3>mvn dependency:analyze [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for com.maven.chapter3:service:jar:1.0-SNAPSHOT [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 22, column 15 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building service says hello maven. 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] >>> maven-dependency-plugin:2.8:analyze (default-cli) > test-compile @ service >>> [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ service --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ service --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ service --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ service --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] <<< maven-dependency-plugin:2.8:analyze (default-cli) < test-compile @ service <<< [INFO] [INFO] --- maven-dependency-plugin:2.8:analyze (default-cli) @ service --- [INFO] No dependency problems found [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.028 s [INFO] Finished at: 2016-06-18T20:56:17+08:00 [INFO] Final Memory: 15M/222M [INFO] ------------------------------------------------------------------------
A1)Used undeclared dependencies:意指项目中使用到 的,但没有显式声明的依赖;A2)Unused declared dependencies:意指项目中未使用到的,但显式声明的依赖;
标签:
原文地址:http://blog.csdn.net/pacosonswjtu/article/details/51707190