IDEA中使用Maven
Maven的安裝與使用
安裝
1、下載,官網(wǎng)下載。
2、解壓,存放路徑中不可包含空格和中文。如:"E:\dev\workspace\maven\apache-maven-3.6.0"
3、配置本地倉庫,進(jìn)入 "conf/settings.xml" 中,在 settings 節(jié)下開啟如下配置,該路徑就是指向本地倉庫的路徑:
<localRepository>E:\dev\workspace\maven\repository</localRepository>Maven 在查找 jar 時(shí)遵循什么樣的順序呢?
為方便使用,這里提供了已包含常用 jar 包的本地倉庫,點(diǎn)擊下載。
三套生命周期
Maven 對(duì)項(xiàng)目構(gòu)建過程分為三套相互獨(dú)立的生命周期,請(qǐng)注意這里說的是“三套”,而且“相互獨(dú)立”,這三套生命周期分別是:
每一個(gè)階段都有一個(gè)對(duì)應(yīng)的命令,且有相應(yīng)的插件來支持命令的執(zhí)行。
注:屬于同一個(gè)命令周期內(nèi)的命令,當(dāng)執(zhí)行后面的命令時(shí),前面的命令會(huì)自動(dòng)執(zhí)行。
常用命令
- complie:編譯命令,作用是將 'src/main/java' 下的 java 源文件編譯為 class 文件并輸出到 target 下的 classes 目錄下。 ZHANGZHONGEN+zze@ZHANGZHONGEN MINGW64 /f/idea/0219/helloworld $ mvn compile [INFO] Scanning for projects... [INFO] [INFO] -------------------------< com.zze:helloworld >------------------------- [INFO] Building helloworld Maven Webapp 1.0-SNAPSHOT [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ helloworld --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory F:\idea\0219\helloworld\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ helloworld --- [INFO] Nothing to compile - all classes are up to date [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.763 s [INFO] Finished at: 2019-02-21T15:22:51+08:00 [INFO] ------------------------------------------------------------------------ 例:
- clean:清除命令,執(zhí)行 clean 會(huì)刪除 target 目錄及其目錄下所有內(nèi)容。 ZHANGZHONGEN+zze@ZHANGZHONGEN MINGW64 /f/idea/0219/helloworld $ mvn clean [INFO] Scanning for projects... [INFO] [INFO] -------------------------< com.zze:helloworld >------------------------- [INFO] Building helloworld Maven Webapp 1.0-SNAPSHOT [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ helloworld --- [INFO] Deleting F:\idea\0219\helloworld\target [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.359 s [INFO] Finished at: 2019-02-21T15:27:50+08:00 [INFO] ------------------------------------------------------------------------ 例:
- test:測試命令,會(huì)執(zhí)行 'src/main/java' 下的單元測試類。 ZHANGZHONGEN+zze@ZHANGZHONGEN MINGW64 /f/idea/0219/helloworld $ mvn test [INFO] Scanning for projects... [INFO] [INFO] -------------------------< com.zze:helloworld >------------------------- [INFO] Building helloworld Maven Webapp 1.0-SNAPSHOT [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ helloworld --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory F:\idea\0219\helloworld\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ helloworld --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ helloworld --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory F:\idea\0219\helloworld\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ helloworld --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to F:\idea\0219\helloworld\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ helloworld --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.zze.test1.DemoTest 2 [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.019 s - in com.zze.test1.DemoTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.414 s [INFO] Finished at: 2019-02-21T15:36:20+08:00 [INFO] ------------------------------------------------------------------------ 例:
- package:打包命令,執(zhí)行 package 命令對(duì)于 java 工程會(huì)打成 jar 包,對(duì)于 web 工程會(huì)打成 war 包。 ZHANGZHONGEN+zze@ZHANGZHONGEN MINGW64 /f/idea/0219/helloworld $ mvn package [INFO] Scanning for projects... [INFO] [INFO] -------------------------< com.zze:helloworld >------------------------- [INFO] Building helloworld Maven Webapp 1.0-SNAPSHOT [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ helloworld --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory F:\idea\0219\helloworld\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ helloworld --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ helloworld --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory F:\idea\0219\helloworld\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ helloworld --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to F:\idea\0219\helloworld\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ helloworld --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.zze.test1.DemoTest 2 [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.018 s - in com.zze.test1.DemoTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] [INFO] --- maven-war-plugin:3.2.2:war (default-war) @ helloworld --- [INFO] Packaging webapp [INFO] Assembling webapp [helloworld] in [F:\idea\0219\helloworld\target\helloworld] [INFO] Processing war project [INFO] Copying webapp resources [F:\idea\0219\helloworld\src\main\webapp] [INFO] Webapp assembled in [44 msecs] [INFO] Building war: F:\idea\0219\helloworld\target\helloworld.war [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.752 s [INFO] Finished at: 2019-02-21T15:40:07+08:00 [INFO] ------------------------------------------------------------------------ 例:
- install:安裝命令,執(zhí)行 install 會(huì)將項(xiàng)目打成 jar 或 war 包發(fā)布到本地倉庫。 ZHANGZHONGEN+zze@ZHANGZHONGEN MINGW64 /f/idea/0219/helloworld $ mvn install [INFO] Scanning for projects... [INFO] [INFO] -------------------------< com.zze:helloworld >------------------------- [INFO] Building helloworld Maven Webapp 1.0-SNAPSHOT [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ helloworld --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory F:\idea\0219\helloworld\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ helloworld --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ helloworld --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory F:\idea\0219\helloworld\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ helloworld --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ helloworld --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.zze.test1.DemoTest 2 [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.021 s - in com.zze.test1.DemoTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] [INFO] --- maven-war-plugin:3.2.2:war (default-war) @ helloworld --- [INFO] Packaging webapp [INFO] Assembling webapp [helloworld] in [F:\idea\0219\helloworld\target\helloworld] [INFO] Processing war project [INFO] Copying webapp resources [F:\idea\0219\helloworld\src\main\webapp] [INFO] Webapp assembled in [43 msecs] [INFO] Building war: F:\idea\0219\helloworld\target\helloworld.war [INFO] [INFO] --- maven-install-plugin:2.5.2:install (default-install) @ helloworld --- [INFO] Installing F:\idea\0219\helloworld\target\helloworld.war to E:\dev\workspace\maven\repository\com\zze\helloworld\1.0-SNAPSHOT\helloworld-1.0-SNAPSHOT.war [INFO] Installing F:\idea\0219\helloworld\pom.xml to E:\dev\workspace\maven\repository\com\zze\helloworld\1.0-SNAPSHOT\helloworld-1.0-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.544 s [INFO] Finished at: 2019-02-21T15:44:21+08:00 [INFO] ------------------------------------------------------------------------ 例:
依賴管理
依賴的范圍
- compile:編譯范圍,默認(rèn)值
compile 是默認(rèn)的范圍;如果沒有提供一個(gè)范圍,那該依賴的范圍就是編譯范圍。編譯范圍依賴在所有的 classpath 中可用,同時(shí)它們也會(huì)被打包。
- provided:已提供范圍
provided 依賴只有在當(dāng) JDK 或者一個(gè)容器已提供該依賴之后才使用。例如, 如果你開發(fā)了一個(gè) web 應(yīng)用,你可能在編譯 classpath 中需要可用的 Servlet API 來編譯一個(gè) Servlet,但是你不會(huì)想要在打包好的 war 中包含這個(gè) Servlet API;這個(gè) Servlet API JAR 由你的應(yīng)用服務(wù)器或者 Servlet 容器提供。已提供范圍的依賴在編譯 classpath (不是運(yùn)行時(shí))可用。它們不是傳遞性的,也不會(huì)被打包。
- runtime:運(yùn)行時(shí)范圍
runtime 依賴在運(yùn)行和測試系統(tǒng)的時(shí)候需要,但在編譯的時(shí)候不需要。比如,你可能在編譯的時(shí)候只需要 JDBC API JAR,而只有在運(yùn)行的時(shí)候才需要 JDBC 驅(qū)動(dòng)實(shí)現(xiàn)。
- test:測試范圍
test 范圍依賴在一般的編譯和運(yùn)行時(shí)都不需要,它們只有在測試編譯和測試運(yùn)行階段可用。
- system:系統(tǒng)范圍
system 范圍依賴與 provided 類似,但是你必須顯式的提供一個(gè)對(duì)于本地系統(tǒng)中JAR 文件的路徑。這么做是為了允許基于本地對(duì)象編譯,而這些對(duì)象是系統(tǒng)類庫的一部分。這樣的構(gòu)件應(yīng)該是一直可用的,Maven 也不會(huì)在倉庫中去尋找它。如果你將一個(gè)依賴范圍設(shè)置成系統(tǒng)范圍,你必須同時(shí)提供一個(gè) systemPath 元素。注意該范圍是不推薦使用的(你應(yīng)該一直盡量去從公共或定制的 Maven 倉庫中引用依賴)。
依賴的傳遞
參考工程的繼承與聚合,它其實(shí)就是使用依賴的傳遞來實(shí)現(xiàn)的。
排除依賴
創(chuàng)建工程,引入 'struts2-core' 依賴:
<?xml version="1.0" encoding="UTF-8"?> <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.zze</groupId><artifactId>A</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-core</artifactId><version>2.3.37</version></dependency></dependencies></project> pom.xml假如我們不想使用傳遞進(jìn)來的 'javassist',那么我們可以通過配置將其排除:
<?xml version="1.0" encoding="UTF-8"?> <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.zze</groupId><artifactId>A</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-core</artifactId><version>2.3.37</version><exclusions><exclusion><groupId>javassist</groupId><artifactId>javassist</artifactId></exclusion></exclusions></dependency></dependencies></project> pom.xml路徑近者優(yōu)先
創(chuàng)建工程,引入'struts2-spring-plugin' 依賴:
<?xml version="1.0" encoding="UTF-8"?> <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.zze</groupId><artifactId>A</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-spring-plugin</artifactId><version>2.3.37</version></dependency></dependencies></project> pom.xml接著引入 'spring-beans' 的依賴:
<?xml version="1.0" encoding="UTF-8"?> <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.zze</groupId><artifactId>A</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-spring-plugin</artifactId><version>2.3.37</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.2.4.RELEASE</version></dependency></dependencies></project> pom.xml此時(shí)會(huì)發(fā)現(xiàn) 'spring-beans' 的版本為下面直接聲明的版本,因?yàn)樗侵苯右?#xff0c;相對(duì)傳進(jìn)進(jìn)來路徑更近。
第一聲明者優(yōu)先
創(chuàng)建工程,引入 'struts2-spring-plugin' 依賴:
<?xml version="1.0" encoding="UTF-8"?> <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.zze</groupId><artifactId>A</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-spring-plugin</artifactId><version>2.3.37</version></dependency></dependencies></project> pom.xml接著引入 'spring-context' 依賴:
<?xml version="1.0" encoding="UTF-8"?> <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.zze</groupId><artifactId>A</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-spring-plugin</artifactId><version>2.3.37</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.4.RELEASE</version></dependency></dependencies></project> pom.xml此時(shí)會(huì)發(fā)現(xiàn) 'spring-beans' 的版本依舊是 'struts2-spring-plugin' 傳遞進(jìn)來的,因?yàn)?'struts2-spring-plugin' 是先聲明的。
交換 'struts2-spring-plugin' 和 'spring-context' 依賴的聲明順序:
<?xml version="1.0" encoding="UTF-8"?> <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.zze</groupId><artifactId>A</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.4.RELEASE</version></dependency><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-spring-plugin</artifactId><version>2.3.37</version></dependency></dependencies></project> pom.xml此時(shí) 'spring-beans' 的版本就改為 'spring-context' 傳遞進(jìn)來的版本了,因?yàn)?'spring-context' 是先聲明的。
版本鎖定
版本鎖定一般在父子工程間使用,創(chuàng)建父工程 A,鎖定 'spring-beans' 的版本:
<?xml version="1.0" encoding="UTF-8"?><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.zze</groupId><artifactId>A</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><modules><module>../B</module></modules><dependencyManagement><!--dependencyManagement 下的 dependencies 節(jié)只是用來預(yù)先鎖定指定依賴的版本,并不會(huì)真的引入依賴--><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.2.4.RELEASE</version></dependency></dependencies></dependencyManagement></project> pom.xml [A]創(chuàng)建子工程 B ,繼承父工程 A,引入 'spring-beans' 依賴:
<?xml version="1.0" encoding="UTF-8"?> <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"><parent><artifactId>A</artifactId><groupId>com.zze</groupId><version>1.0-SNAPSHOT</version><relativePath>../A/pom.xml</relativePath></parent><modelVersion>4.0.0</modelVersion><artifactId>B</artifactId><dependencies><!--因?yàn)樵诟腹こ讨幸呀?jīng)鎖定了 spring-beans 的版本,所以在子工程中不用指定版本--><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId></dependency></dependencies> </project> pom.xml [B]因?yàn)樵诟腹こ讨幸呀?jīng)鎖定了 'spring-beans' 的版本,所以在子工程中不指定版本會(huì)默認(rèn)引用父工程鎖定的版本。
版本常量使用
創(chuàng)建工程,創(chuàng)建版本常量,在依賴中引用版本常量:
<?xml version="1.0" encoding="UTF-8"?><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.zze</groupId><artifactId>A</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><properties><!--創(chuàng)建版本常量--><spring.version>4.2.4.RELEASE</spring.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><!--引用版本常量--><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency></dependencies></dependencyManagement></project> pom.xml工程的繼承與聚合
繼承
創(chuàng)建一個(gè)父工程 A,再創(chuàng)建一個(gè)子工程 B 繼承 A:
<?xml version="1.0" encoding="UTF-8"?><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.zze</groupId><artifactId>A</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><modules><module>../B</module></modules><dependencies><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-core</artifactId><version>2.3.37</version></dependency></dependencies> </project> pom.xml [A] <?xml version="1.0" encoding="UTF-8"?> <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"><parent><artifactId>A</artifactId><groupId>com.zze</groupId><version>1.0-SNAPSHOT</version><relativePath>../A/pom.xml</relativePath></parent><modelVersion>4.0.0</modelVersion><packaging>jar</packaging><artifactId>B</artifactId></project> pom.xml [B]A 的 依賴會(huì)傳遞給B,此時(shí)就可以直接在子工程 B 中使用父工程 A 依賴了,這就是工程的繼承。
聚合
創(chuàng)建工程 A,再創(chuàng)建工程 B 依賴工程 A:
<?xml version="1.0" encoding="UTF-8"?> <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.zze</groupId><artifactId>A</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-core</artifactId><version>2.3.37</version></dependency></dependencies> </project> pom.xml [A] <?xml version="1.0" encoding="UTF-8"?> <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.zze</groupId><artifactId>B</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>com.zze</groupId><artifactId>A</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies> </project> pom.xml [B]此時(shí) A 的依賴會(huì)傳遞給 B ,在工程 B 就可以直接使用工程 A 引入的依賴了,這就是工程的聚合。
IDEA中使用Maven
配置
1、快捷鍵 CTRL+ALT+S 打開 IDEA 設(shè)置,配置 Maven 地址:
2、在 Importing 頁中勾選如圖項(xiàng):
3、配置 Runner 頁中屬性 '-DarchetypeCatalog=internal',防止未聯(lián)網(wǎng)情況不能創(chuàng)建 Maven 工程。
創(chuàng)建工程
創(chuàng)建java工程
1、新建項(xiàng)目,選中 Maven,直接 Next:
2、輸入坐標(biāo),再次 Next:
3、直接 Finish:
4、創(chuàng)建完成,編寫代碼測試:
創(chuàng)建web工程
1、新建項(xiàng)目,選中 Maven,如圖選擇 web 工程骨架,Next:
2、選擇自己配置的 Maven 目錄,Next:
3、直接 Finish:
4、輸出如圖則創(chuàng)建成功:
web項(xiàng)目的運(yùn)行
準(zhǔn)備
下面以運(yùn)行一個(gè) HelloWorld 程序?yàn)槔?#xff1a;
1、新建 web 項(xiàng)目,在 'src/main' 下 java 創(chuàng)建文件夾,并標(biāo)記其為 Sources Root 文件夾,執(zhí)行完這個(gè)操作后 java 文件夾就相當(dāng)于普通工程的 classpath 根目錄了。
2、在 pom 文件中引入 Servlet 開發(fā)依賴 jar:
<dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version><scope>provided</scope> </dependency> <dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.0</version><scope>provided</scope> </dependency>注意:因?yàn)?maven? web 工程后續(xù)運(yùn)行時(shí)使用 maven 提供的 tomcat 環(huán)境,這里要設(shè)置引入 jar 的 scope 為 provided,否則會(huì)因?yàn)?jar 包重復(fù)出異常。
3、在 'src\main\java' 下創(chuàng)建 Servlet 如下:
package com.zze.servlet;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException;public class HelloServlet extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.getWriter().write("Hello world!!!");} } com.zze.servlet.HelloServlet <servlet><servlet-name>helloServlet</servlet-name><servlet-class>com.zze.servlet.HelloServlet</servlet-class> </servlet> <servlet-mapping><servlet-name>helloServlet</servlet-name><url-pattern>/hello</url-pattern> </servlet-mapping> WEB-INF/web.xml運(yùn)行方式一:命令運(yùn)行
1、打開 MavenProjects 窗口:
2、點(diǎn)擊如圖按鈕:
3、輸入 'tomcat:run' 指令,Execute:
4、此時(shí)項(xiàng)目就以被部署到 tomcat 并運(yùn)行:
5、訪問 'localhost:8080/helloworld/hello' 測試:
運(yùn)行方式二:配置運(yùn)行
1、進(jìn)入 Edit Configurations:
2、選擇 Maven:
3、輸入如下,Apply:
4、此時(shí)就可直接點(diǎn)擊該圖標(biāo)啟動(dòng)項(xiàng)目了:
5、還可在 Maven Projects 窗口中雙擊該配置啟動(dòng):
運(yùn)行方式三:本地Tomcat運(yùn)行
1、進(jìn)入 Edit Configurations:
2、選擇 Tomcat 下的 Local 項(xiàng):
3、選擇 Deployment 欄,點(diǎn)擊 + 號(hào):
3、選擇 Artifact:
4、選擇 war exploded 結(jié)尾項(xiàng):
5、OK,接下來就可以像運(yùn)行普通 web 工程一樣啟動(dòng) maven 項(xiàng)目:
補(bǔ)充
配置國內(nèi)倉庫源
在 "conf/settings.xml" 文件中的?mirrors 節(jié)下選下面一個(gè)節(jié)點(diǎn)添加即可:
<mirror><id>alimaven</id><name>aliyun maven</name><url>http://maven.aliyun.com/nexus/content/groups/public/</url><mirrorOf>central</mirrorOf> </mirror> 阿里云 <mirror><id>jboss-public-repository-group</id><mirrorOf>central</mirrorOf><name>JBoss Public Repository Group</name><url>http://repository.jboss.org/nexus/content/groups/public</url> </mirror> jboss配置Tomcat插件
<!--使用 tomcat7:run--> <plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version><configuration><port>8080</port><path>/</path></configuration> </plugin>聚合工程整合SSH示例
點(diǎn)擊下載
IDEA創(chuàng)建Maven工程常用骨架
下面是我習(xí)慣使用的骨架:
創(chuàng)建普通 java 工程:不使用骨架;
創(chuàng)建父工程:maven-archetype-site-simple;
創(chuàng)建 web 工程:maven-archetype-webapp;
轉(zhuǎn)載于:https://www.cnblogs.com/zze46/p/10399663.html
總結(jié)
以上是生活随笔為你收集整理的IDEA中使用Maven的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。