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项目