都說Maven好,以前一直用ant,這次體驗一下.
開始之前,maven給我的印象有2個,一是庫依賴管理做得比較好,二是規范了構建編譯過程,說白了就是什么目錄都規定好了.
好開始安裝,解壓縮,設置m2_home, mave_opts,path環境變量,在命令行執行"mvn help:describe -Dplugin=help"...
[WARNING] Failed to retrieve plugin descriptor for org.apache.maven.plugins:maven-clean-plugin:2.5: ...
...
[ERROR] No plugin found for prefix 'help' in the current project and in the plugin groups [org.apache.maven.plugins, org
.codehaus.mojo] available from the repositories [local (D:\tmp\mvn-test), central (https://repo.maven.apache.org/maven2)
] -> [Help 1]
咦,出錯了, 根據錯誤信息執行"mvn help:describe -Dplugin=help -e -X"
Caused by: org.eclipse.aether.transfer.ArtifactTransferException: Could not transfer artifact org.apache.maven.plugins:m
aven-install-plugin:pom:2.4 from/to central (https://repo.maven.apache.org/maven2): Connect to repo.maven.apache.org:443
[repo.maven.apache.org/185.31.17.215] failed: Connection timed out: connect
網絡連接有問題,看來應該設置代理,直接修改conf/setting.xml, 把代理加上去.
運行,咦,又tm出錯了, 錯誤信息是(注意黑斜體):
[WARNING] Failure to transfer org.codehaus.mojo/maven-metadata.xml from https://repo.maven.apache.org/maven2 was cached
in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates
are forced. Original error: Could not transfer metadata org.codehaus.mojo/maven-metadata.xml from/to central (https://re
po.maven.apache.org/maven2): Not authorized by proxy , ReasonPhrase:Proxy Authentication Required.
org.eclipse.aether.transfer.MetadataTransferException: Failure to transfer org.codehaus.mojo/maven-metadata.xml from htt
ps://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the updat
e interval of central has elapsed or updates are forced. Original error: Could not transfer metadata org.codehaus.mojo/m
aven-metadata.xml from/to central (https://repo.maven.apache.org/maven2): Not authorized by proxy , ReasonPhrase:Proxy A
uthentication Required.
這如何是好,明明不需要密碼的, baidu一把, oh,原來有人碰到類似情況, 如果是NTLM proxy(搞不清楚這個代理類型,可能是微軟的東東吧),需要添加一個附加包,如下:
Download the wagon-http-lightweight-2.2.jar , copy到lib\ext目錄下, 問題解決!
接下來看看幫助信息(很重要):
mvn help:help -Ddetail=true <-查看help
mvn help:describe -Dplugin=help <-查看help插件
mvn help:describe -Dplugin=help -Dgoal=system -Ddetail <-查看help插件下的system goal詳細信息。
mvn help:describe -Dcmd=jboss-as:deploy -Ddetail=true <-一個例子, 文檔中說要執行 mvn jboss-as:deploy, 這個命令將查看 jboss-as:deploy的幫助
好,寫了一個HelloWorld.java放到src\main\java中,注意目錄結構
\src\ \src\main\java\ \pom.xml
pom.xml
<project> <modelVersion>4.0.0</modelVersion> <groupId>bjfarmer</groupId> <artifactId>MyTest</artifactId> <version>1.0</version> </project>
不知道為何modelVersion必須是4.0.0,否則報錯,這個留在后面找答案,運行"mvn clean install",繼續。
這時候mvn又開始下載插件(resource,compiler,surefire...),額地神,到處都是插件,看來離開了插件不能活啊。可惡的代理,時斷時續,我不得不重復執行clean install.總算可以了。這時候發現build后發現多了一個target目錄,如下:
\src\ \src\main\java\ \target\ \target\classes\ <-編譯的class \target\maven-archiver\ <-打包的參數設置 \target\maven-status\ <-狀態,里面只有一個文件夾 \target\maven-status\maven-compiler-plugin\ <-里面記錄了要編譯的源文件清單,目標類文件清單等 \target\MyTest-1.0.jar <-最終生成的jar包 \pom.xml
那么現在問題來了,這個編譯到底用了哪個版本的JDK呢?這樣的黑盒也太黑了,不行我得搞清楚。
不看不知道,一看嚇一跳,將包MyTest-1.0.jar解開,然后用16進制查看5~6(minor version),7~8字節(major version), 十進制為49.0, 對應於JRE為1.5。注:如果您還不知道如何查看一個class是從那個版本JDK編譯來的,請閱讀JVM Specification Chapter4,或者用JDK自帶工具 javap -verbose <class file>
查看插件compiler的幫助,看官可能有些疑問,這么高級的命令你怎么一開始就知道?其實不是,就是學習了基本的help命令之后,稍微理解了插件是由很多goal組成,而每個goal都有些parameter。執行下面命令:
mvn help:describe -Dplugin=compiler -Dgoal=compile -Ddetail=true
...
target (Default: 1.5)
User property: maven.compiler.target
The -target argument for the Java compiler.
....
果真,不制定版本,默認真是1.5,這真是領教了“convention over configuration”,執行下面的命令,
mvn clean install -Dmaven.compiler.target=1.7
發現class文件major version變成了51,-;
到現在為止已經可以build一個簡單項目了,還缺少Junit test,再努力一把,加入如下的dependency, 把Junit 測試代碼放到\src\test\java\下, 重新運行mvn clean install, ok!
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> </dependency> </dependencies>
當然用mvn默認的項目創建更好,譬如:
mvn archetype:create -DgroupId=<group> -DartifactId=<name> -DpackageName=com.zproject
草,失敗了,看了一下幫助(mvn help:describe -Dplugin=archetype -Ddetail=true),這個goal deprecated, 建議用generate
mvn archetype:generate -DgroupId=<group> -DartifactId=<name> -DpackageName=com.zproject
終於成了,注意到默認源碼目錄中的package不是com.zproject, 而是groupId。有些意思。
好了回顧一下,重新領略一下maven的一些重要的概念,這片算是完結了。
- Convention over Configuration (約定優於配置), 黑盒子,不用自己定義了,方便的同時帶來了麻煩。
- 依賴管理,確實方便,根據坐標(groupId:artifactId:version:package,如junit:junit:3.8.1:jar)直接尋找,不用自己管理。
- 生命周期,預定義了軟件生命周期,resources, compile, testResources,testCompile, test, jar, 執行這些生命周期等同於相應的任務組合。
- 插件,插件,插件由goal組成,譬如 mvn compiler:compile,這個就是執行一個插件的goal。
- 還有2個重要命令: mvn dependency:tree mvn dependency:resolve
- Profile, 上文中沒有提到,啥東西? 先記下這個命令: mvn help:active-profiles mvn clean install -P<profile-name> 構建移植,嗯,profile就是干這個的. 譬如開發環境的部署與生產環境的部署.
over,有機會寫一篇高級技巧。
