Maven使用入门
Maven是Java中开发流行的项目管理工具,其他同类产品还有ant(见过工厂制造执行系统MES中使用ant打包)和gradle(Spring源码使用的gradle管理),目前来说maven是最广泛使用的,下面简单记录一下。
Maven安装和配置
maven可以管理项目的生命周期,包括编译、测试、打包,发布、部署的整个过程,其安装过程非常方便,下面是window下的安装方式。
(1)官网下载maven,地址:http://maven.apache.org/download.cgi,目前最新已经支持3.6.2,如果是下载3.6版本以上的maven,需要保证至少JDK8的版本,并且环境变量保证JAVE_HOME指向jdk正确的安装目录,maven启动后会读取这个变量,如果没有配置JAVA_HOME,启动会出现问题。
# maven启动bat文件中需要读取JAVA_HOME变量
@REM ==== START VALIDATION ==== if not "%JAVA_HOME%"=="" goto OkJHome for %%i in (java.exe) do set "JAVACMD=%%~$PATH:i" goto checkJCmd :OkJHome set "JAVACMD=%JAVA_HOME%\bin\java.exe"
(2)下载完成后解压安装包即可,开始配置Maven环境变量,注意安装目录中不要有中文或空格。
添加MAVEN_HOME变量名和变量值,变量值就是maven目录,根据具体路径配置即可。
将MAVEN_HOME添加到path环境变量,将%MAVEN_HOME%\bin;添加到末尾。
如果是linux,配置类似,修改/etc/profile文件即可,export MAVEN_HOME='maven安装目录',然后export $MAVEN_HOME/bin:$PATH即可。
(3)安装完成后进入CMD命令窗口执行mvn -v命令,如果能看到提示版本信息,说明安装成功。
(4)conf/setting.xml中配置maven本地库和镜像库,本地库就是保存在本机的jar包资源的集合,镜像库是保存在远程的,镜像库可以选择有名的私服或者maven中央仓库。由于中央仓库在美国,下载依赖很慢,因此一般选择国内阿里云或网易私服镜像。
<!-- localRepository | The path to the local repository maven will use to store artifacts. | | Default: ${user.home}/.m2/repository <localRepository>/path/to/local/repo</localRepository> -->
<!--本地库如果不配置就默认在用户家目录/.m2/repository,一般在c盘,最好修改不要放到c盘-->
<localRepository>本地库路径</localRepository> ...省略 <mirrors> <!-- mirror | Specifies a repository mirror site to use instead of a given repository. The repository that | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used | for inheritance and direct lookup purposes, and must be unique across the set of mirrors. | <mirror> <id>mirrorId</id> <mirrorOf>repositoryId</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://my.repository.com/repo/path</url> </mirror> -->
<!--配置阿里云镜像-->
<mirror> <id>nexus-aliyun</id> <mirrorOf>central</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror>
</mirrors>
到此为止,就完成了maven的安装配置,接下来可以使用maven了,如通过pom.xml文件的配置,让maven帮忙下载jar包。
常用命令
(1)可以使用交互式方式创建项目project,在window目录下敲击cmd,直接进入当前目录cmd窗口,使用mvn archetype:generate命令创建项目,也可以使用非交互式的方式直接创建,最后说明。
# 创建项目过程
1 D:\mvnProject>mvn archetype:generate #回车 2 [INFO] Scanning for projects... 3 [INFO] 4 [INFO] ------------------< org.apache.maven:standalone-pom >------------------- 5 [INFO] Building Maven Stub Project (No POM) 1 6 [INFO] --------------------------------[ pom ]--------------------------------- 7 [INFO] 8 [INFO] >>> maven-archetype-plugin:3.1.0:generate (default-cli) > generate-source 9 s @ standalone-pom >>> 10 [INFO] 11 [INFO] <<< maven-archetype-plugin:3.1.0:generate (default-cli) < generate-source 12 s @ standalone-pom <<< 13 [INFO] 14 [INFO] 15 [INFO] --- maven-archetype-plugin:3.1.0:generate (default-cli) @ standalone-pom 16 --- 17 [INFO] Generating project in Interactive mode # 提示创建模式为交互式 18 [WARNING] No archetype found in remote catalog. Defaulting to internal catalog 19 [INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven. 20 archetypes:maven-archetype-quickstart:1.0) 21 Choose archetype:
# 提示可以选择的archetype,一共10种,如果不选择默认是7 22 1: internal -> org.apache.maven.archetypes:maven-archetype-archetype (An archety 23 pe which contains a sample archetype.) 24 2: internal -> org.apache.maven.archetypes:maven-archetype-j2ee-simple (An arche 25 type which contains a simplifed sample J2EE application.) 26 3: internal -> org.apache.maven.archetypes:maven-archetype-plugin (An archetype 27 which contains a sample Maven plugin.) 28 4: internal -> org.apache.maven.archetypes:maven-archetype-plugin-site (An arche 29 type which contains a sample Maven plugin site. 30 This archetype can be layered upon an existing Maven plugin project.) 31 5: internal -> org.apache.maven.archetypes:maven-archetype-portlet (An archetype 32 which contains a sample JSR-268 Portlet.) 33 6: internal -> org.apache.maven.archetypes:maven-archetype-profiles ()
# 普通maven工程选择7 34 7: internal -> org.apache.maven.archetypes:maven-archetype-quickstart (An archet 35 ype which contains a sample Maven project.) 36 8: internal -> org.apache.maven.archetypes:maven-archetype-site (An archetype wh 37 ich contains a sample Maven site which demonstrates 38 some of the supported document types like APT, XDoc, and FML and demonstra 39 tes how 40 to i18n your site. This archetype can be layered upon an existing Maven pr 41 oject.) 42 9: internal -> org.apache.maven.archetypes:maven-archetype-site-simple (An arche 43 type which contains a sample Maven site.)
# web工程选择10 44 10: internal -> org.apache.maven.archetypes:maven-archetype-webapp (An archetype 45 which contains a sample Maven Webapp project.) 46 Choose a number or apply filter (format: [groupId:]artifactId, case sensitive co 47 ntains): 7: 7 # 选择7 48 Define value for property 'groupId': com.boe #groupId一般为公司的域名倒写 49 Define value for property 'artifactId': mvntest #artifactId一般为项目名 50 Define value for property 'version' 1.0-SNAPSHOT: : #首次建立默认为snapshot,代表还在开发中,其他还有RELEASE(发布)和LATEST(最新) 51 Define value for property 'package' com.boe: : #输入包名,默认和groupId一样
# 最后确认配置,确认选Y
52 Confirm properties configuration: 53 groupId: com.boe 54 artifactId: mvntest 55 version: 1.0-SNAPSHOT 56 package: com.boe 57 Y: : Y
# 打印日志,提示创建项目成功 58 [INFO] ------------------------------------------------------------------------- 59 --- 60 [INFO] Using following parameters for creating project from Old (1.x) Archetype: 61 maven-archetype-quickstart:1.1 62 [INFO] ------------------------------------------------------------------------- 63 --- 64 [INFO] Parameter: basedir, Value: D:\mvnProject 65 [INFO] Parameter: package, Value: com.boe 66 [INFO] Parameter: groupId, Value: com.boe 67 [INFO] Parameter: artifactId, Value: mvntest 68 [INFO] Parameter: packageName, Value: com.boe 69 [INFO] Parameter: version, Value: 1.0-SNAPSHOT 70 [INFO] project created from Old (1.x) Archetype in dir: D:\mvnProject\mvntest 71 [INFO] ------------------------------------------------------------------------ 72 [INFO] BUILD SUCCESS 73 [INFO] ------------------------------------------------------------------------ 74 [INFO] Total time: 01:27 min 75 [INFO] Finished at: 2019-11-09T15:43:17+08:00 76 [INFO] ------------------------------------------------------------------------
这样就完成了项目的创建,可以去目录下查看发现有了新的项目,但是只有main和test代码目录,没有resources目录,需要手动添加。
除了使用交互式方式,还可以使用非交互式的方式创建工程。
# 非交互方式,一行指定groupId,artifactId,package,还有构建骨架类型archetype等
1 D:\mvnProject>mvn archetype:generate -DgroupId=com.boe -DartifactId=mvntest1 - 2 Dpackage=com.boe.mvntest1 -DarchetypeArtifactId=maven-archetype-webapp -Dvers 3 ion=1.0 -DinteractiveMode=No; 4 [INFO] Scanning for projects... 5 [INFO] 6 [INFO] ------------------< org.apache.maven:standalone-pom >------------------- 7 [INFO] Building Maven Stub Project (No POM) 1 8 [INFO] --------------------------------[ pom ]--------------------------------- 9 [INFO] 10 [INFO] >>> maven-archetype-plugin:3.1.0:generate (default-cli) > generate-source 11 s @ standalone-pom >>> 12 [INFO] 13 [INFO] <<< maven-archetype-plugin:3.1.0:generate (default-cli) < generate-source 14 s @ standalone-pom <<< 15 [INFO] 16 [INFO] 17 [INFO] --- maven-archetype-plugin:3.1.0:generate (default-cli) @ standalone-pom 18 ---
# 提示为批处理模式 19 [INFO] Generating project in Batch mode 20 [WARNING] No archetype found in remote catalog. Defaulting to internal catalog 21 Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/publ 22 ic/org/apache/maven/archetypes/maven-archetype-bundles/2/maven-archetype-bundles 23 -2.pom 24 Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/publi 25 c/org/apache/maven/archetypes/maven-archetype-bundles/2/maven-archetype-bundles- 26 2.pom (1.5 kB at 1.3 kB/s) 27 Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/publ 28 ic/org/apache/maven/archetype/maven-archetype-parent/1/maven-archetype-parent-1. 29 pom 30 Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/publi 31 c/org/apache/maven/archetype/maven-archetype-parent/1/maven-archetype-parent-1.p 32 om (1.3 kB at 3.5 kB/s)
# 提示创建成功 33 [INFO] ------------------------------------------------------------------------- 34 --- 35 [INFO] Using following parameters for creating project from Old (1.x) Archetype: 36 maven-archetype-webapp:1.0 37 [INFO] ------------------------------------------------------------------------- 38 --- 39 [INFO] Parameter: basedir, Value: D:\mvnProject 40 [INFO] Parameter: package, Value: com.boe.mvntest1 41 [INFO] Parameter: groupId, Value: com.boe 42 [INFO] Parameter: artifactId, Value: mvntest1 43 [INFO] Parameter: packageName, Value: com.boe.mvntest1 44 [INFO] Parameter: version, Value: 1.0 45 [INFO] project created from Old (1.x) Archetype in dir: D:\mvnProject\mvntest1 46 [INFO] ------------------------------------------------------------------------ 47 [INFO] BUILD SUCCESS 48 [INFO] ------------------------------------------------------------------------ 49 [INFO] Total time: 4.352 s 50 [INFO] Finished at: 2019-11-09T16:02:27+08:00 51 [INFO] ------------------------------------------------------------------------
目录下查看项目,发现创建了一个web工程。
(2) 项目编译,进入项目目录,使用mvn compile命令,java源码和resources资源编译后的会在新生成的target目录下,其中target中有编译后的字节码文件。
1 D:\mvnProject\mvntest>mvn compile 2 [INFO] Scanning for projects... 3 [INFO] 4 [INFO] --------------------------< com.boe:mvntest >--------------------------- 5 [INFO] Building mvntest 1.0-SNAPSHOT 6 [INFO] --------------------------------[ jar ]--------------------------------- 7 [INFO] 8 [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mvntest -- 9 - 10 [INFO] Using 'UTF-8' encoding to copy filtered resources. 11 [INFO] skip non existing resourceDirectory D:\mvnProject\mvntest\src\main\resour 12 ces 13 [INFO] 14 [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mvntest --- 15 [INFO] Changes detected - recompiling the module! 16 [INFO] Compiling 1 source file to D:\mvnProject\mvntest\target\classes 17 [INFO] ------------------------------------------------------------------------ 18 [INFO] BUILD SUCCESS 19 [INFO] ------------------------------------------------------------------------ 20 [INFO] Total time: 2.519 s 21 [INFO] Finished at: 2019-11-09T16:18:23+08:00 22 [INFO] ------------------------------------------------------------------------
编译后目录。
(3) mvn test命令,会执行test源码中的方法,这里执行的是默认生成的方法。
src/test/AppTest.java文件内容。
1 package com.boe; 2 3 import junit.framework.Test; 4 import junit.framework.TestCase; 5 import junit.framework.TestSuite; 6 7 /** 8 * Unit test for simple App. 9 */ 10 public class AppTest 11 extends TestCase 12 { 13 /** 14 * Create the test case 15 * 16 * @param testName name of the test case 17 */ 18 public AppTest( String testName ) 19 { 20 super( testName ); 21 } 22 23 /** 24 * @return the suite of tests being tested 25 */ 26 public static Test suite() 27 { 28 return new TestSuite( AppTest.class ); 29 } 30 31 /** 32 * Rigourous Test :-) 33 */ 34 public void testApp() 35 { 36 assertTrue( true ); 37 } 38 }
执行mvn test命令结果。
# 进入项目目录进行测试
1 D:\mvnProject\mvntest>mvn test 2 [INFO] Scanning for projects... 3 [INFO] 4 [INFO] --------------------------< com.boe:mvntest >--------------------------- 5 [INFO] Building mvntest 1.0-SNAPSHOT 6 [INFO] --------------------------------[ jar ]--------------------------------- 7 [INFO] 8 [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mvntest -- 9 - 10 [INFO] Using 'UTF-8' encoding to copy filtered resources. 11 [INFO] skip non existing resourceDirectory D:\mvnProject\mvntest\src\main\resour 12 ces 13 [INFO] 14 [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mvntest --- 15 [INFO] Changes detected - recompiling the module! 16 [INFO] Compiling 1 source file to D:\mvnProject\mvntest\target\classes 17 [INFO] 18 [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mv 19 ntest --- 20 [INFO] Using 'UTF-8' encoding to copy filtered resources. 21 [INFO] skip non existing resourceDirectory D:\mvnProject\mvntest\src\test\resour 22 ces 23 [INFO] 24 [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mvntest 25 --- 26 [INFO] Changes detected - recompiling the module! 27 [INFO] Compiling 1 source file to D:\mvnProject\mvntest\target\test-classes 28 [INFO] 29 [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mvntest --- 30 [INFO] Surefire report directory: D:\mvnProject\mvntest\target\surefire-reports 31
# 测试后打印输出 T E S T S 32 ------------------------------------------------------- 33 T E S T S 34 ------------------------------------------------------- 35 Running com.boe.AppTest 36 Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec 37 38 Results : 39 40 Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 41 42 [INFO] ------------------------------------------------------------------------ 43 [INFO] BUILD SUCCESS 44 [INFO] ------------------------------------------------------------------------ 45 [INFO] Total time: 2.832 s 46 [INFO] Finished at: 2019-11-09T16:28:20+08:00 47 [INFO] ------------------------------------------------------------------------
测试完成后,target目录显示如下,其中编译后的class文件在test-classes目录下,而surefire-reports目录下保存的是测试后的结果。
surefire-reports中内容为测试结果,其中xml文件中的内容除了测试结果还包含更多的信息,如虚拟机名字,编码,时区,当前机器名字等等,txt中内容只有测试结果。
# com.boe.AppTest.txt文件内容
-------------------------------------------------------------------------------
Test set: com.boe.AppTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec
(4)打包命令mvn package, 这个命令最常用,如大数据hadoop写好的MapReduce程序打成jar包后,放到hdfs集群中运行,不过最好先执行mvn clean清除target目录下内容,防止对本次命令产生影响,执行这个命令会默认执行mvn test,mvn compile。
# 在项目目录下执行mvn package
1 D:\mvnProject\mvntest>mvn package 2 [INFO] Scanning for projects... 3 [INFO] 4 [INFO] --------------------------< com.boe:mvntest >--------------------------- 5 [INFO] Building mvntest 1.0-SNAPSHOT
# 打成jar包,这个是在项目pom.xml中通过packaging标签指定的,
# 此外还有war,pom等打包方式,其中war包一般用在tomcat中,pom用在SpringCloud微服务中创建父pom工程用 6 [INFO] --------------------------------[ jar ]--------------------------------- 7 [INFO] 8 [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mvntest -- 9 - 10 [INFO] Using 'UTF-8' encoding to copy filtered resources. 11 [INFO] skip non existing resourceDirectory D:\mvnProject\mvntest\src\main\resour 12 ces 13 [INFO] 14 [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mvntest --- 15 [INFO] Changes detected - recompiling the module! 16 [INFO] Compiling 1 source file to D:\mvnProject\mvntest\target\classes 17 [INFO] 18 [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mv 19 ntest --- 20 [INFO] Using 'UTF-8' encoding to copy filtered resources. 21 [INFO] skip non existing resourceDirectory D:\mvnProject\mvntest\src\test\resour 22 ces 23 [INFO] 24 [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mvntest 25 --- 26 [INFO] Changes detected - recompiling the module! 27 [INFO] Compiling 1 source file to D:\mvnProject\mvntest\target\test-classes 28 [INFO] 29 [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mvntest --- 30 [INFO] Surefire report directory: D:\mvnProject\mvntest\target\surefire-reports 31
# 可以看出进行的mvn test 32 ------------------------------------------------------- 33 T E S T S 34 ------------------------------------------------------- 35 Running com.boe.AppTest 36 Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.009 sec 37 38 Results : 39 40 Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 41 42 [INFO] 43 [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mvntest ---
# 打包成功,生成jar文件 44 [INFO] Building jar: D:\mvnProject\mvntest\target\mvntest-1.0-SNAPSHOT.jar 45 [INFO] ------------------------------------------------------------------------ 46 [INFO] BUILD SUCCESS 47 [INFO] ------------------------------------------------------------------------ 48 [INFO] Total time: 2.670 s 49 [INFO] Finished at: 2019-11-09T17:03:20+08:00 50 [INFO] ------------------------------------------------------------------------
(5)mvn install命令的效果,就是让当前工程打包资源后,变成本地库的一部分,交给了本地库管理,可以通过坐标获取项目中的jar包。
# 当前目录下执行命令
1 D:\mvnProject\mvntest>mvn install 2 [INFO] Scanning for projects... 3 [INFO] 4 [INFO] --------------------------< com.boe:mvntest >--------------------------- 5 [INFO] Building mvntest 1.0-SNAPSHOT 6 [INFO] --------------------------------[ jar ]--------------------------------- 7 [INFO] 8 [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mvntest -- 9 - 10 [INFO] Using 'UTF-8' encoding to copy filtered resources.
# 资源目录没有直接skip 11 [INFO] skip non existing resourceDirectory D:\mvnProject\mvntest\src\main\resour 12 ces 13 [INFO] 14 [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mvntest --- 15 [INFO] Changes detected - recompiling the module! 16 [INFO] Compiling 1 source file to D:\mvnProject\mvntest\target\classes 17 [INFO] 18 [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mv 19 ntest --- 20 [INFO] Using 'UTF-8' encoding to copy filtered resources. 21 [INFO] skip non existing resourceDirectory D:\mvnProject\mvntest\src\test\resour 22 ces 23 [INFO] 24 [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mvntest 25 --- 26 [INFO] Changes detected - recompiling the module!
# 可以看出执行了mvn compile 27 [INFO] Compiling 1 source file to D:\mvnProject\mvntest\target\test-classes 28 [INFO] 29 [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mvntest --- 30 [INFO] Surefire report directory: D:\mvnProject\mvntest\target\surefire-reports 31
# 可以看出执行了mvn test 32 ------------------------------------------------------- 33 T E S T S 34 ------------------------------------------------------- 35 Running com.boe.AppTest 36 Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.008 sec 37 38 Results : 39 40 Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 41 42 [INFO] 43 [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mvntest --- 44 [INFO] Building jar: D:\mvnProject\mvntest\target\mvntest-1.0-SNAPSHOT.jar 45 [INFO] 46 [INFO] --- maven-install-plugin:2.4:install (default-install) @ mvntest ---
# 安装jar资源到本地库 47 [INFO] Installing D:\mvnProject\mvntest\target\mvntest-1.0-SNAPSHOT.jar to d:\al 48 i_repo\com\boe\mvntest\1.0-SNAPSHOT\mvntest-1.0-SNAPSHOT.jar
# 安装pom文件到本地库 49 [INFO] Installing D:\mvnProject\mvntest\pom.xml to d:\ali_repo\com\boe\mvntest\1 50 .0-SNAPSHOT\mvntest-1.0-SNAPSHOT.pom 51 [INFO] ------------------------------------------------------------------------ 52 [INFO] BUILD SUCCESS 53 [INFO] ------------------------------------------------------------------------ 54 [INFO] Total time: 2.565 s 55 [INFO] Finished at: 2019-11-09T17:14:00+08:00 56 [INFO] ------------------------------------------------------------------------
可以在本地库找到install后的项目。
(6)mvn deploy是将本地资源发布到远程库,一般用在私服搭建上,本人暂时还没有用过。
Scope属性
scope属性是定义在pom.xml文件中的,了解它之前需要先了解如何在本地库或者远程库定位一个资源。答案是通过maven资源坐标,常见的表现形式如下,通过groupId,artifactId,version就可以在本地库或远程库唯一定位一个资源。scope标签的作用,是约束依赖资源使用的时机,即项目中导入的依赖不一定全生命周期都要使用,可以通过scope属性来限定。
1 <dependency> 2 <groupId>junit</groupId> 3 <artifactId>junit</artifactId> 4 <version>3.8.1</version> 5 <scope>test</scope> 6 </dependency>
scope常用的属性值有compile、test、runtime、provided,system,import等。
(1)compile:如果不指定默认为compile,作用在编译、打包、安装和发布阶段等全生命周期。
(2)test:作用在测试时,其他阶段依赖资源不生效。
(3)runtime:不参与编译,其他阶段都生效,一般用在JDBC,本来要运行时导入的包,避免在编译阶段导入。
(4)provided:只作用在编译阶段,如servlet相关的包,可以在编译阶段使用,但是实际运行时需要依赖tomcat提供的servlet,为了避免冲突,servlet相关的包必须scope属性设置为provided。
(5)system:当需要使用的资源没有提供坐标时使用,但是需要配置systemPath标签。
<groupId>cn.boe</groupId> <artifactId>mvntest</artifactId> <version>1.0</version> <scope>system</scope>
<!--配置systemPath,指定jar包位置--> <systemPath>D:\ali_repo\com\boe\mvntest\1.0-SNAPSHOT\mvntest-1.0-SNAPSHOT.jar</systemPath> <dependency> </dependency>
(6)import:用在dependencyManagement标签中,将目标pom的声明式配置和并到当前pom的声明式配置中,在配置springcloud时会用到,下面导入了springcloud需要的依赖。
1 <!--整合springcloud的依赖资源,使用import的方式--> 2 <dependencyManagement> 3 <dependencies> 4 <!--配置springcloud的依赖包,需要时才继承,不需要不继承--> 5 <dependency> 6 <groupId>org.springframework.cloud</groupId> 7 <artifactId>spring-cloud-dependencies</artifactId> 8 <version>Edgware.RELEASE</version> 9 <type>pom</type> 10 <scope>import</scope> 11 </dependency> 12 </dependencies> 13 </dependencyManagement>
依赖传递
maven中当指定了一个资源的坐标,通过dependency标签可以获取到对应的资源,但是有时候发现明明只有一个依赖,却导入了不止一个jar包,这是因为导入当前资源时,它可能还需要依赖其他别的资源,maven很聪明的将它们全部导进来了,这就是依赖的传递。那如果在使用的过程中不想某个子资源导入进来,需要配置exclusions标签,在里面指定不需要的子依赖。
1 <dependency> 2 <groupId>org.springframework</groupId> 3 <artifactId>spring-context</artifactId> 4 <version>4.1.5.RELEASE</version> 5 <exclusions> 6 <exclusion> 7 <!--不导入commons-logging的包--> 8 <groupId>org.springframework</groupId> 9 <artifactId>commons-logging</artifactId> 10 </exclusion> 11 </exclusions> 12 </dependency>
编译时发现没有导入commons-logging的包,exclusion标签生效。
1 D:\mvnProject\mvntest>mvn compile 2 [INFO] Scanning for projects... 3 [INFO] 4 [INFO] --------------------------< com.boe:mvntest >--------------------------- 5 [INFO] Building mvntest 1.0-SNAPSHOT 6 [INFO] --------------------------------[ jar ]--------------------------------- 7 Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/publ 8 ic/org/springframework/spring-context/4.1.5.RELEASE/spring-context-4.1.5.RELEASE 9 .pom 10 Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/publi 11 c/org/springframework/spring-context/4.1.5.RELEASE/spring-context-4.1.5.RELEASE. 12 pom (4.8 kB at 2.7 kB/s) 13 Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/publ 14 ic/org/springframework/spring-aop/4.1.5.RELEASE/spring-aop-4.1.5.RELEASE.pom 15 Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/publi 16 c/org/springframework/spring-aop/4.1.5.RELEASE/spring-aop-4.1.5.RELEASE.pom (2.7 17 kB at 7.9 kB/s) 18 Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/publ 19 ic/org/springframework/spring-beans/4.1.5.RELEASE/spring-beans-4.1.5.RELEASE.pom 20 21 Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/publi 22 c/org/springframework/spring-beans/4.1.5.RELEASE/spring-beans-4.1.5.RELEASE.pom 23 (2.5 kB at 6.4 kB/s) 24 Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/publ 25 ic/org/springframework/spring-core/4.1.5.RELEASE/spring-core-4.1.5.RELEASE.pom 26 Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/publi 27 c/org/springframework/spring-core/4.1.5.RELEASE/spring-core-4.1.5.RELEASE.pom (2 28 .5 kB at 6.6 kB/s) 29 Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/publ 30 ic/org/springframework/spring-expression/4.1.5.RELEASE/spring-expression-4.1.5.R 31 ELEASE.pom 32 Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/publi 33 c/org/springframework/spring-expression/4.1.5.RELEASE/spring-expression-4.1.5.RE 34 LEASE.pom (1.7 kB at 5.3 kB/s) 35 Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/publ 36 ic/org/springframework/spring-context/4.1.5.RELEASE/spring-context-4.1.5.RELEASE 37 .jar 38 Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/publ 39 ic/org/springframework/spring-expression/4.1.5.RELEASE/spring-expression-4.1.5.R 40 ELEASE.jar 41 Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/publ 42 ic/org/springframework/spring-aop/4.1.5.RELEASE/spring-aop-4.1.5.RELEASE.jar 43 Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/publ 44 ic/org/springframework/spring-beans/4.1.5.RELEASE/spring-beans-4.1.5.RELEASE.jar 45 46 Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/publ 47 ic/org/springframework/spring-core/4.1.5.RELEASE/spring-core-4.1.5.RELEASE.jar 48 Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/publi 49 c/org/springframework/spring-expression/4.1.5.RELEASE/spring-expression-4.1.5.RE 50 LEASE.jar (259 kB at 138 kB/s) 51 Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/publi 52 c/org/springframework/spring-aop/4.1.5.RELEASE/spring-aop-4.1.5.RELEASE.jar (359 53 kB at 180 kB/s) 54 Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/publi 55 c/org/springframework/spring-beans/4.1.5.RELEASE/spring-beans-4.1.5.RELEASE.jar 56 (709 kB at 254 kB/s) 57 Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/publi 58 c/org/springframework/spring-context/4.1.5.RELEASE/spring-context-4.1.5.RELEASE. 59 jar (1.0 MB at 351 kB/s) 60 Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/publi 61 c/org/springframework/spring-core/4.1.5.RELEASE/spring-core-4.1.5.RELEASE.jar (1 62 .0 MB at 335 kB/s) 63 [INFO] 64 [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mvntest -- 65 - 66 [INFO] Using 'UTF-8' encoding to copy filtered resources. 67 [INFO] skip non existing resourceDirectory D:\mvnProject\mvntest\src\main\resour 68 ces 69 [INFO] 70 [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mvntest --- 71 [INFO] Changes detected - recompiling the module! 72 [INFO] Compiling 1 source file to D:\mvnProject\mvntest\target\classes 73 [INFO] ------------------------------------------------------------------------ 74 [INFO] BUILD SUCCESS 75 [INFO] ------------------------------------------------------------------------ 76 [INFO] Total time: 8.148 s 77 [INFO] Finished at: 2019-11-09T18:49:37+08:00 78 [INFO] ------------------------------------------------------------------------
常用插件
maven提供的插件很多,如tomcat插件(一般在springboot用的多)、main插件、打源码插件等,均在pom.xml文件中设置。
(1)tomcat插件,可以配合IDEA一起使用,无需手动部署。
1 <!-- tomcat插件--> 2 <plugin> 3 <groupId>org.apache.tomcat.maven</groupId> 4 <artifactId>tomcat7-maven-plugin</artifactId> 5 <version>2.2</version> 6 <configuration> 7 <!-- 端口 --> 8 <port>8080</port> 9 <!-- contextPath --> 10 <path>/</path> 11 <!-- receive utf-8 --> 12 <uriEncoding>UTF-8</uriEncoding> 13 </configuration> 14 </plugin>
IDEA中在Command line中配置clean tomcat7:run就可以启动内嵌tomcat容器。
(2)main插件,打包后如果直接使用命令java -jar来运行jar包,会报错''没有主清单属性'',需要使用main插件来指定jar包的main方法入口。
指定main方法入口,这样配置后在jar包的manifest.mf文件中会增加一项内容,为Main-Class:com.boe.App。
1 <build> 2 <plugins> 3 4 <!--main插件--> 5 <plugin> 6 <groupId>org.apache.maven.plugins</groupId> 7 <artifactId>maven-jar-plugin</artifactId> 8 <configuration> 9 <archive> 10 <manifest> 11 <addClasspath>true</addClasspath> 12 <mainClass>com.boe.App</mainClass> 13 </manifest> 14 </archive> 15 </configuration> 16 </plugin> 17 18 </plugins> 19 </build>
manifest中增加的内容,如果不使用main插件,实际上打包后修改这个文件同样可以实现指定main方法入口。
再次执行jar包,OK。
(3)打源码插件,打包后可以看到源码内容。
1 <!--源码插件--> 2 <plugin> 3 <groupId>org.apache.maven.plugins</groupId> 4 <artifactId>maven-source-plugin</artifactId> 5 <version>3.0.1</version> 6 <configuration> 7 <attach>true</attach> 8 </configuration> 9 <executions> 10 <execution> 11 <phase>compile</phase> 12 <goals> 13 <goal>jar</goal> 14 </goals> 15 </execution> 16 </executions> 17 </plugin>
重新打包后多出了源码包。
IDEA中配置Maven
IDEA中配置Maven非常方便,快捷键Ctrl+Alt+S进入maven配置,指定maven安装目录,conf/settings.xml和本库仓库目录即可。
POM继承
POM支持继承,当定义一个父工程打包方式为pom时,这个父工程可以被其他子工程使用,子工程中只需要使用parent标签导入,就可以实现父工程同样的配置,这样子工程再添加自己独有的配置,就可以完成pom的设置,并且使用父pom文件便于管理多个子工程,保持信息一致。那么父工程的那些可以被子工程继承呢?
(1)groupId:子工程可以继承,可以自定义修改
(2)versionId:子工程可以继承,可以自定义修改
(3)properties:父工程定义的属性值和名称可以在子工程中使用
(4)dependency:父工程引用了那些依赖,子工程也将引用
(5)dependencyManagement:声明式依赖,子工程中不会自动直接导入,需要导入时需要在手动单独配置,不需要写版本,默认使用父pom文件中定义的版本,如果子工程指定了新的版本则使用新版本。
(6)build:配置的所有插件都可以继承
下面创建了一个父工程和子工程进行了验证测试。
父pom文件
<?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.boe</groupId> <artifactId>MavenProjectParent</artifactId> <version>1.0-SNAPSHOT</version> <!-- 测试pom的继承,需要将packaging修改为pom--> <packaging>pom</packaging> <!--1 groupid:子项目可继承--> <!--2 vesionid:子项目可以继承,可以修改--> <!--3 properties:子项目可以继承父工程中的--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!--junit版本--> <junit.version>3.8.1</junit.version> </properties> <!--4 dependency: 可以直接继承,父项目中依赖了资源,子工程中会直接传递过来--> <dependencies> <!-- springmvc包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.7.RELEASE</version> </dependency> </dependencies> <!--5 dependencyManagement:声明式依赖继承,子工程不会自动引入,需要时需要在子工程中手动导入, 不需要写版本号,默认使用父pom文件中的版本号,如果子工程中指定了版本号,则使用子工程中定义的版本号。 --> <dependencyManagement> <dependencies> <!-- 引入mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.8</version> </dependency> </dependencies> </dependencyManagement> <!--6 build:插件部分,子项目都可以继承--> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <!-- 端口 --> <port>8099</port> <!-- contextPath --> <path>/</path> <!-- receive utf-8 --> <uriEncoding>UTF-8</uriEncoding> </configuration> </plugin> </plugins> </build> </project>
子pom文件
<?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.boe</groupId>--> <artifactId>MavenProjectSon</artifactId> <!-- <version>1.0-SNAPSHOT</version>--> <!--继承父项目POM,使用parent标签--> <parent> <groupId>com.boe</groupId> <artifactId>MavenProjectParent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <!--验证dependencyManagement--> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--验证properties继承--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> </project>
测试发现:
(1)父pom文件中在properties中定义了junit.version,在子工程中可以使用,并且导入了junit的包,验证properties可以继承。
(2)父工程导入的spring-mvc的包,子工程也导入了,验证了dependency可以继承。
(3)定义在dependencyManagement中的mysql在父工程中并没有导入依赖包,在子工程中无需指定版本就导入了,验证了dependencyManagement可以继承。
(4)父pom文件中定义的tomcat插件,子工程中也继承了,验证了build也可以继承。
此外还有groupId和version没有验证,这两项在子pom文件中是没有指定的,本文使用install后查看本地库来验证,发现本地库中子工程和父工程都在com/boe下,因此groupId继承OK,另外jar包版本都是 1.0-snapshot,说明version继承也OK,以上验证均通过。
以上为maven入门知识,后续继续补充。
参考博文:
(1)https://www.jianshu.com/p/57eeac8860c0 maven入门