Maven_2_核心知识
Documentation http://maven.apache.org/guides/index.html
-----3-1 常用构建命令------------------------------------------------------------------------------------------------------
mvn
-v
compile
test 测试
package 打包
clean 删除target
D:\java\code\maven01>mvn clean [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building maven01-model 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven01-model --- [INFO] Deleting D:\java\code\maven01\target [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.228 s [INFO] Finished at: 2017-06-27T17:22:55+08:00 [INFO] Final Memory: 9M/104M [INFO] ------------------------------------------------------------------------
install 安装jar包到本地仓库
example:maven02 中使用maven01的类
1) cd 到maven01 目录 ,mvn install 把jar包安装到本地仓库
[INFO] Installing D:\java\code\maven01\target\maven01-model-0.0.1-SNAPSHOT.jar to C:\Users\30478\.m2\repository\com\iooc\maven01-model\0.0.1-SNAPSHOT\maven01-model-0.0.1-SNAPSHOT.jar [INFO] Installing D:\java\code\maven01\pom.xml to C:\Users\30478\.m2\repository\com\iooc\maven01-model\0.0.1-SNAPSHOT\maven01-model-0.0.1-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 10.419 s [INFO] Finished at: 2017-06-27T17:40:35+08:00 [INFO] Final Memory: 18M/168M [INFO] ------------------------------------------------------------------------
2) 在maven02的pom.xml中添加依赖,
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion><!--固定--> <groupId>com.imooc.maven02</groupId><!--groupId项目包名--> <artifactId>maven02-model02</artifactId><!--artifactId模块名;建议使用项目名--> <version>0.0.1SNAPSHOT</version><!--快照版本--> <dependencies> <dependency> <!--junit依赖--> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> <dependency> <groupId>com.iooc</groupId> <artifactId>maven01-model</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
再cd 到maven02 ,执行mvn compile
D:\java\code\maven02>mvn compile [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for com.imooc.maven02:maven02-model02:jar:0.0.1SNAPSHOT [WARNING] 'version' uses an unsupported snapshot version format, should be '*-SNAPSHOT' instead. @ line 9, column 11 [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 maven02-model02 0.0.1SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven02-model02 --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\java\code\maven02\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven02-model02 --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent! [INFO] Compiling 1 source file to D:\java\code\maven02\target\classes [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.909 s [INFO] Finished at: 2017-06-27T17:43:33+08:00 [INFO] Final Memory: 13M/162M [INFO] ------------------------------------------------------------------------
通过mvn compile编译源代码,如果在编译的过程中程序用到了其他的依赖包,它会去pom.xml中查找是否引入了该依赖包的坐标(groupId,artifactId,version),
如果找到了该坐标,会去本地仓库中查找是否有相应的jar,如果没有的话,会去maven的中央仓库中去找并下载到本地仓库中,供项目引用。
----maven 自动建立目录骨架-------------------------------------------------------------------------------------------------------
建立maven03 目录
cd 到maven03根目录,
方式1 archetype:generate 按照提示:
mvn archetype:generate 会先下载依赖的jar
1878: remote -> us.fatehi:schemacrawler-archetype-plugin-lint (-)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 991: 回车
Choose org.apache.maven.archetypes:maven-archetype-quickstart version:
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
Choose a number: 6: 6 选择版本,此处选6
groupId: com.imooc.mavne03
artifactId: maven03-service
version: 1.0.0SNAPSHOT
package: com.imooc.maven03.server
Y: : y
-archetype-1.0.0.jar (3.7 kB at 8.3 kB/s) Define value for property 'groupId': com.imooc.mavne03 Define value for property 'artifactId': maven03-service Define value for property 'version' 1.0-SNAPSHOT: : 1.0.0SNAPSHOT Define value for property 'package' com.imooc.mavne03: : com.imooc.maven03.server Confirm properties configuration: groupId: com.imooc.mavne03 artifactId: maven03-service version: 1.0.0SNAPSHOT package: com.imooc.maven03.server Y: : y
Y: : y [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Archetype: maven-reactjs-blank-archetype:1.0.0 [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: com.imooc.mavne03 [INFO] Parameter: artifactId, Value: maven03-service [INFO] Parameter: version, Value: 1.0.0SNAPSHOT [INFO] Parameter: package, Value: com.imooc.maven03.server [INFO] Parameter: packageInPathFormat, Value: com/imooc/maven03/server [INFO] Parameter: package, Value: com.imooc.maven03.server [INFO] Parameter: version, Value: 1.0.0SNAPSHOT [INFO] Parameter: groupId, Value: com.imooc.mavne03 [INFO] Parameter: artifactId, Value: maven03-service [WARNING] CP Don't override file D:\java\code\maven03-service\app\components\Greet.jsx [WARNING] CP Don't override file D:\java\code\maven03-service\app\components\App.jsx [INFO] Project created from Archetype in dir: D:\java\code\maven03-service [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 05:53 min [INFO] Finished at: 2017-06-28T16:28:55+08:00 [INFO] Final Memory: 15M/135M [INFO] ------------------------------------------------------------------------
方式2 一次性输入所有参数(注意 参数名与参数值之间不能有空格 错: -DartifactId= maven04-model )
使用mvn archetype:generate -DgroupId=组织名 -DarticfactId=项目名-模块名 -Dversion=... -Dpackage=包 等待加载相应的依赖
mvn archetype:generate -DgroupId=com.imooc.maven04 -DartifactId=maven04-demo -Dversion=1.0.0SNAPSHO
T -Dpackage=com.imooc.maven04.demo
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 991: 回车
Choose org.apache.maven.archetypes:maven-archetype-quickstart version:
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
Choose a number: 6: 6
[INFO] Using property: groupId = com.imooc.maven04
[INFO] Using property: artifactId = maven04-demo
[INFO] Using property: version = 1.0.0SNAPSHOT
[INFO] Using property: package = com.imooc.maven04.demo
Confirm properties configuration:
groupId: com.imooc.maven04
artifactId: maven04-demo
version: 1.0.0SNAPSHOT
package: com.imooc.maven04.demo
Y: : y
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 991: Choose org.apache.maven.archetypes:maven-archetype-quickstart version: 1: 1.0-alpha-1 2: 1.0-alpha-2 3: 1.0-alpha-3 4: 1.0-alpha-4 5: 1.0 6: 1.1 Choose a number: 6: 6 [INFO] Using property: groupId = com.imooc.maven04 [INFO] Using property: artifactId = maven04-demo [INFO] Using property: version = 1.0.0SNAPSHOT [INFO] Using property: package = com.imooc.maven04.demo Confirm properties configuration: groupId: com.imooc.maven04 artifactId: maven04-demo version: 1.0.0SNAPSHOT package: com.imooc.maven04.demo Y: : y [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.1 [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: basedir, Value: D:\java\code\maven03 [INFO] Parameter: package, Value: com.imooc.maven04.demo [INFO] Parameter: groupId, Value: com.imooc.maven04 [INFO] Parameter: artifactId, Value: maven04-demo [INFO] Parameter: packageName, Value: com.imooc.maven04.demo [INFO] Parameter: version, Value: 1.0.0SNAPSHOT [INFO] project created from Old (1.x) Archetype in dir: D:\java\code\maven03\maven04-demo [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 01:59 min [INFO] Finished at: 2017-06-28T16:57:41+08:00 [INFO] Final Memory: 19M/207M [INFO] ------------------------------------------------------------------------
----3-3 maven 中的坐标和仓库-------------------------------------------------------------------------------------------------------
坐标 groupId,artifactId,version构成一个坐标,在maven项目中,所有的依赖,插件,项目构建的输出都可以称之为‘构件’,构件通过坐标作为其唯一标识。
构件 (groupId,artifactId,version等)
<groupId>com.imooc.mavne03</groupId>
<artifactId>maven03-service</artifactId>
<version>1.0.0SNAPSHOT</version> 组成基本坐标
(package 名应与groupId 和 artifactId 相吻合,看起来更加清晰)
仓库(依赖)
本地仓库和远程仓库
maven提供的超级pom:下载的maven/lib/maven-model-builder-3.3.3.jar/org/apache/maven/model/pom-4.0.0.xml
url为全球的中央仓库的地址,http://repo.maven.apache.org/maven2
id表示唯一标识,name为名称,url为地址,layout为布局,snapshot为快照版本,<enabled>false</enabled>表示禁止下载快照的版本。
镜像仓库的概念:A提供和B一样的功能。 maven中修改镜像仓库位置的文件:maven/conf/settings.xml 里mirrors
id为仓库标识,mirrorOf表示为谁创建镜像仓库,name为镜像仓库的命名,url为镜像仓库地址
仓库默认放在当前用户的目录中,.m2目录
更改仓库位置:找到maven的conf/settings.xml中的<localRepository></localRepository>为设置的路径,并把settings.xml拷贝到新创建的路径中,这样每次更新maven,就不用重新设置仓库路径了
---3-4 eclipse中安装maven插件以及创建maven项目------------------------------------------------------------------------------------
eclipse4.0以上版本,myEclipse不需要安装
查看方式 Eclipse-Windows-preferences中是否有maven选项
说明以3.6版本为例子:
1.安装maven插件
下载好的maven插件 放入eclipse/dropins目录,就完成安装,然后重启eclipse
放入到插件目录后,windows-preference中点击maven如果有如图所示的弹出框,表示eclipse需要jdk支持,可以 点击其中的eclipse.ini在其中配置jvm
配置与jdk相匹配的jre(preferences -java-Installed JREs)
修改settings.xml的路径(preferences -Maven-User Settings自己配置的本地仓库的路径)。
2 新建maven项目
新建maven项目- new -other-maven- maven project
目录模板选择quickstart
配置groupId,artifactId,version,点击finish
运行新建的maven项目,pom.xml点击右键-->run as-->maven build
在Main的goals中输入compile命令
console中报红字,此时项目并没有编译,此时在JRE的Default VM Arguments中配置'相应的报红字'=$M2_HOME
(右击项目-run as-maven build-输入compile报错: -Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match. 解决办法: Windows-preferences-Java-InstalledJRE-edit, -Dmaven.multiModuleProjectDirectory=$M2_HOME)
控制台无报错,同样步骤在goals中输入 package命令。
进入项目根目录进行查看,生成target文件,里面有classes,reports及相应的项目jar包
-----3-5 maven的生命周期和插件-----------------------------------------------------------------------------------
一个项目的完整的构建过程:清理·编译·测试·打包·集成测试·验证·部署
maven抽象出一套项目构建的生命周期,插件是对maven抽象的具体实现。
maven构建了三套项目生命周期,clean 清理项目,default 构建项目,site 生成项目站点
三个生命周期是相互独立的,每个生命周期之间又包含相应的阶段,每个阶段之间又是有顺序的。
且后一个阶段依赖前一个阶段,阶段之间是有顺序的,此阶段的执行不会触发其他的生命周期。
运行package时,虽然没有执行compile和test,但是这两个命令是依次执行的。(执行maven生命周期中的某个阶段时,其前面的每个阶段会依次执行。)
clean 清理项目
pre-clean 执行清理前的工作
clean 清理上一次构建生成的所有文件
post-clean 执行清理后的文件
default包含构建项目时所要执行的步骤。
compile test package install属于default默认的生命周期中。
site会根据pom.xml中的相关信息生成站点。
pre-site 在生成项目站点前要完成的工作
site 生成项目的站点文档
post-site 在生成项目站点后要完成的工作
site-deploy 发布生成的站点到服务器上
eclipse默认可操作的语言为java,但可以通过第三方插件来运行PHP,c等语言,这些语言运行时所需要执行的类似javac.exe则来自于插件中。maven也是通过插件的方式调用compile,test等。如果要在maven项目中添加一个插件,需要在pom.xml中添加
******
plugins参照
http://maven.apache.org/plugins/index.html
本例子:http://maven.apache.org/plugins/maven-source-plugin/
******
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase> <!-表示在package命令中执行此source插件- -->
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupId>com.imooc.hellow</groupId> <artifactId>mode</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>mode</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
goals中输入package ,执行 结果:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building mode 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mode ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\java\workspace\mode\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mode ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mode ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\java\workspace\mode\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mode ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mode ---
[INFO] Surefire report directory: D:\java\workspace\mode\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.imooc.hellow.mode.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.013 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mode ---
[INFO] Building jar: D:\java\workspace\mode\target\mode-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-source-plugin:2.4:jar-no-fork (default) @ mode ---
[INFO] Building jar: D:\java\workspace\mode\target\mode-0.0.1-SNAPSHOT-sources.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 19.411 s
[INFO] Finished at: 2017-06-29T10:29:43+08:00
[INFO] Final Memory: 17M/168M
[INFO] ------------------------------------------------------------------------
package 执行时自动执行了compile,test ,而且打包了source
执行maven生命周期中的某个阶段时,其前面的每个阶段会依次执行。
-----3-6 maven中Pom.xml解析-----------------------------------------------------------------------------------------------
pom.xml是maven项目的核心管理文件,用于项目描述,组织管理,依赖管理,构件信息的管理。
project是pom.xml的根元素,设定一些约束。
modelVersion指定了当前pom的版本,其次是坐标的信息,groupId为主项目标识,表示maven属于哪个主项目,由反写的公司网址+项目名组成。
实际的项目和maven项目会有些不同,maven项目体现的是一个模块化的概念,一个项目可能有几个模块构成。
artifactId就是项目实际的模块,项目名+模块名构成。
version表示版本号,一般有三个0构成,第一个0表示大版本好,第二个0表示分支版本号,第三个0表示小版本号。snapshot快照版本。版本有快照版本,α版本(内测版本)以及beta版本(公测版本),还有release版本,ga版本,release版本为一个稳定的版本,ga版本为一个正式的发布版本。
packaging标签表示maven项目默认的打包方式,默认为jar,也有war,zip,pom包。
name为项目描述名,url为项目地址,description为项目描述,developers为开发人员信息,licenses为许可证信息。
organization为组织信息。
dependencies表示依赖列表,dependency表示依赖项,scope表示依赖范围,optional表示设置依赖是否可选,默认为false,默认继承,为true则项目必须加载依赖项。exclusions表示排除依赖传递列表,比如如果项目引用spring,spring中被引用其他jar也会被引用,此为依赖传递。
dependencyManagement为依赖的管理,一般用于父模块中,运行时不会被编译加载依赖。用于子模块继承父模块中依赖,比如junit测试依赖。
build中为插件列表,会放相应的插件,parent用于对父模块中pom的继承,modules用于当项目启动时,需要多个模块是,对其他项目进行编译,不需要一个个进行编译。
pom.xml常用元素介绍: <project>根 <modelVersion>指定了当前pom的版本 <groupId>反写的公司网址+项目名 <artifactId>项目名+模块名 <version>第一个0表示大版本号、第二个0表示分支版本号、第三个0表示小版本号 0.0.1SNAPSHOT <packaging>打包方式,默认是jar <name>项目描述名 <url>项目地址 <description>项目描述 <developers>开发人员列表 <licenses>许可证信息 <organization>组织信息 <dependencies> <dependence>坐标groupId,artifactId,version <groupId></groupId> <artifactId></artifactId> <version></version> <type></type> <scope></scope>依赖范围 <optional></optional>设置依赖是否可选,true必须显示的引入该依赖/false继承 <exclusions>排除依赖传递列表(a依赖b,b依赖c,c对a 就是传递依赖)
<exclusion>
</exclusion>
</exclusions></dependence> </dependencies> 依赖管理
<dependencyManagement>
<dependence>
</dependence>
</dependencyManagement> <build> <plugins> <plugin> </plugin> </plugins> </build>
---3-7 maven 依赖范围dependency scope ----------------------------------------------------------------------
Documentation script:http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
当我们要引用某个框架的话,需要把项目的jar放入到项目的classpath中,这样项目就可以引用框架的jar了。
maven提供了三种calsspath,1编译,2测试,3运行
dependency中的scope就是控制依赖的范围,test表示仅在测试的classpath中。
scope有六种,compile,provided,runtime,test,system,import
provided在编译和测试时有效。
runtime在测试和运行时有效。
system与本机系统相关联,可移植性差,比如利用到本机的jdk配置。
import仅使用在dependencyManagement中,表示从其它的pom中导入dependency的配置。
如图表示将a中的依赖导入到B中。
<name>B</name>
<dependencyManagement>
<dependences>
<dependencey>
<groupId>maven</groupId>
<artifactId>A</artifactId>
<version>1.8</version>
<type>pom</type>
<scope>import</scope>
</dependencey>
</dependences>
</dependencyManagement>
----3-8 maven依赖传递-------------------------------------------------------------------------------------------------------
创建三个maven项目bge,nange,shanji。nange依赖bge项目启动,shanji依赖nange启动,启动下层项目时,上层需要打成jar。如图如果不写其中的exclusion中的内容时,shanji也会依赖bge的jar。此为传递依赖。
下层项目启动,上层没有打包,安装时的error msg。goals中直接输入clean
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building shanji 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [WARNING] The POM for com.hongxing:nange:jar:0.0.1-SNAPSHOT is missing, no dependency information available [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.306 s [INFO] Finished at: 2017-06-29T13:59:32+08:00 [INFO] Final Memory: 6M/123M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal on project shanji: Could not resolve dependencies for project com.hongxing:shanji:jar:0.0.1-SNAPSHOT: Could not find artifact com.hongxing:nange:jar:0.0.1-SNAPSHOT -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
bge b哥 (mvn package,mvn install 之后 nange才能找到(打包安装在本地仓库中))
安装成功log
[INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ nange --- [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ nange --- [INFO] Installing D:\java\workspace\nange\target\nange-0.0.1-SNAPSHOT.jar to D:\java\localRepository\com\hongxing\nange\0.0.1-SNAPSHOT\nange-0.0.1-SNAPSHOT.jar [INFO] Installing D:\java\workspace\nange\pom.xml to D:\java\localRepository\com\hongxing\nange\0.0.1-SNAPSHOT\nange-0.0.1-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.904 s [INFO] Finished at: 2017-06-29T14:01:29+08:00 [INFO] Final Memory: 12M/155M [INFO] ------------------------------------------------------------------------
nange 南哥 (mvn package,mvn install 之后 shanji才能找到(打包安装在本地仓库中))
shanji 山鸡
bge pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hongxing</groupId> <artifactId>bge</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>bge</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
nange pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hongxing</groupId> <artifactId>nange</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>nange</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.hongxing</groupId> <artifactId>bge</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
shanji
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hongxing</groupId> <artifactId>shanji</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>shanji</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.hongxing</groupId> <artifactId>nange</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
排除bge的依赖
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.hongxing</groupId> <artifactId>nange</artifactId> <version>0.0.1-SNAPSHOT</version> <exclusions> <exclusion> <groupId>com.hongxing</groupId> <artifactId>bge</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
bge已经被排除
注意 :JRE System Library 的版本maven 默认为1.5,此处已经手工修改为1.8
可以通过修改settings.xml来改变。
182行左右 找到 <profiles>添加如下代码
<profiles> <profile> <id>jdk-1.8</id> <activation> <activeByDefault>true</activeByDefault> <jdk>1.8</jdk> </activation> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> </properties>
要点1:install指令能把项目打包到本地仓库,运行install指令,同时还运行了package打包。 编译一个项目,如果存在依赖,首先会在本地仓库查找,如果没找到,直接到中央仓库查找,如果还是没找到,则报错。所以编译一个存在本地依赖的项目,本地依赖一定要运行install指令,把依赖的项目打包到本地仓库。 要点2: 如果想排除某个依赖的项目,则需要在pom.xml添加exclusions标签,添加到已经添加了排除依赖的依赖里,把需要排除的依赖的坐标引进来。
----3-9 依赖冲突-------------------------------------------------------------------------------------------------------
依赖冲突:
A->B->C->X(jar1.0)
A->D->X(jar1.1)
这是A依赖两个X的版本,会有冲突。
1.短路优先原则
bge 新加依赖 2.4
nange 新加依赖2.0
山鸡 由于短路优先 会是2.0
注:eclipse,在pom.xml中添加完依赖后会自动下载
2.如果路径长度相同的情况下,则谁先声明,先解析谁。(如果前面两个pom.xml都声明了同一个依赖的不同版本,第三个pom.xml同时依赖了前两个pom.xml,则谁的依赖坐标在前,就用谁的依赖版本。)
上例修改为 nange不依赖bge,shanji同时依赖nange和bge(exclusion去掉),bge坐标在前就解析为2.4
---3-10 maven聚合和继承--------------------------------------------------------------------------------------------------------
如果项目D依赖项目C,项目C依赖项目B,项目B依赖项目A,我们需要一个个安装这项项目,在Maven中有一种方式可以将多个项目一次性安装,这就是聚合的概念。简单讲就是,需要人工多次操作的,只要Maven能理解,一次性告诉他,他就能帮我们做这件单调烦人的事情了。——使用
maven聚合:聚合实际上是对多个项目同时进行做处理,比如编译、打包等。做法如下:创建一个新的maven项目,用于做其它项目的聚合,然后把pom.xml文件里的package标签里的jar改成pom,并去掉依赖,然后添加modules把其它项目引进来。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hongxing</groupId> <artifactId>aggreation</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>aggreation</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <modules> <module>../bge</module> <module>../nange</module> <module>../shanji</module> </modules> </project>
pom.xml - run as -maven build :
Goals: clean install (删除target 安装)
[INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Reactor Build Order: [INFO] [INFO] bge [INFO] nange [INFO] shanji [INFO] aggreation [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building bge 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom [INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom (3.9 kB at 2.4 kB/s) [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ bge --- [INFO] Deleting D:\java\workspace\bge\target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ bge --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory D:\java\workspace\bge\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ bge --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:\java\workspace\bge\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ bge --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory D:\java\workspace\bge\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ bge --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:\java\workspace\bge\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ bge --- [INFO] Surefire report directory: D:\java\workspace\bge\target\surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.hongxing.bge.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.01 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ bge --- [INFO] Building jar: D:\java\workspace\bge\target\bge-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ bge --- [INFO] Installing D:\java\workspace\bge\target\bge-0.0.1-SNAPSHOT.jar to D:\java\localRepository\com\hongxing\bge\0.0.1-SNAPSHOT\bge-0.0.1-SNAPSHOT.jar [INFO] Installing D:\java\workspace\bge\pom.xml to D:\java\localRepository\com\hongxing\bge\0.0.1-SNAPSHOT\bge-0.0.1-SNAPSHOT.pom [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building nange 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ nange --- [INFO] Deleting D:\java\workspace\nange\target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ nange --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory D:\java\workspace\nange\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ nange --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:\java\workspace\nange\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ nange --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory D:\java\workspace\nange\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ nange --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:\java\workspace\nange\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ nange --- [INFO] Surefire report directory: D:\java\workspace\nange\target\surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.hongxing.nange.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.013 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ nange --- [INFO] Building jar: D:\java\workspace\nange\target\nange-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ nange --- [INFO] Installing D:\java\workspace\nange\target\nange-0.0.1-SNAPSHOT.jar to D:\java\localRepository\com\hongxing\nange\0.0.1-SNAPSHOT\nange-0.0.1-SNAPSHOT.jar [INFO] Installing D:\java\workspace\nange\pom.xml to D:\java\localRepository\com\hongxing\nange\0.0.1-SNAPSHOT\nange-0.0.1-SNAPSHOT.pom [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building shanji 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ shanji --- [INFO] Deleting D:\java\workspace\shanji\target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ shanji --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory D:\java\workspace\shanji\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ shanji --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:\java\workspace\shanji\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ shanji --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory D:\java\workspace\shanji\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ shanji --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:\java\workspace\shanji\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ shanji --- [INFO] Surefire report directory: D:\java\workspace\shanji\target\surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.hongxing.shanji.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ shanji --- [INFO] Building jar: D:\java\workspace\shanji\target\shanji-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ shanji --- [INFO] Installing D:\java\workspace\shanji\target\shanji-0.0.1-SNAPSHOT.jar to D:\java\localRepository\com\hongxing\shanji\0.0.1-SNAPSHOT\shanji-0.0.1-SNAPSHOT.jar [INFO] Installing D:\java\workspace\shanji\pom.xml to D:\java\localRepository\com\hongxing\shanji\0.0.1-SNAPSHOT\shanji-0.0.1-SNAPSHOT.pom [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building aggreation 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ aggreation --- [INFO] Deleting D:\java\workspace\aggreation\target [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ aggreation --- [INFO] Installing D:\java\workspace\aggreation\pom.xml to D:\java\localRepository\com\hongxing\aggreation\0.0.1-SNAPSHOT\aggreation-0.0.1-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary: [INFO] [INFO] bge ................................................ SUCCESS [ 6.069 s] [INFO] nange .............................................. SUCCESS [ 0.670 s] [INFO] shanji ............................................. SUCCESS [ 0.494 s] [INFO] aggreation ......................................... SUCCESS [ 0.019 s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 7.419 s [INFO] Finished at: 2017-06-29T15:59:41+08:00 [INFO] Final Memory: 19M/139M [INFO] ------------------------------------------------------------------------
继承:多次使用到的依赖,比如:单元测试,没有必要在所有的项目中都引用一下,此时就可以采用继承的方式来实现,先来一个父级的POM.XML然后再继承此POM.XML。
1. packaging 改为pom。
2. dependencyManagement 中并不提取依赖,只进行统一管理。
3. property中定义了junit版本号,之后可以引用。
4. 父类parent中的main和test没有意义,可以删除。
父类pom:
<properties>
<junit.version>3.8.1</junit.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
子类pom:
<parent>
<groupId>com.hongxin</groupId>
<artifactId>hongxin-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------