point1:<dependency>依赖范围
<scope/>中进行配置
--compile:默认配置,对编译/测试/运行三种都有效。
--test:只对测试有效。
--runtime:测试和运行时有效,编译时无效。比如要使用JDBC驱动时,只在运行时使用它就好。
--provided:编译和测试时有效,运行时无效。举个场景,在有的web项目中,会用到servlet-api这个依赖,在本地编译和测试都没有问题,但是当把war包部署到tomcat时,会报错:
Caused by: java.lang.LinkageError: loader constraint violation: loader (instance of org/apache/catalina/loader/WebappClassLoader) previously initiated loading for a different type with name "javax/servlet/ServletContext" at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:800) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) at java.net.URLClassLoader.access$100(URLClassLoader.java:71) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
产生的原因就是tomcat也有这个servlet-api,所以添加:
<dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> <scope>provided</scope> </dependency>
可以让servlet-api只在编译和测试时有效,而当tomcat启动后就不生效,非常方便。
----------------------------------------------------------------------------------
point2:effective-pom
--并不是说我们自己pom.xml文件中所定义的依赖就是整个工程的依赖,它还包含了超级pom中的依赖(安装包中的lib下maven-model-builder-3.0.5.jar中规定了核心的依赖),所以真正生效的pom是我们自定义和超级pom以及可能的其他pom(父pom)等的叠加。by the way,在超级pom中还定义了中央库的位置,所以默认的settings.xml中并不用指定到哪里下载这些依赖。
----------------------------------------------------------------------------------
point3:mvn site
这可以生成一个站点,包含了此项目的一些信息,比较常见的mvn cobertura:cobertura可以将项目的代码覆盖率统计并展示出来,进入site目录,点开index.html可以看到:
----------------------------------------------------------------------------------
point4:打包命令--
maven提供了完备的打包机制。assembly插件常用的套件描述符包括
bin----将jar包打zip包
jar-with-dependencies----带有依赖的jar包
project----将工程源代码打包
src----将工程src目录下的源代码打包
E:UsersBruceChanworkspacedev01TestMaven>mvn assembly:single -DdescriptorId=project [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for com.changjiang.test:TestMaven:jar:0.0.1-SNAPSHOT [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-jar-plugin is missing. @ line 67, 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 TestMaven 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-assembly-plugin:2.2-beta-5:single (default-cli) @ TestMaven --- [INFO] Building tar : E:UsersBruceChanworkspacedev01TestMaven argetTestMaven-0.0.1-SNAPSHOT-project.tar.gz [INFO] Building tar : E:UsersBruceChanworkspacedev01TestMaven argetTestMaven-0.0.1-SNAPSHOT-project.tar.bz2 [INFO] Building zip: E:UsersBruceChanworkspacedev01TestMaven argetTestMaven-0.0.1-SNAPSHOT-project.zip [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 8.702s [INFO] Finished at: Thu Aug 25 10:05:22 CST 2016 [INFO] Final Memory: 20M/244M [INFO] ------------------------------------------------------------------------