日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

IDEA中使用Maven

發(fā)布時間:2023/12/1 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IDEA中使用Maven 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Maven的安裝與使用

安裝

1、下載,官網(wǎng)下載。

2、解壓,存放路徑中不可包含空格和中文。如:"E:\dev\workspace\maven\apache-maven-3.6.0"

3、配置本地倉庫,進入 "conf/settings.xml" 中,在 settings 節(jié)下開啟如下配置,該路徑就是指向本地倉庫的路徑:

<localRepository>E:\dev\workspace\maven\repository</localRepository>

Maven 在查找 jar 時遵循什么樣的順序呢?

  • 優(yōu)先在本地倉庫中查找。
  • 如果本地倉庫中找不到,則從私服查找,找到后下載到本地倉庫。
  • 如果私服中找不到,則從中央倉庫查找,找到后下載帶私服,最后下載到本地倉庫。
  • 為方便使用,這里提供了已包含常用 jar 包的本地倉庫,點擊下載。

    三套生命周期

    Maven 對項目構建過程分為三套相互獨立的生命周期,請注意這里說的是“三套”,而且“相互獨立”,這三套生命周期分別是:

  • Clean Lifecycle:在進行真正的構建之前進行一些清理工作。
  • Default Lifecycle:構建的核心部分,如編輯、測試、打包、部署等等。
  • Site Lifecycle:生成項目報告、站點、發(fā)布站點。
  • 每一個階段都有一個對應的命令,且有相應的插件來支持命令的執(zhí)行。

    注:屬于同一個命令周期內的命令,當執(zhí)行后面的命令時,前面的命令會自動執(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 會刪除 target 目錄及其目錄下所有內容。 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:測試命令,會執(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 命令對于 java 工程會打成 jar 包,對于 web 工程會打成 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 會將項目打成 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:編譯范圍,默認值

      compile 是默認的范圍;如果沒有提供一個范圍,那該依賴的范圍就是編譯范圍。編譯范圍依賴在所有的 classpath 中可用,同時它們也會被打包。

    • provided:已提供范圍

      provided 依賴只有在當 JDK 或者一個容器已提供該依賴之后才使用。例如, 如果你開發(fā)了一個 web 應用,你可能在編譯 classpath 中需要可用的 Servlet API 來編譯一個 Servlet,但是你不會想要在打包好的 war 中包含這個 Servlet API;這個 Servlet API JAR 由你的應用服務器或者 Servlet 容器提供。已提供范圍的依賴在編譯 classpath (不是運行時)可用。它們不是傳遞性的,也不會被打包。

    • runtime:運行時范圍

      runtime 依賴在運行和測試系統(tǒng)的時候需要,但在編譯的時候不需要。比如,你可能在編譯的時候只需要 JDBC API JAR,而只有在運行的時候才需要 JDBC 驅動實現(xiàn)。

    • test:測試范圍

      test 范圍依賴在一般的編譯和運行時都不需要,它們只有在測試編譯和測試運行階段可用。

    • system:系統(tǒng)范圍

      system 范圍依賴與 provided 類似,但是你必須顯式的提供一個對于本地系統(tǒng)中JAR 文件的路徑。這么做是為了允許基于本地對象編譯,而這些對象是系統(tǒng)類庫的一部分。這樣的構件應該是一直可用的,Maven 也不會在倉庫中去尋找它。如果你將一個依賴范圍設置成系統(tǒng)范圍,你必須同時提供一個 systemPath 元素。注意該范圍是不推薦使用的(你應該一直盡量去從公共或定制的 Maven 倉庫中引用依賴)。

    依賴的傳遞

    參考工程的繼承與聚合,它其實就是使用依賴的傳遞來實現(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

    假如我們不想使用傳遞進來的 '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

    此時會發(fā)現(xiàn) 'spring-beans' 的版本為下面直接聲明的版本,因為它是直接引入,相對傳進進來路徑更近。

    第一聲明者優(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

    此時會發(fā)現(xiàn) 'spring-beans' 的版本依舊是 'struts2-spring-plugin' 傳遞進來的,因為 '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

    此時 'spring-beans' 的版本就改為 'spring-context' 傳遞進來的版本了,因為 '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é)只是用來預先鎖定指定依賴的版本,并不會真的引入依賴--><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><!--因為在父工程中已經(jīng)鎖定了 spring-beans 的版本,所以在子工程中不用指定版本--><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId></dependency></dependencies> </project> pom.xml [B]

    因為在父工程中已經(jīng)鎖定了 'spring-beans' 的版本,所以在子工程中不指定版本會默認引用父工程鎖定的版本。

    版本常量使用

    創(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)建一個父工程 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><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 的 依賴會傳遞給B,此時就可以直接在子工程 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]

    此時 A 的依賴會傳遞給 B ,在工程 B 就可以直接使用工程 A 引入的依賴了,這就是工程的聚合。

    IDEA中使用Maven

    配置

    1、快捷鍵 CTRL+ALT+S 打開 IDEA 設置,配置 Maven 地址:

    2、在 Importing 頁中勾選如圖項:

    3、配置 Runner 頁中屬性 '-DarchetypeCatalog=internal',防止未聯(lián)網(wǎng)情況不能創(chuàng)建 Maven 工程。

    創(chuàng)建工程

    創(chuàng)建java工程

    1、新建項目,選中 Maven,直接 Next:

    2、輸入坐標,再次 Next:

    3、直接 Finish:

    4、創(chuàng)建完成,編寫代碼測試:

    創(chuàng)建web工程

    1、新建項目,選中 Maven,如圖選擇 web 工程骨架,Next:

    2、選擇自己配置的 Maven 目錄,Next:

    3、直接 Finish:

    4、輸出如圖則創(chuàng)建成功:

    web項目的運行

    準備

    下面以運行一個 HelloWorld 程序為例:

    1、新建 web 項目,在 'src/main' 下 java 創(chuàng)建文件夾,并標記其為 Sources Root 文件夾,執(zhí)行完這個操作后 java 文件夾就相當于普通工程的 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>

    注意:因為 maven? web 工程后續(xù)運行時使用 maven 提供的 tomcat 環(huán)境,這里要設置引入 jar 的 scope 為 provided,否則會因為 jar 包重復出異常。

    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

    運行方式一:命令運行

    1、打開 MavenProjects 窗口:

    2、點擊如圖按鈕:

    3、輸入 'tomcat:run' 指令,Execute:

    4、此時項目就以被部署到 tomcat 并運行:

    5、訪問 'localhost:8080/helloworld/hello' 測試:

    運行方式二:配置運行

    1、進入 Edit Configurations:

    2、選擇 Maven:

    3、輸入如下,Apply:

    4、此時就可直接點擊該圖標啟動項目了:

    5、還可在 Maven Projects 窗口中雙擊該配置啟動:

    運行方式三:本地Tomcat運行

    1、進入 Edit Configurations:

    2、選擇 Tomcat 下的 Local 項:

    3、選擇 Deployment 欄,點擊 + 號:

    3、選擇 Artifact:

    4、選擇 war exploded 結尾項:

    5、OK,接下來就可以像運行普通 web 工程一樣啟動 maven 項目:

    補充

    配置國內倉庫源

    在 "conf/settings.xml" 文件中的?mirrors 節(jié)下選下面一個節(jié)點添加即可:

    <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示例

    點擊下載

    IDEA創(chuàng)建Maven工程常用骨架

    下面是我習慣使用的骨架:

    創(chuàng)建普通 java 工程:不使用骨架;

    創(chuàng)建父工程:maven-archetype-site-simple;

    創(chuàng)建 web 工程:maven-archetype-webapp;

    轉載于:https://www.cnblogs.com/zze46/p/10399663.html

    總結

    以上是生活随笔為你收集整理的IDEA中使用Maven的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。