Maven介绍,包括作用、核心概念、用法、常用命令、扩展及配置
Maven库:
Maven依赖查询:
【Maven常用命令】(转自:)
1. 创建Maven的普通java项目:
有下面几种不同的DarchetypeArtifactId
1.省略默认是maven-archetype-quickstart
mvn archetype:generate -DgroupId=com.xxxx.xxxx -DartifactId=test-project -DarchetypeCatalog=internal -DarchetypeArtifactId=maven-archetype-quickstart
工程目录结构
2.maven-archetype-archetype 包含一个archetype的例子,主要用于当我们要建立自己的archetype的时候
工程目录结构
工程目录结构
4.maven-archetype-plugin 包含一个Maven plugin的例子
工程目录结构
5.maven-archetype-plugin-site 包含一个Maven plugin site的例子
6.Maven-archetype-portlet 包含一个portlet的例子打包失败
7.maven-archetype-simple 包含一个简单maven项目创建失败
8.Maven-archetype-site 包含一个maven site的例子,它能够展示一些支持的文档类型,包括APT、XDoc和FML工程结构目录
9.Maven-archetype-site-simple 包含一个maven site的例子
工程目录结构
2. 创建Maven的Web项目: mvn archetype:create -DgroupId=packageName -DartifactId=webappName -DarchetypeArtifactId=maven-archetype-webapp 3. 清理编译:mvn clean compile4. 清理测试:mvn clean test5. 清理打包:mvn clean package
6. 带有依赖打包:mvn package assembly:single
7.指定编译版本的打包:mvn package -Dmaven.test.skip=true -Dmaven.compiler.source=1.8 -Dmaven.compiler.target=1.88. 清理安装,会安装到~/.m2/目录下面:mvn clean install9. 生成eclipse项目:mvn eclipse:eclipse 10. 生成idea项目:mvn idea:idea 12. 编译测试的内容:mvn test-compile 13. 只打jar包: mvn jar:jar 14. 跳过测试: mvn install -Dmaven.test.skip=true 15. 指定端口: mvn -Dmaven.tomcat.port=909016. 只测试而不编译,也不测试编译:mvn test -skipping compile -skipping test-compile ( -skipping 的灵活运用,当然也可以用于其他组合命令) 17. 清除eclipse的一些系统设置: mvn eclipse:clean
18.打包依赖:mvn assembly:assembly,使用后执行成功后会在target文件夹下多出一个以-jar-with-dependencies结尾的JAR包. 这个JAR包就包含了项目所依赖的所有JAR的CLASS.
通过cvs或svn下载代码到本机,然后执行mvn eclipse:eclipse生成ecllipse项目文件,然后导入到eclipse就行了;修改代码后执行mvn compile或mvn test检验,也可以下载eclipse的maven插件。
发布第三方Jar到本地库中: mvn install:install-file -DgroupId=com -DartifactId=client -Dversion=0.1.0 -Dpackaging=jar -Dfile=d:\client-0.1.0.jar。
19.添加 jar 包到本地仓库
mvn install:install-file
-DgroupId=xxx -DartifactId=xxx -Dversion=1.0 //版本号 -Dpackaging=jar //类型 -Dfile=d:\xxx-1.0.jar //jar实际路径mvn install:install-file -Dfile=jar包的位置 -DgroupId=上面的groupId -DartifactId=上面的artifactId -Dversion=上面的version -Dpackaging=jar
20.如果遇到mvn package 内存爆掉的情况,增加内存,然后mvn -version查看内存是多少
export MAVEN_OPTS="-Xmx512m -XX:MaxPermSize=128m"
添加打包插件:
maven-compiler-plugin 1.6 1.6 UTF-8 src\main\webapp\WEB-INF\lib
【库版本选择】
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>[1.1,)</version>
</dependency>
表达式 含义
(,1.0] version<=1.0
[1.2,1.3] 1.2<=version<=1.3
[1.0,2.0) 1.0<=version<2.0
[1.5,) 1.5<=version
(,1.1),(1.1,) version!=1.1
【pom.xml 基本节点】
<project> 根节点
<modelversion> pom.xml 使用的对象模型版本
<groupId> 创建项目的组织或团体的唯一 Id
<artifactId> 项目唯一Id, 项目名
<packaging> 打包扩展名(JAR、WAR、EAR)
<version> 项目版本号
<name> 显示名,用于生成文档
<url> 组织站点,用于生成文档
<description> 项目描述,用于生成文档
<dependency>之<scope> 管理依赖部署
<scope> 可使用 5 个值:
compile 缺省值,用于所有阶段,随项目一起发布
provided 期望JDK、容器或使用者提供此依赖。如servlet.jar
runtime 只在运行时使用
test 只在测试时使用,不随项目发布
system 需显式提供本地jar,不在代码仓库中查找
自用maven的setting.xml配置文件
nexus-releases admin xxx nexus-snapshots admin xxx mynexus mynexus My Nexus http://tencent:40000/nexus/content/repositories/thirdparty/ repo2 central Human Readable Name for this Mirror. http://repo2.maven.org/maven2/ nexus-aliyun aliyun Nexus aliyun http://maven.aliyun.com/nexus/content/groups/public ui ui Human Readable Name for this Mirror. http://uk.maven.org/maven2/ ibiblio ibiblio Human Readable Name for this Mirror. http://mirrors.ibiblio.org/pub/mirrors/maven2/ nexus public Public Repositories http://tencent:40000/nexus/content/repositories/thirdparty public Public Repositories http://tencent:40000/nexus/content/repositories/thirdparty nexus