ideal pom文件安装到maven库中_java学习之web基础(14)Maven基础学习
maven
介紹
Maven 是一個項目管理工具,它包含了一個項目對象模型 (POM: Project Object Model),一組標準集合,一個項目生命周期(Project Lifecycle),
一個依賴管理系統(Dependency Management System),和用來運行定義在生命周期階段(phase)中插件(plugin)目標(goal)的邏輯。
maven可以干什么:
能幫你構建工程,管理 jar包,編譯代碼,還能幫你自動運行單元測試,打包,生成報表,甚至能幫你部署項目,生成 Web 站點
maven對jar包的管理
maven 工程中不直接將 jar 包導入到工程中,而是通過在 pom.xml 文件中添加所需 jar包的坐標,這樣就很好的避免了 jar 直接引入進來,
在需要用到 jar 包的時候,只要查找 pom.xml 文件,再通過 pom.xml 文件中的坐標,到一個專門用于”存放 jar 包的倉庫”(maven 倉庫)中根據坐標從而找到這些 jar 包,再把這些 jar 包拿去運行。
maven一鍵構建
我們的項目,往往都要經歷編譯、 測試、 運行、 打包、 安裝 ,部署等一系列過程。
什么是構建?指的是項目從編譯、測試、運行、打包、安裝 ,部署整個過程都交給 maven 進行管理,這個過程稱為構建。
一鍵構建指的是整個構建過程,使用 maven 一個命令可以輕松完成整個工作
通過 mvn tomcat:run 的這個命令,我們發現現在的工程編譯,測試,運行都變得非常簡單
maven倉庫
本地倉庫 :用來存儲從遠程倉庫或中央倉庫下載的插件和 jar 包,項目使用一些插件或 jar 包,優先從本地倉庫查找。
默認本地倉庫位置在 ${user.dir}/.m2/repository, ${user.dir}表示 windows 用戶目錄。
遠程倉庫:如果本地需要插件或者 jar 包,本地倉庫沒有, 默認去遠程倉庫下載。遠程倉庫可以在互聯網內也可以在局域網內。
中央倉庫 :在 maven 軟件中內置一個遠程倉庫地址 Central Repository: ,它是中央倉庫,服務于整個互聯網,
它是由 Maven 團隊自己維護,里面存儲了非常全的 jar 包,它包含了世界上大部分流行的開源項目構件。
maven標準目錄結構
├─.settings└─src├─main│ ├─java│ │ └─cn│ │ └─figo│ │ └─maven│ │ └─servlet│ ├─resources│ └─webapp│ ├─jsp│ └─WEB-INF└─test├─java│ └─cn│ └─figo│ └─maven│ └─test└─resourcessrc/main/java —— 存放項目的.java 文件 src/main/resources —— 存放項目資源文件,如 spring, hibernate 配置文件 src/test/java —— 存放所有單元測試.java 文件,如 JUnit 測試類 src/test/resources —— 測試資源文件 target —— 項目輸出位置,編譯后的 class 文件會輸出到此目錄 pom.xml——maven 項目核心配置文件 注意:如果是普通的 java 項目,那么就沒有 webapp 目錄maven常用命令
clean、 compile 、test、 package、 install、tomcat:run
compile 是 maven 工程的編譯命令,作用是將 src/main/java 下的文件編譯為 class 文件輸出到 target目錄下。mvn compile
test 是 maven 工程的測試命令 mvn test,會執行 src/test/java 下的單元測試類。
clean 是 maven 工程的清理命令,執行 clean 會刪除 target 目錄及內容
package 是 maven 工程的打包命令,對于 java 工程執行 package 打成 jar 包,對于 web 工程打成 war包。
install 是 maven 工程的安裝命令,執行 install 將 maven 打成 jar 包或 war 包發布到本地倉庫。
tomcat:run 可以運行Maven 工程。進入 maven 工程目錄(當前目錄有 pom.xml 文件),運行 tomcat:run 命令。
maven的pom.xml文件內容
<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>cn.figo.maven</groupId><artifactId>maven-helloworld</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>第一個maven工程</name><description>第一個maven工程</description><!-- 添加servlet-api,jsp-api --><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.9</version><scope>test</scope></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.0</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version><scope>provided</scope></dependency></dependencies><build><!-- 配置了很多插件 --><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.7</source><target>1.7</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.1</version><configuration><port>9090</port><path>/mgr</path><uriEncoding>UTF-8</uriEncoding><server>tomcat7</server></configuration></plugin></plugins></build></project>命令演示
D:1_Codejavamaven-helloworld>mvn clean [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for cn.itcast.maven:maven-helloworld:war:0.0.1-SNAPSHOT [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 37, column 12 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building 第一個maven工程 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-helloworld --- [INFO] Deleting D:1_Codejavamaven-helloworldtarget [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.926 s [INFO] Finished at: 2019-10-16T01:07:13+08:00 [INFO] Final Memory: 6M/24M [INFO] ------------------------------------------------------------------------D:1_Codejavamaven-helloworld>mvn compile [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for cn.itcast.maven:maven-helloworld:war:0.0.1-SNAPSHOT [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 37, column 12 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building 第一個maven工程 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-helloworld --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-helloworld --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:1_Codejavamaven-helloworldtargetclasses [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.496 s [INFO] Finished at: 2019-10-16T01:07:29+08:00 [INFO] Final Memory: 11M/40M [INFO] ------------------------------------------------------------------------D:1_Codejavamaven-helloworld>mvn clean [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for cn.itcast.maven:maven-helloworld:war:0.0.1-SNAPSHOT [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 37, column 12 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building 第一個maven工程 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-helloworld --- [INFO] Deleting D:1_Codejavamaven-helloworldtarget [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.427 s [INFO] Finished at: 2019-10-16T01:08:15+08:00 [INFO] Final Memory: 6M/24M [INFO] ------------------------------------------------------------------------D:1_Codejavamaven-helloworld>mvn test [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for cn.itcast.maven:maven-helloworld:war:0.0.1-SNAPSHOT [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 37, column 12 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building 第一個maven工程 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-helloworld --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-helloworld --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:1_Codejavamaven-helloworldtargetclasses [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-helloworld --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-helloworld --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:1_Codejavamaven-helloworldtargettest-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-helloworld --- [INFO] Surefire report directory: D:1_Codejavamaven-helloworldtargetsurefire-reports-------------------------------------------------------T E S T S ------------------------------------------------------- Running cn.itcast.maven.test.HelloTest hello test.... Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.065 secResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.883 s [INFO] Finished at: 2019-10-16T01:08:29+08:00 [INFO] Final Memory: 12M/44M [INFO] ------------------------------------------------------------------------ D:1_Codejavamaven-helloworld>mvn clean [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for cn.itcast.maven:maven-helloworld:war:0.0.1-SNAPSHOT [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 37, column 12 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building 第一個maven工程 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-helloworld --- [INFO] Deleting D:1_Codejavamaven-helloworldtarget [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.473 s [INFO] Finished at: 2019-10-16T01:09:15+08:00 [INFO] Final Memory: 6M/24M [INFO] ------------------------------------------------------------------------D:1_Codejavamaven-helloworld>mvn package [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for cn.itcast.maven:maven-helloworld:war:0.0.1-SNAPSHOT [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 37, column 12 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building 第一個maven工程 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-helloworld --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-helloworld --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:1_Codejavamaven-helloworldtargetclasses [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-helloworld --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-helloworld --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:1_Codejavamaven-helloworldtargettest-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-helloworld --- [INFO] Surefire report directory: D:1_Codejavamaven-helloworldtargetsurefire-reports-------------------------------------------------------T E S T S ------------------------------------------------------- Running cn.itcast.maven.test.HelloTest hello test.... Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.071 secResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO] [INFO] --- maven-war-plugin:2.2:war (default-war) @ maven-helloworld --- WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/D:/develop/maven_repository/com/thoughtworks/xstream/xstream/1.3.1/xstream-1.3.1.jar) to field java.util.Properties.defaults WARNING: Please consider reporting this to the maintainers of com.thoughtworks.xstream.core.util.Fields WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release [INFO] Packaging webapp [INFO] Assembling webapp [maven-helloworld] in [D:1_Codejavamaven-helloworldtargetmaven-helloworld-0.0.1-SNAPSHOT] [INFO] Processing war project [INFO] Copying webapp resources [D:1_Codejavamaven-helloworldsrcmainwebapp] [INFO] Webapp assembled in [67 msecs] [INFO] Building war: D:1_Codejavamaven-helloworldtargetmaven-helloworld-0.0.1-SNAPSHOT.war [INFO] WEB-INFweb.xml already added, skipping [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 5.411 s [INFO] Finished at: 2019-10-16T01:09:35+08:00 [INFO] Final Memory: 13M/47M [INFO] ------------------------------------------------------------------------D:1_Codejavamaven-helloworld>mvn clean [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for cn.itcast.maven:maven-helloworld:war:0.0.1-SNAPSHOT [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 37, column 12 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building 第一個maven工程 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-helloworld --- [INFO] Deleting D:1_Codejavamaven-helloworldtarget [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.525 s [INFO] Finished at: 2019-10-16T01:11:28+08:00 [INFO] Final Memory: 6M/27M [INFO] ------------------------------------------------------------------------D:1_Codejavamaven-helloworld>mvn install [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for cn.itcast.maven:maven-helloworld:war:0.0.1-SNAPSHOT [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 37, column 12 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building 第一個maven工程 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-helloworld --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-helloworld --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:1_Codejavamaven-helloworldtargetclasses [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-helloworld --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-helloworld --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:1_Codejavamaven-helloworldtargettest-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-helloworld --- [INFO] Surefire report directory: D:1_Codejavamaven-helloworldtargetsurefire-reports-------------------------------------------------------T E S T S ------------------------------------------------------- Running cn.itcast.maven.test.HelloTest hello test.... Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.059 secResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO] [INFO] --- maven-war-plugin:2.2:war (default-war) @ maven-helloworld --- WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/D:/develop/maven_repository/com/thoughtworks/xstream/xstream/1.3.1/xstream-1.3.1.jar) to field java.util.Properties.defaults WARNING: Please consider reporting this to the maintainers of com.thoughtworks.xstream.core.util.Fields WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release [INFO] Packaging webapp [INFO] Assembling webapp [maven-helloworld] in [D:1_Codejavamaven-helloworldtargetmaven-helloworld-0.0.1-SNAPSHOT] [INFO] Processing war project [INFO] Copying webapp resources [D:1_Codejavamaven-helloworldsrcmainwebapp] [INFO] Webapp assembled in [71 msecs] [INFO] Building war: D:1_Codejavamaven-helloworldtargetmaven-helloworld-0.0.1-SNAPSHOT.war [INFO] WEB-INFweb.xml already added, skipping [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ maven-helloworld --- [INFO] Installing D:1_Codejavamaven-helloworldtargetmaven-helloworld-0.0.1-SNAPSHOT.war to D:developmaven_repositorycnitcastmavenmaven-helloworld0.0.1-SNAPSHOTmaven-helloworld-0.0.1-SNAPSHOT.war [INFO] Installing D:1_Codejavamaven-helloworldpom.xml to D:developmaven_repositorycnitcastmavenmaven-helloworld0.0.1-SNAPSHOTmaven-helloworld-0.0.1-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 5.540 s [INFO] Finished at: 2019-10-16T01:11:41+08:00 [INFO] Final Memory: 13M/48M [INFO] ------------------------------------------------------------------------idea中創建maven項目
總結
以上是生活随笔為你收集整理的ideal pom文件安装到maven库中_java学习之web基础(14)Maven基础学习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华硕电脑显卡保修期多久(华硕电脑显卡保修
- 下一篇: 笔记本电脑高清壁纸(笔记本电脑高清壁纸4