Maven學(xué)習(xí)筆記

自動建立目錄骨架

Maven創(chuàng)建目錄的兩種方式:
1.archetype:generate 按照提示進行選擇。
2.archetype:generate -DgourpId=組織名,公司網(wǎng)址的反寫+項目名稱
-DartifactId=項目名-模塊名
-Dversion=版本號
-Dpackage=代碼所在的包名
實例:
mvn archetype:generate -DgroupId=com.imooc.maven04 -DartifactId=maven04-demo -Dversion=1.0.0-SNAPSHOT -Dpackage=com.imooc.maven04-demo

Maven中的坐標(biāo)和倉庫

http://www.mvnrepository.com/ 這個是官網(wǎng)
1、坐標(biāo)。
2、構(gòu)件通過坐標(biāo)作為其唯一標(biāo)識。
3、倉庫:
-本地倉庫。
-遠程倉庫:maven-model-builder-3.3.3.jar\org\apache\maven\model,超級pom.xml文件,全球中央倉庫地址:https://repo.maven.apache.org/maven2
-鏡像倉庫:/conf/setting文件:

<mirror>
<id>maven.net.cn</id>
<mirrorOf>central</mirrorOf>
<name>central mirror in china</name>
<url>http://maven.net.cn/content/groups/public</url>
</mirror>

4、修改本地倉庫的位置:<localRepository>D:/Maven/repository</localRepository>
Ps:一旦配置了鏡像倉庫,那么先前的所有配置都會轉(zhuǎn)到鏡像倉庫中。
默認(rèn)倉庫位置.m2 repository,將settings.xml復(fù)制到更改后的本地倉庫中,這樣maven版本更新了,也不用修改settings.xml了

Maven的生命周期

clean 清理項目:又分為三個階段(pre-clean執(zhí)行清理前的工作、clean清理上一次構(gòu)建生成的所有文件、post-clean執(zhí)行清理后的文件)
default 構(gòu)建項目(最核心):compile、test、package、install都屬于default
site 生成項目站點(根據(jù)POM的信息自動生成站點):pre-site 在生成項目站點前要完成的工作、site 生成項目的站點文檔、
post-site 在生成項目站點后要完成的工作、site-deploy 發(fā)布生成的站點到服務(wù)器上
如下:使用source插件將項目的源碼打包,使用命令 clean package

項目管理利器(Maven)——Pom.xml解析

<name>項目的描述名</name> 
<url>項目的地址</url> 
<description>項目描述</description> 
<developers>開發(fā)人員信息</developers> 
<licenses>許可證信息</licenses> 

<!-- 依賴列表 -->
<dependencies>
<!-- 依賴項 -->
<dependency>
<groupId></groupId>
<artifactId></artifactId>
<version></version>
<type></type>
<scope>依賴的范圍</scope>
<optional>設(shè)置依賴是否可選,默認(rèn)是false</optional>
<!-- 排除依賴傳遞列表 -->
<exclusions>
<exclusion></exclusion>
</exclusions>
</dependency>
</dependencies>

<!-- 依賴的管理,一般定義在父模塊中,由子模塊去繼承 -->
<dependencyManagement>
<dependencies>
<dependency></dependency>
</dependencies>
</dependencyManagement>

<!-- 對構(gòu)建行為提供相應(yīng)的支持 -->
<build>
<!-- 插件列表 -->
<plugins>
<plugin>
<!-- 指定坐標(biāo) -->
<groupId></groupId>
<artifactId></artifactId>
<version></version>
</plugin>
</plugins>
</build>

<!-- 一般在子模塊中指定所繼承的父模塊 -->
<parent></parent>

<!-- 模塊列表 -->
<modules>
<module></module>
</modules>

Maven 配置web項目

1:打開Eclipse
2:創(chuàng)建Maven項目,選擇archetype-webapp模板
3:解決Servlet沒有生命依賴的問題,在pom.xml中添加servlet的依賴,可以去中央倉庫選擇servlet版本
例如:

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0-b01</version>
</dependency>

4:手工完善Maven約定好的目錄結(jié)構(gòu)
可以在navigator窗口中手動新建
../src/main/java
../src/test/java
5:Project Explorer中更新項目
6:創(chuàng)建項目的輸出目錄
build path中設(shè)置輸出路徑output folder,確保每個module都輸出在../tartet/classes目錄中
7:將Maven項目轉(zhuǎn)換為Web項目
右擊-屬性-project facets-勾選動態(tài)模板(Dynamic Web Module)
8:修改部署時配置去掉多余的配置項例如:測試代碼
右擊-屬性-Deployment Assembly-刪除多余的配置項(測試代碼)
9:使用package命令打包項目—使用jetty這個插件(在Maven的中央插件庫中尋找)作為Web容器,在中央倉庫查找jetty的plugin坐標(biāo)配置到pom.xml中
pom.xml例子:

<build>
<finalName>webbproject-demo</finalName>
<plugins>
<plugin>
        <groupId>org.apache.tomcat.maven</groupId> 
        <artifactId>tomcat7-maven-plugin</artifactId> 
        <version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容