国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Maven 使用介紹
創(chuàng)建project
先去官方網(wǎng)站下載一個(gè)最新版本http://maven.apache.org/download.cgi. 下載后解壓,使用之前最好先將maven的bin目錄設(shè)置到path環(huán)境變量里面。
maven無非也就是用來build一個(gè)project的,直接先上一個(gè)例子,在命令行下輸入下面的命令:
mvn archetype:generate DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.mycompany.app -DartifactId=myapp
mvn就是maven的命令行程序,archetype:generate中的archetype是plugin的名字,generate是goal的名字,命令行后面的是一些參數(shù)。關(guān)于archetype和goal以及后面的參數(shù),后面再細(xì)說。
如果是第一次運(yùn)行,這個(gè)過程會有點(diǎn)慢,maven需要下載一些依賴項(xiàng),中間如果有輸入提示信息,直接回車使用默認(rèn)值就可以了。這條命令執(zhí)行完后,會在你的當(dāng)前目錄下生成一個(gè)名為myapp的目錄:
注意這個(gè)目錄結(jié)構(gòu),src/main/java 和 src/test/java 是不能改動,不然maven會無法找到源文件。下面是maven一個(gè)標(biāo)準(zhǔn)的目錄結(jié)構(gòu):
src/main/java Application/Library sources
src/main/resources Application/Library resources
src/main/filters Resource filter files
src/main/assembly Assembly descriptors
src/main/config Configuration files
src/main/scripts Application/Library scripts
src/main/webapp Web application sources
src/test/java Test sources
src/test/resources Test resources
src/test/filters Test resource filter files
src/site Site
另外maven還生成了一個(gè)重要的文件pom.xml,maven就是通過這個(gè)文件來來管理整個(gè)project,可以理解位類似于eclipse的.project文件。默認(rèn)生成的pom.xml文件的內(nèi)容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* 1-1 */
<project xmlns="
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.1.0.1</version>
<packaging>jar</packaging>
<name>myapp</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>
解釋一下這個(gè)xml文件的內(nèi)容:
modelVersion: 這個(gè)XML文件所使用的POM格式的版本
groupId: 相當(dāng)于這個(gè)project的所有者或者機(jī)構(gòu)的一個(gè)標(biāo)識,一般是com.company.xxx這種格式
artifactId:  這個(gè)project最后所生成的文檔(jar、war)的名字,比如對于junit這個(gè)開源的project,它的artifactId就是junit
packaging: 這個(gè)project的打包的類型,一般是war、jar等值
version: project的版本
name: project的名字,生成文檔等內(nèi)容的時(shí)候會用的這個(gè)名字
這個(gè)project創(chuàng)建好后和普通的project沒有什么不同,我們直接往里面放源代碼進(jìn)行開發(fā)就可以了,如果有目錄想修改的也完全可以。
POM & archetype
archetype就是一個(gè)project的模板,上面我們生成的project就是用默認(rèn)的archetype生成的。如果使用不同的archetype,生成的project結(jié)構(gòu)會有所不同。 一個(gè)archetype指明了
1) 項(xiàng)目的目錄結(jié)構(gòu)以及什么目錄是放source code,哪些是放test source code,哪些目錄是放resource的。
2) 一個(gè)默認(rèn)的pom.xml文件,這個(gè)默認(rèn)的pom.xml文件中的groupId,artifactId,version用占位符表示,在創(chuàng)建project的時(shí)候通過參數(shù)傳進(jìn)來。
pom.xml文件的POM全稱是Project Object Model,這個(gè)文件對于maven的使用者來說是一個(gè)和maven交互的渠道,pom.xml包含了一個(gè)maven project的配置,一個(gè)project該如何編譯打包,project有哪些依賴項(xiàng)等等。
仔細(xì)看一下我們前面創(chuàng)建project的命令:mvn archetype:generate DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.mycompany.app -DartifactId=myapp
1) archetype:generate, 這是一個(gè)maven的plugin,用來從一個(gè)archetype創(chuàng)建一個(gè)project,關(guān)于plugin后面再說。
2) DarchetypeGroupId,這個(gè)就是指的archetype的groupid,也就是說是用的哪個(gè)archetype,或者說用哪個(gè)項(xiàng)目模板。
3) 后面的兩個(gè)參數(shù),用來放大pom.xml文件里面,作為當(dāng)前創(chuàng)建的project的描述信息。
Project創(chuàng)建好了,看如何去編譯,直接進(jìn)入的project的目錄,在命令行下:
mvn compile
編譯完后maven會創(chuàng)建一個(gè)target目錄去保存編譯結(jié)果。 我們需要編譯成一個(gè)什么樣的內(nèi)容,以及要輸出到什么地方等等,都是可以在pom.xml文件里面配置的,但是因?yàn)槲覀兡壳安]有指定這些內(nèi)容,所以maven會使用默認(rèn)值。
我們還可以用maven執(zhí)行test:
mvn test
第一次執(zhí)行時(shí),maven會去下載一些依賴項(xiàng)。另外要注意的時(shí),如果我們更改了默認(rèn)的目錄結(jié)構(gòu),maven會因?yàn)檎襜u到test而無法去執(zhí)行test。如果只需要編譯test可以執(zhí)行:
mvn test-compile
要把項(xiàng)目打包,執(zhí)行:
mvn package
mvn會根據(jù)pom.xml里面的packaging選項(xiàng)打包成相應(yīng)的文件。
repository & dependency
maven里面有一個(gè)repository的概念,當(dāng)我們的項(xiàng)目依賴于某個(gè)jar時(shí),maven會去repository里面去找。repository分兩種,一種是遠(yuǎn)程的,一種是本地的。如果有幾個(gè)project都用到j(luò)unit,我們可以把junit放在repository里面,幾個(gè)project可以公用,節(jié)約存儲空間而且方便管理,這個(gè)repository的位置可以在pom.xml里面設(shè)置。
本地的默認(rèn)的路徑是安裝用戶的目錄下的 .m2\repository 文件夾。如果一個(gè)依賴項(xiàng)在本地的repository里面沒有,那么maven會去他自己的遠(yuǎn)程的repositoryhttp://repo.maven.apache.org/maven2 去下載后放到本地的repository里面。
也就是說,我們?nèi)绻覀兊膒roject需要要引用一個(gè)依賴項(xiàng),我們只需要在pom.xml文件中進(jìn)行配置,maven會自動幫我們?nèi)ヒ谩?我們之前的創(chuàng)建project里面需要寫單元測試,引用到了junit,看pom中的配置:
1
2
3
4
5
6
7
8
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
每一個(gè)需要為每一個(gè) dependency 指明groupId,artifactId,version。scope很簡單,意思是說我們需要怎么引用,比如我們上面的例子里面設(shè)置的是test,意思是說只在test里面引用junit。 但是我們?nèi)绾沃纆roupId,artifactId和version呢? 比如我現(xiàn)在想引用log4j,這個(gè)幾個(gè)值怎么填? 可以去http://mirrors.ibiblio.org/maven2/ 上去查找。比如log4j,我們就在上面這個(gè)地址加上log4j,也就是http://mirrors.ibiblio.org/maven2/junit/。進(jìn)去后會有一個(gè)maven-metadata.xml,打開就可以知道這些值了然后添加這個(gè)dependency了。
如果要把一個(gè)project安裝到本地的repository里面,可以執(zhí)行下面的命令:
mvn install
到這里就說完了創(chuàng)建,編譯,測試,打包以及安裝,大部分的項(xiàng)目也就是做這些事情。
再介紹幾個(gè)其它命令:
mvn site : 為你的project創(chuàng)建一個(gè)站點(diǎn)
mvn clean: 清除target目錄下的所有文件
mvn eclipse:eclipse :為project生成eclipse的工程文件和classpath文件
build lifecycle & build phase & goal
maven有一套build的生命周期,是按照一套順序走下來的,這一套順序就叫一個(gè)生命周期(lifecycle)。maven內(nèi)置三種生命周期:default, clean 和 site。一個(gè)生命周期分為多個(gè)build phase,下面是default生命周期全部的build phase:
validate:validate the project is correct and all necessary information is available.
initialize:initialize build state, e.g. set properties or create directories.
generate-sources:generate any source code for inclusion in compilation.
process-sources:process the source code, for example to filter any values.
generate-resources:generate resources for inclusion in the package.
process-resources:copy and process the resources into the destination directory, ready for packaging.
compile:compile the source code of the project.
process-classes:post-process the generated files from compilation, for example to do bytecode enhancement on Java classes.
generate-test-sources:generate any test source code for inclusion in compilation.
process-test-sources:process the test source code, for example to filter any values.
generate-test-resources:create resources for testing.
process-test-resources:copy and process the resources into the test destination directory.
test-compile:compile the test source code into the test destination directory
process-test-classes:post-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above.
test:run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
prepare-package:perform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. (Maven 2.1 and above)
package:take the compiled code and package it in its distributable format, such as a JAR.
pre-integration-test:perform actions required before integration tests are executed. This may involve things such as setting up the required environment.
integration-test:process and deploy the package if necessary into an environment where integration tests can be run.
post-integration-test:perform actions required after integration tests have been executed. This may including cleaning up the environment.
verify:run any checks to verify the package is valid and meets quality criteria.
install:install the package into the local repository, for use as a dependency in other projects locally.
deploy:done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
這些build phase是按照順序執(zhí)行的,如果執(zhí)行后面的build phase,前面的build phase 也會被執(zhí)行。例如如果執(zhí)行:
mvn deploy
前面的install、verify一直到validate這些build phase都會執(zhí)行。
每一個(gè)build phase是由goal組成的,一個(gè)goal其實(shí)就是一個(gè)任務(wù),一個(gè)goal可以關(guān)聯(lián)到一個(gè)build phase也可以不關(guān)聯(lián)到任何build phase 。 不關(guān)聯(lián)到任何phase的goal是可以獨(dú)立執(zhí)行的,例如:
mvn clean dependency:copy-dependencies package
上面的命令會導(dǎo)致先執(zhí)行clean這個(gè)phase,然后拷貝依賴項(xiàng),最后打包。注意,這里clean這個(gè)goal是clean這個(gè)lifecycle里面的一個(gè)goal,所以可以看到不同lifecycle的build phase和goal是可以混合在一起執(zhí)行的。 如果一個(gè)goal被綁定到多個(gè)phase上,那么goal就會被執(zhí)行多次。
phase的順序是已經(jīng)固定的,如果一個(gè)phase沒有綁定到任何goal,那么phase就不會被執(zhí)行。 一個(gè)goal可以通過兩種方式綁定到一個(gè)phase,一個(gè)是指定packaging,另一個(gè)就是plugin。
packaging&plugin
plugin就是用來向maven提供goal的。一個(gè)plugin里面可以有多個(gè)goal,這就是為什么我們在指明goal時(shí),前面會用一個(gè)冒號與plugin的名字。
一個(gè)plugin自己可以指定自己的goal綁定到哪個(gè)lifecycle的哪一個(gè)Phase上,另外也可以配置一個(gè)goal綁定到哪個(gè)phase上??梢栽趐om.xml里面配置。 看兩個(gè)配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<plugin>
<groupId>org.codehaus.modello</groupId>
<artifactId>modello-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<configuration>
<models>
<model>src/main/mdo/maven.mdo</model>
</models>
<version>4.0.0</version>
</configuration>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
</plugin>
這個(gè)就在當(dāng)前的lifecycle里面添加了一個(gè)名字叫java的goal,這goal會根據(jù)自己的配置去綁定到一個(gè)phase,在phase執(zhí)行的時(shí)候這個(gè)goal會執(zhí)行。并且在這個(gè)配置里面,可以指定多個(gè)execution來讓這個(gè)goal執(zhí)行多次。
看另一個(gè)示例配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
<plugin>
<groupId>com.mycompany.example</groupId>
<artifactId>display-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>process-test-resources</phase>
<goals>
<goal>time</goal>
</goals>
</execution>
</executions>
</plugin>
這個(gè)名為time的goal把自己綁定到了process-test-resource這個(gè)phase上。
在默認(rèn)情況下,并不是所有的phase都綁定了goal,比如clean這個(gè)lifecycle是有三個(gè)phase的,但是只有其中的一個(gè)名為clean的phase默認(rèn)綁定了一個(gè)clean:clean goal,其它兩個(gè)phase默認(rèn)沒有綁定任何goal。
之前已經(jīng)提到過packaging,在pom.xml可以指定packaging,每種packaging都設(shè)定了一組phase和goal之間的綁定關(guān)系。在default lifecycle下,當(dāng)packaging為 ejb/ejb3/jar/par/rar/war 其中之一的值的時(shí)候,只有以下的phase綁定了goal,具體如下:
process-resourcesresources:resources
compilecompiler:compile
process-test-resourcesresources:testResources
test-compilecompiler:testCompile
testsurefire:test
packagejar:jar
installinstall:install
deploydeploy:deploy
總結(jié)
首先搞清楚maven的project的目錄結(jié)構(gòu),然后理解maven的lifecycle,lifecycle是由build phase組成,每一個(gè)build phase會綁定到goal。goal是由plugin提供的。 每一種packaging的值都表明了一定的phase和goal之間的綁定關(guān)系。
另外一個(gè)很重要的就是dependency,我們要在項(xiàng)目中引用一個(gè)依賴,只需要在pom.xml指定依賴的名字和版本,maven會自動去遠(yuǎn)程的repository下載,然后放到本地的repository里面,這樣以后所有的project都可以共用
其它細(xì)節(jié)可以參考http://maven.apache.org/guides/index.html。
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
徹底理解maven
Maven、Nexus、SVN、Hudson 以及團(tuán)隊(duì)異地開發(fā)那些事(1)
[翻譯]Maven五分鐘入門
Maven編譯代碼的相關(guān)命令
換個(gè)視角看 Maven:一個(gè)領(lǐng)域平臺的優(yōu)美設(shè)計(jì)
maven3常用命令、java項(xiàng)目搭建、web項(xiàng)目搭建詳細(xì)圖解
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服