Documentation http://maven.apache.org/guides/index.htmlhtml
-----3-1 經常使用構建命令------------------------------------------------------------------------------------------------------java
mvnreact
-vspring
compileapache
test 測試windows
package 打包服務器
clean 刪除targetapp
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的類eclipse
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>
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------