国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

maven 標(biāo)準(zhǔn)目錄介紹及使用

2019-11-06 09:23:36
字體:
供稿:網(wǎng)友

創(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è)過程會(huì)有點(diǎn)慢,maven需要下載一些依賴項(xiàng),中間如果有輸入提示信息,直接回車使用默認(rèn)值就可以了。這條命令執(zhí)行完后,會(huì)在你的當(dāng)前目錄下生成一個(gè)名為myapp的目錄:

注意這個(gè)目錄結(jié)構(gòu),src/main/java 和 src/test/java 是不能改動(dòng),不然maven會(huì)無法找到源文件。下面是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è)共同的標(biāo)準(zhǔn)目錄結(jié)構(gòu),使開發(fā)人員能在熟悉了一個(gè)Maven工程后,對(duì)其他的Maven工程也能清晰了解。這樣做也省去了很多設(shè)置的麻煩。

以上的文檔介紹是Maven希望的目錄結(jié)構(gòu),并且也是目錄創(chuàng)建工程是采用的目錄結(jié)構(gòu)。Maven推薦大家盡可能的遵守這樣的目錄結(jié)構(gòu)。

在頂級(jí)目錄上是工程的描述文件pom.xml(如果使用Ant則還包括其他屬性文件,maven.xml或build.xml),另外還包括提供給最終用戶的文件,如,README.txt,LICENSE.txt等等。

頂級(jí)目錄還包括兩個(gè)子目錄:src,target。頂級(jí)目錄下可能出現(xiàn)的其他目錄僅僅是CVS或.svn和其他多模塊工程的工程目錄,最好不要再有其他目錄。

Target目錄是所有工程編譯構(gòu)建的輸出目錄。

Src目錄包含所有工程的源碼文件,配置文件,資源文件等等。它下面的子目錄一般包含main(主要的工程源文件),test(測試文件),site(項(xiàng)目站點(diǎn)文件)。

標(biāo)準(zhǔn)目錄結(jié)構(gòu):

src

  -main

      –bin 腳本庫

      –java java源代碼文件

      –resources 資源庫,會(huì)自動(dòng)復(fù)制到classes目錄里

      –filters 資源過濾文件

      –assembly 組件的描述配置(如何打包)

      –config 配置文件

      –webapp web應(yīng)用的目錄。WEB-INF、CSS、js等

  -test

      –java 單元測試java源代碼文件

      –resources 測試需要用的資源庫

      –filters 測試資源過濾庫

  -site Site(一些文檔)

target

LICENSE.txt Project’s license

README.txt Project’s readme

工程根目錄下就只有src和target兩個(gè)目錄

target是有存放項(xiàng)目構(gòu)建后的文件和目錄,jar包、war包、編譯的class文件等。

target里的所有內(nèi)容都是maven構(gòu)建的時(shí)候生成的

maven默認(rèn)生成了一個(gè)重要的文件pom.xml,maven就是通過這個(gè)文件來來管理整個(gè)project,可以理解位類似于eclipse的.project文件。默認(rèn)生成的pom.xml文件的內(nèi)容如下:

/* 1-1 */<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.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)識(shí),一般是com.company.xxx這種格式artifactId:  這個(gè)project最后所生成的文檔(jar、war)的名字,比如對(duì)于junit這個(gè)開源的project,它的artifactId就是junitpackaging: 這個(gè)project的打包的類型,一般是war、jar等值version: project的版本name: project的名字,生成文檔等內(nèi)容的時(shí)候會(huì)用的這個(gè)名字

這個(gè)project創(chuàng)建好后和普通的project沒有什么不同,我們直接往里面放源代碼進(jìn)行開發(fā)就可以了,如果有目錄想修改的也完全可以。

POM & archetype

archetype就是一個(gè)project的模板,上面我們生成的project就是用默認(rèn)的archetype生成的。如果使用不同的archetype,生成的project結(jié)構(gòu)會(huì)有所不同。 一個(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è)文件對(duì)于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會(huì)創(chuàng)建一個(gè)target目錄去保存編譯結(jié)果。 我們需要編譯成一個(gè)什么樣的內(nèi)容,以及要輸出到什么地方等等,都是可以在pom.xml文件里面配置的,但是因?yàn)槲覀兡壳安]有指定這些內(nèi)容,所以maven會(huì)使用默認(rèn)值。

我們還可以用maven執(zhí)行test:

mvn test

第一次執(zhí)行時(shí),maven會(huì)去下載一些依賴項(xiàng)。另外要注意的時(shí),如果我們更改了默認(rèn)的目錄結(jié)構(gòu),maven會(huì)因?yàn)檎襜u到test而無法去執(zhí)行test。如果只需要編譯test可以執(zhí)行:

mvn test-compile

要把項(xiàng)目打包,執(zhí)行:

mvn package

mvn會(huì)根據(jù)pom.xml里面的packaging選項(xiàng)打包成相應(yīng)的文件。

 

repository & dependency

maven里面有一個(gè)repository的概念,當(dāng)我們的項(xiàng)目依賴于某個(gè)jar時(shí),maven會(huì)去repository里面去找。repository分兩種,一種是遠(yuǎn)程的,一種是本地的。如果有幾個(gè)project都用到j(luò)unit,我們可以把junit放在repository里面,幾個(gè)project可以公用,節(jié)約存儲(chǔ)空間而且方便管理,這個(gè)repository的位置可以在pom.xml里面設(shè)置。

本地的默認(rèn)的路徑是安裝用戶的目錄下的 .m2/repository 文件夾。如果一個(gè)依賴項(xiàng)在本地的repository里面沒有,那么maven會(huì)去他自己的遠(yuǎn)程的repositoryhttp://repo.maven.apache.org/maven2 去下載后放到本地的repository里面。

也就是說,我們?nèi)绻覀兊膒roject需要要引用一個(gè)依賴項(xiàng),我們只需要在pom.xml文件中進(jìn)行配置,maven會(huì)自動(dòng)幫我們?nèi)ヒ谩?我們之前的創(chuàng)建project里面需要寫單元測試,引用到了junit,看pom中的配置:

<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)去后會(huì)有一個(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 directoryprocess-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 也會(huì)被執(zhí)行。例如如果執(zhí)行:

mvn deploy

前面的install、verify一直到validate這些build phase都會(huì)執(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

上面的命令會(huì)導(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就會(huì)被執(zhí)行多次。

phase的順序是已經(jīng)固定的,如果一個(gè)phase沒有綁定到任何goal,那么phase就不會(huì)被執(zhí)行。 一個(gè)goal可以通過兩種方式綁定到一個(gè)phase,一個(gè)是指定packaging,另一個(gè)就是plugin。

packaging&plugin

plugin就是用來向maven提供goal的。一個(gè)plugin里面可以有多個(gè)goal,這就是為什么我們?cè)谥该鱣oal時(shí),前面會(huì)用一個(gè)冒號(hào)與plugin的名字。

一個(gè)plugin自己可以指定自己的goal綁定到哪個(gè)lifecycle的哪一個(gè)Phase上,另外也可以配置一個(gè)goal綁定到哪個(gè)phase上。可以在pom.xml里面配置。 看兩個(gè)配置:

<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會(huì)根據(jù)自己的配置去綁定到一個(gè)phase,在phase執(zhí)行的時(shí)候這個(gè)goal會(huì)執(zhí)行。并且在這個(gè)配置里面,可以指定多個(gè)execution來讓這個(gè)goal執(zhí)行多次。

看另一個(gè)示例配置:

<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-resources

resources:resources

compile

compiler:compile

process-test-resources

resources:testResources

test-compile

compiler:testCompile

test

surefire:test

package

jar:jar

install

install:install

deploy

deploy:deploy

 

總結(jié)

首先搞清楚maven的project的目錄結(jié)構(gòu),然后理解maven的lifecycle,lifecycle是由build phase組成,每一個(gè)build phase會(huì)綁定到goal。goal是由plugin提供的。 每一種packaging的值都表明了一定的phase和goal之間的綁定關(guān)系。

另外一個(gè)很重要的就是dependency,我們要在項(xiàng)目中引用一個(gè)依賴,只需要在pom.xml指定依賴的名字和版本,maven會(huì)自動(dòng)去遠(yuǎn)程的repository下載,然后放到本地的repository里面,這樣以后所有的project都可以共用

其它細(xì)節(jié)可以參考http://maven.apache.org/guides/index.html。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 汤原县| 砚山县| 明光市| 信阳市| 扶绥县| 莱州市| 太仓市| 五家渠市| 乌鲁木齐县| 城步| 万山特区| 遂平县| 平遥县| 安吉县| 墨竹工卡县| 云浮市| 恭城| 广丰县| 包头市| 炉霍县| 上蔡县| 揭西县| 双桥区| 太康县| 黑河市| 深水埗区| 新竹县| 周口市| 玉田县| 芒康县| 溧水县| 贡觉县| 泰宁县| 古蔺县| 威宁| 台东市| 文昌市| 明溪县| 盖州市| 永宁县| 苗栗县|