1、 maven
1.新建一個maven項目,項目後期的打包,編譯,文檔均可以經過maven進行維護html
2.能夠管理項目依賴的jar包
項目團隊中:解決jar文件不統一,爲了維護全公司全部jar環境
---- pom.xml ----> struts 2.3java
Maven是一個項目管理工具,它包含了一個項目對象模型 (Project Object Model ---> pom.xml),
一組標準插件(生命週期各階段是綁定插件中的目標來完成項目構建工做) 編譯,打包,測試
一個項目生命週期(Project Lifecycle),
一個依賴管理系統(Dependency Management System). mysql
2、Maven下載與安裝:
a. maven 官網地址:http://maven.apache.org/程序員
b. 安裝maven:
-- 設置M2_HOME 變量 C:\maven\apache-maven-3.2.5
-- 設置系統環境變量path. C:\maven\apache-maven-3.2.5\bin
建立一個本地倉庫:放下載的jar文件等相關的項目插件。
-- 在C:\maven\apache-maven-3.2.5\conf\settings.xml文件中更改本地倉庫路徑:
<localRepository>C:/maven/apache-maven-3.2.5/repo</localRepository>web
-- 在eclipse中關聯你的maven安裝目錄(相似於關聯tomcat同樣)spring
maven中央庫地址: https://repo1.maven.org/maven2/sql
搜尋jar包網址: http://search.maven.orgapache
c. 配置私服(鏡像)。
在conf/settings.xml文件中給定倉庫下載的鏡像:
-- id : 鏡像惟一標識符,用來區分不一樣的mirror
-- mirrorOf : 鏡像哪些倉庫 *:表明全部的倉庫 central: 表明倉庫id爲central的倉庫.
-- name : 鏡像名稱
-- url : 鏡像的URL
<mirror>
<id>public</id>
<mirrorOf>*</mirrorOf>
<name>public Mirror</name>
<url>http://localhost:8081/nexus/content/groups/public</url>
</mirror>windows
3、Eclipse中Maven插件安裝: m2e插件.api
eclipse中maven3在線安裝地址: http://download.eclipse.org/technology/m2e/releases
Window --> Preferences --> Maven --> User setting 配置關聯自已的Maven.
4、Maven私服搭建(管理jar包依賴的JavaEE Web系統):
企業會以公司爲單位,新建一個maven的私服,這裏面放本公司全部的jar文件
全部的程序員都經過maven鏈接公司的私服進行項目的管理
-- eclipse(maven) ----> 公司的私服下載jar包 ----> 去maven中心庫下載
nexus下載地址:http://www.sonatype.org/nexus/
1. 安裝私服(公司的maven服務器 ,放全公司的jar包,以及項目生命週期中要使用到的各類插件)。
a. 解壓nexus-2.8.0-05-bundle.zip。
nexus-2.8.0-05:核心目錄。
-- bin\jsw: 存放了操做Nexus應用的命令。
-- conf : nexus.properties 配置文件可更改port、host、nexus-work、runtime。
sonatype-work : 工做目錄。
-- sonatype-work\nexus\storage:全部倉庫存儲目錄。
b. 配置啓動nexus應用
nexus-2.10.0-02\bin\jsw\windows-x86-64:
- console-nexus.bat : 控制檯運行nexus
- start-nexus.bat: 啓動nexus服務
- stop-nexus.bat: 中止nexus服務
- install-nexus.bat : 安裝nexus到window服務中
- uninstall-nexus.bat:從window服務中刪除nexus
cmd啓動nexus: sc start nexus-webapp
cmd中止nexus: sc stop nexus-webapp
2. nexus應用使用。
a. 啓動進入Nexus主界面:
- http://localhost:8081/nexus/index.html
b. 登陸Nexus:admin/admin123
c. 點擊左邊菜單Repositories查看全部倉庫:
- Public Repositories:倉庫組。
- 3rd party:第三方的倉庫。
- Maven Central:Maven中央倉庫。
- Releases:發行版存放倉庫。
- Snapshots:快照版存放倉庫。 (測試版本尚未到達最終版本)
- fkjava Central: 自定義的倉庫。
說明:四種類型的倉庫:group(組)、proxy(代理)、hosted(本地)、virtual(虛擬)
d. 建立本身的倉庫。
-- Add按鈕後在configuration選項中進行配置.
5、 Maven的核心概念。
1. Maven 插件和目標 (Plugins and Goals)
-- Maven構建項目由一組標準插件組成。
-- 一個maven插件是由一個或者多個目標組成的。
-- 一個目標是一個明確的任務,它能夠做爲單獨的目標運行,
或者做爲一個大的構建的一部分和其它目標一塊兒運行。
一個目標是Maven中的一個「工做單元」。
目標的例子包括Compiler插件中的compile目標,它用來編譯項目中的全部源文件,
或者Surefire插件中的test目標,用來運行單元測試。
2. Maven 生命週期 (Lifecycle)
-- Clean Lifecycle 在構建項目以前進行一些清理工做。
pre-clean
clean
post-clean
-- Default Lifecycle 構建的核心部分,初始化、編譯、測試、打包、驗證、部署和站點生成等。
validate 驗證項目是否正確,以及全部爲了完整構建必要的信息是否可用
generate-sources 生成全部須要包含在編譯過程當中的源代碼
process-sources 處理源代碼,好比過濾一些值
generate-resources 生成全部須要包含在打包過程當中的資源文件
process-resources 複製並處理資源文件至目標目錄,準備打包
compile 編譯項目的源代碼
process-classes 後處理編譯生成的文件,例如對Java類進行字節碼加強(bytecode enhancement)
generate-test-sources 生成全部包含在測試編譯過程當中的測試源碼
process-test-sources 處理測試源碼,好比過濾一些值
generate-test-resources 生成測試須要的資源文件
process-test-resources 複製並處理測試資源文件至測試目標目錄
test-compile 編譯測試源碼至測試目標目錄
test 使用合適的單元測試框架運行測試。這些測試應該不須要代碼被打包或發佈
prepare-package 在真正的打包以前,執行一些準備打包必要的操做。這一般會產生一個包的展開的處理過的版本(將會在Maven 2.1+中實現)
package 將編譯好的代碼打包成可分發的格式,如JAR,WAR,或者EAR
pre-integration-test 執行一些在集成測試運行以前須要的動做。如創建集成測試須要的環境
integration-test 若是有必要的話,處理包併發布至集成測試能夠運行的環境
post-integration-test 執行一些在集成測試運行以後須要的動做。如清理集成測試環境。
verify 執行全部檢查,驗證包是有效的,符合質量規範
install 安裝包至本地倉庫,以備本地的其它項目做爲依賴使用
deploy 複製最終的包至遠程倉庫,共享給其它開發人員和項目(一般和一次正式的發佈相關)
-- Site Lifecycle 生成項目報告,站點,發佈站點。
pre-site
site
post-site
site-deploy
說明:生命週期各階段Phase由一個或多個插件的Goal目標集合而成。
用mvn命令直接執行生命週期中的指定階段(phase)
mvn phase(階段名稱)
3. Maven 座標 (Coordinates)
座標解釋(groupId、artifactId、version):
groupId: 建立這個項目的組織名稱(通常爲組織的域名反寫)
artifactId: 表示一個單獨項目的惟一標識符(項目名或jar包)
version: 項目的版本號
注意:groupId、artifactId、version它們標識了一個惟一的項目。
4. Maven 資源庫(Repositories)
什麼是Maven資源庫? 資源存儲的地方,即全部項目的jar庫,插件庫。
Maven庫中有三種類型
-- local : 本地庫
<localRepository>C:/MyLocalRepository</localRepository>
-- remote : 遠程倉庫(代理倉庫也就是自已的私服)
-- central : 中央存儲庫(http://repo1.maven.org/maven2/)
Maven的依賴jar包搜索序列:
第一種:本地庫 ---> 中央庫
第二種:本地庫 ---> 遠程庫 ---> 中央庫
5. Maven 依賴管理 (Dependency Management)
a、Transitive Dependency (傳遞依賴)
你須要A jar包, A又依賴B ,你申明瞭A ,B就會自動拉取下來。
你的項目依賴於A,A又依賴於B。你的項目是否要聲明你依賴於B?
Maven的回答是它幫你自動管理這種依賴的傳遞,
你不須要聲明你依賴於B,由Maven來作。
b、Dependency Scope (依賴範圍)
舉例來講,你開發時須要作測試,你須要依賴於junit的jar,可是部署應用時並不須要它,
由於單元測試不會在生產環境上跑,也就是說最終打包的jar或者war不包含junit的jar。
又如你開發web程序,你的servlet/jsp進行編譯須要依賴於servlet-jsp的標準api(J2EE的jar),
可是部署時也是不須要它的,由於你的應用服務器確定有這些東西。
所以,Maven考慮了6中可能的scope供選擇:
- compile: 默認的scope。編譯、測試、打包全都須要。compile參與依賴傳遞,就是說,
你的項目A依賴於B(依賴scope是compile),項目C依賴於你的項目A,那麼C也就依賴於B。
- provided: 表示JDK或者容器會在Runtime時提供這些(jar),
如上面說到的servlet api。provided的東西在編譯和測試時會用到,不參與傳遞依賴。
- runtime: 表示編譯時不須要,但測試和運行時須要,最終打包時會包含進去。
- test: 只用於測試階段(測試的編譯和測試的運行),典型的就是junit的jar。
- system: 和provided相似,但要求jar是你的系統裏已有的,
不會在repository裏找,如rt.jar,tools.jar這些。
- import: 簡單的說,你的項目的pom能夠繼承另外一個項目的pom,
從而繼承了父項目的依賴關係,可是由於以後single inheritance的限制,
因此創造了import,使得你能夠「導入」或者說「繼承」任何一到多個項目的依賴關係。
c. Dependency Classier (依賴jdk版本)
在jdk1.4下編譯的仍是jdk1.5
d. 解決依賴衝突(排除指定的依賴)
<exclusions>
<exclusion>
<artifactId>servlet-api</artifactId>
<groupId>javax.servlet</groupId>
</exclusion>
</exclusions>
6. 站點生成和報告 (Site Generation and Reporting)
-- mvn site 命令
運行site生命週期階段
6、Maven標準插件說明(-D指定屬性):
注意:標準插件的位置(第一次使用時它會去Maven中央庫下載):
Maven本地倉庫\org\apache\maven\plugins\
mvn 插件:goal -D屬性名=屬性值 ……
1. 經過原型建立項目: mvn archetype:create.
-DgroupId=org.fkjava : 設置項目組織名(通常爲公司域名反寫)
-DartifactId=hrm : 設置項目名
-DpackageName : 設置包名
-DarchetypeArtifactId : 設置生成項目所使用的原型(默認爲:maven-archetype-quickstart)
2. 編譯源代碼: mvn compiler:compile.
3. 清除項目:mvn clean:clean (只留下源文件).
4. 測試命令:
-- mvn surefire:test : 運行測試代碼
5. 安裝:mvn install:install
6. 安裝指定文件到本地倉庫:mvn install:install-file
-DgroupId=<groupId> : 設置項目代碼的包名(通常用組織名)
-DartifactId=<artifactId> : 設置項目名或模塊名
-Dversion=1.0.0 : 版本號
-Dpackaging=jar : 什麼類型的文件(jar包)
-Dfile=<myfile.jar> : 指定jar文件路徑與文件名(同目錄只需文件名)
mvn install:install-file -DgroupId=org.springframework -DartifactId=spring-core -Dversion=4.0.2.RELEASE -Dpackaging=jar -Dfile=spring-core-4.0.2.RELEASE.jar
mvn install:install-file -DgroupId=org.directwebremoting -DartifactId=dwr -Dversion=3.0.M1 -Dpackaging=jar -Dfile=dwr.jar
7. 只打jar包: mvn jar:jar
8. 只打war包: mvn war:war
9. 部署最終的包至遠程倉庫: mvn deploy:deploy
10. 生成eclipse項目文件:mvn eclipse:eclipse
11. 清除eclipse的一些系統設置: mvn eclipse:clean
12. 生成站點目錄:mvn site:site
說明:Maven標準插件使用地址: http://maven.apache.org/plugins/index.html
6、Maven建立項目:
第一種方式:經過mvn命令.
-- 建立java項目(經過原型建立項目):
mvn archetype:create -DgroupId=org.fkjava -DartifactId=hrm -DpackageName=org.fkjava.hrm -DarchetypeArtifactId=maven-archetype-quickstart
mvn 插件名:目標 -D參數
-DgroupId : 組織名
-DartifactId: 項目名
-DpackageName : 包名
-DarchetypeArtifactId : 建立項目的原型
-- 建立web項目(經過原型建立項目):
mvn archetype:create -DgroupId=org.fkjava -DartifactId=hrm -DpackageName=org.fkjava.hrm -DarchetypeArtifactId=maven-archetype-webapp
-- 選擇原型目錄建立項目:
mvn archetype:generate -DarchetypeCatalog=internal (58種原型目錄來建立項目)
說明:
-- archetypeCatalog屬性:internal(內部), remote(遠程)
-- 16:表明java項目、19: 表明web項目
第二種方式:經過eclipse選擇原型建立.
maven-archetype-quickstart: java項目
maven-archetype-webapp: 普通的web項目
1. 建立出來的普通的java項目結構:
src/main/java java源文件 源代碼路徑 src
src/main/resources java資源文件 配置文件 好比Struts.xml文件
src/test/java test源文件
src/test/resources test資源文件
7、建立java項目(maven-archetype-quickstart):
1. mvn compile 編譯java項目.
2. mvn test 運行測試用例.
3. mvn package 編譯java項目並打成jar包.
4. mvn clean 清理java項目.
5. mvn install 安裝jar包至本地倉庫.
6. mvn deploy 部署最終jar包至遠程倉庫.
a. 部署到releases與snapshots倉庫:
第一步:在pom.xml文件中配置部署元素
<distributionManagement>
<!-- 發行版本倉庫 -->
<repository>
<id>releases</id>
<name>Nexus Release Repository</name>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<!-- 快照版本倉庫 -->
<snapshotRepository>
<id>snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
第二步:在conf/settings.xml文件中配置server權限元素
<server>
<!-- 這是server的id,該id與distributionManagement
中repository元素的id相匹配 -->
<id>releases</id>
<!-- 登陸名 -->
<username>admin</username>
<!-- 密碼 -->
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
b. 部署到自已的倉庫:
第一步:在pom.xml文件中配置部署元素
<distributionManagement>
<!-- 發行版本倉庫 -->
<repository>
<id>lxh</id>
<name>Nexus lxh Repository</name>
<url>http://localhost:8081/nexus/content/repositories/lxh/</url>
</repository>
</distributionManagement>
第二步:在conf/settings.xml文件中配置server權限元素
<server>
<id>lxh</id>
<username>admin</username>
<password>admin123</password>
</server>
d. 設置jdk版本:
1. 配置編譯插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
2. 更新Maven項目
8、建立Web項目(maven-archetype-webapp):
1. mvn compile 編譯java項目.
2. mvn test 運行測試用例.
3. mvn package 編譯java項目並打成war包.
4. mvn clean 清理java項目.
a. 配置web項目依賴的jar包
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>8.0.14</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
b. 配置web項目運行web容器插件
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
</configuration>
</plugin>
c. 設置jdk版本:
1. 配置編譯插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
2. 更新Maven項目
9、POM(Project Object Model)項目對象模型(pom.xml)配置:
a、基礎元素配置:
<!-- 指定POM對象模型的版本號 -->
<modelVersion>4.0.0</modelVersion>
<!-- 指定組織ID(公司域名反寫) -->
<groupId>org.fkjava</groupId>
<!-- 指定項目名 -->
<artifactId>Struts2-MyBatis3</artifactId>
<!-- 指定項目的版本 -->
<version>1.0.0-RELEASE</version>
<!-- 指定項目打包的產物(jar、ear、war、pom) -->
<packaging>pom</packaging>
b、全局屬性配置:
<properties>
<currentVersion>${project.version}</currentVersion>
<struts2.version>2.3.16.3</struts2.version>
<mybatis.version>3.2.7</mybatis.version>
<mysql.version>5.1.27</mysql.version>
<tomcat.version>7.0.47</tomcat.version>
</properties>
c、依賴關係配置:
<dependencies>
<dependency>
<groupId>org.fkjava.hrm</groupId>
<artifactId>hrm-server</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>jar</type>
<classifier>jdk15</classifier>
</dependency>
</dependencies>
d、構建元素配置:
<!-- 構建項目元素 -->
<build>
<finalName>hrm</finalName>
<!-- 定義默認的目標 -->
<defaultGoal>package</defaultGoal>
<!-- 源碼目錄 -->
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
<!-- 測試代碼目錄 -->
<testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
<!-- 資源文件 -->
<resources>
<resource>
<!-- 源目錄 -->
<directory>${basedir}/src/main/resources</directory>
</resource>
</resources>
</build>
e、經常使用插件配置:
1. 編譯插件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<inherited>true</inherited>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
2. 定義打jar包插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<inherited>true</inherited>
<version>2.4</version>
<!-- 指定插件的配置信息 -->
<configuration>
<!-- 指定JDK版本 -->
<classifier>jdk17</classifier>
</configuration>
</plugin>
3. 生成API文檔插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<!-- 指定插件須要的配置信息 -->
<configuration>
<!-- 源文件編碼 -->
<encoding>UTF-8</encoding>
<!-- 生成時的編碼 -->
<charset>UTF-8</charset>
<!-- 文檔的編碼 -->
<docencoding>UTF-8</docencoding>
<!-- 文檔標題 -->
<doctitle>瘋狂java</doctitle>
<!-- 窗口標題 -->
<windowtitle>OA辦公管理系統</windowtitle>
</configuration>
<executions>
<execution>
<!-- 指定目標 -->
<goals><goal>javadoc</goal></goals>
<!-- 生命週期哪一個階段時,執行上面javadoc目標 -->
<phase>compile</phase>
</execution>
</executions>
</plugin>
4. tomcat插件:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<!-- 指定插件須要的配置信息 -->
<configuration>
<!-- 設置端口 -->
<port>8080</port>
</configuration>
<executions>
<execution>
<!-- 指定目標 -->
<goals><goal>run</goal></goals>
<!-- 生命週期哪一個階段時,執行上面run目標 -->
<phase>package</phase>
</execution>
</executions>
</plugin>
5. jetty插件:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.16.v20140903</version>
<configuration>
<!-- 設置web項目配置信息 -->
<webApp>
<contextPath>/${project.artifactId}</contextPath>
</webApp>
<!-- 設置支持熱部署 -->
<scanIntervalSeconds>1</scanIntervalSeconds>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>9090</port>
</connector>
</connectors>
</configuration>
<executions>
<execution>
<!-- 指定目標 -->
<goals><goal>run</goal></goals>
<!-- 生命週期哪一個階段時,執行上面run目標 -->
<phase>package</phase>
</execution>
</executions>
</plugin>
f、部署元素配置:
<distributionManagement>
<!-- 發行版本倉庫 -->
<repository>
<id>releases</id>
<name>Nexus Release Repository</name>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<!-- 快照版本倉庫 -->
<snapshotRepository>
<id>snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
g、站點元素配置: <!-- 指定項目名稱,通常用於文檔生成 --> <name>sm</name> <!-- 指定公司網站地址 --> <url>http://maven.apache.org</url> <!-- 指定項目描述 --> <description>瘋狂java用Struts二、MyBatis3架構項目</description> <!-- 指定項目的開始時間 --> <inceptionYear>2014</inceptionYear>