01-Maven教程
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了01-Maven教程相关的知识,希望对你有一定的参考价值。
1.Maven优势
1)项目自动化编译部署
开发项目过程:
上述过程是重复的.maven可以最大化的消除重复,简化了重复的过程,这只是要一个简单的命令.
2)项目jar包的依赖管理
3)项目的插件管理
2.Maven介绍
maven是一个基于项目对象模型(POM,maven项目的一个总的配置文件)构建和管理的工具,可以方便的编译代码、进行依赖管理、插件集成,
有利于项目过程规范化、自动化、高效化.
3.安装Maven
注意:maven-3.3及以上版本需要JDK 1.7以上的支持.
2)环境变量配置MAVEN_HOME
3)验证是否安装成功 mvn -v
4.maven项目约定
项目约定主要是规范开发人员编程,统一项目风格,简化操作.
指自己有固定的文件目录及对应目录的文件输出和文件配置的输出.
约定大于配置 => 简化配置
Maven项目结构:
5.Maven的核心知识
项目骨架:项目骨架是根据maven项目约定的规则来创建项目目录结构和资源文件.
构建项目命令:
mvn archetype:generate //archetype [‘ɑ?k?ta?p]:原型 ;generate [‘d?en?re?t]:生成,产生
-DgroupId=组织名称
-DartifactId=项目名称
-Dversion=版本号
-Dpackage=包路径
实例:
打开CMD,这里统一将项目放到F:\\mavenDemo目录下,切换到F:\\mavenDemo,执行如下命令:
C:\\Users\\chj03>F:
F:\\>cd mavenDemo
F:\\mavenDemo> //cls清屏 ;dir <path>列出path路径下的文件及目录 ;
创建maven项目:
1)与用户交互的方式创建:
通过网络:
// maven从官网下载项目骨架目录
F:\\mavenDemo>mvn archetype:generate //与用户交互式创建maven项目
[INFO]Scanningfor projects...
...
Choose a number or apply filter (format:[groupId:]artifactId,case sensitive contains):777: //通过
maven-archetype-quickstart(777)创建普通的maven项目,这是默认的Choose org.apache.maven.archetypes:maven-archetype-quickstart version://选择maven-archetype-quickstart version的版本,直接回车,
1:1.0-alpha-1
2:1.0-alpha-2
3:1.0-alpha-3
4:1.0-alpha-4
5:1.0
6:1.1
Choose a number:6://回车
...
Define value for property ‘groupId‘:: com.chenjun.demo //组织名称
Define value for property ‘artifactId‘:: maven-01 //项目名称
Define value for property ‘version‘:1.0-SNAPSHOT::1.0-SNAPSHOT //版本号
Define value for property ‘package‘: com.chenjun.demo:: com.chenjun.demo //包的路径
Confirm properties configuration: //确认上述输入是否正确,确定无误输入Y并回车
groupId: com.chenjun.demo
artifactId: maven-01
version:1.0-SNAPSHOT
package: com.chenjun.demo
Y:: Y
[INFO]----------------------------------------------------------------------------
[INFO]Using following parameters for creating project from Old(1.x)Archetype: maven-archetype-quickstart:1.1
[INFO]----------------------------------------------------------------------------
[INFO]Parameter: groupId,Value: com.chenjun.demo
[INFO]Parameter: packageName,Value: com.chenjun.demo
[INFO]Parameter: package,Value: com.chenjun.demo
[INFO]Parameter: artifactId,Value: maven-01
[INFO]Parameter: basedir,Value: F:\\mavenDemo
[INFO]Parameter: version,Value:1.0-SNAPSHOT
[INFO] project created from Old(1.x)Archetypein dir: F:\\mavenDemo\\maven-01
[INFO]------------------------------------------------------------------------
[INFO] BUILD SUCCESS //表示创建成功
[INFO]------------------------------------------------------------------------
[INFO]Total time:59:57 min
[INFO]Finished at:2016-05-13T12:27:21+08:00
[INFO]FinalMemory:12M/129M
[INFO]------------------------------------------------------------------------
通过插件:
//骨架目录结构不通过远程创建,通过插件内部目录结构创建,快速
F:\\mavenDemo\\train>mvn archetype:generate -DarchetypeCatalog=internal //走本地骨架的目录
[INFO]Scanningfor projects...
[INFO]
[INFO]------------------------------------------------------------------------
[INFO]BuildingMavenStubProject(No POM)1
[INFO]------------------------------------------------------------------------
[INFO]
[INFO]>>> maven-archetype-plugin:2.4:generate (default-cli)> generate-sources @ standalone-pom >>>
[INFO]
[INFO]<<< maven-archetype-plugin:2.4:generate (default-cli)< generate-sources @ standalone-pom <<<
[INFO]
[INFO]--- maven-archetype-plugin:2.4:generate (default-cli)@ standalone-pom ---
[INFO]Generating project inInteractive mode
[INFO]No archetype defined.Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: internal -> org.apache.maven.archetypes:maven-archetype-archetype (An archetype which contains a sample archetype.)
2: internal -> org.apache.maven.archetypes:maven-archetype-j2ee-simple (An archetype which contains a simplifed sample J2EE application.)
3: internal -> org.apache.maven.archetypes:maven-archetype-plugin (An archetype which contains a sample Maven plugin.)
4: internal -> org.apache.maven.archetypes:maven-archetype-plugin-site (An archetype which contains a sample Maven plugin site.
This archetype can be layered upon an existing Maven plugin project.)
5: internal -> org.apache.maven.archetypes:maven-archetype-portlet (An archetype which contains a sample JSR-268Portlet.)
6: internal -> org.apache.maven.archetypes:maven-archetype-profiles ()
7: internal -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.)
8: internal -> org.apache.maven.archetypes:maven-archetype-site (An archetype which contains a sample Maven site which demonstrates
some of the supported document types like APT,XDoc, and FML and demonstrates how
to i18n your site.This archetype can be layered upon an existing Maven project.)
9: internal -> org.apache.maven.archetypes:maven-archetype-site-simple (An archetype which contains a sample Maven site.)
10: internal -> org.apache.maven.archetypes:maven-archetype-webapp (An archetype which contains a sample MavenWebapp project.)
Choose a number or apply filter (format:[groupId:]artifactId,case sensitive contains):7: //默认,回车
Define value for property ‘groupId‘:: com.sohu.train //组织名称
Define value for property ‘artifactId‘:: maven-01 //项目名称
Define value for property ‘version‘:1.0-SNAPSHOT::1.0-SNAPSHOT //版本号
Define value for property ‘package‘: com.sohu.train:: com.sohu.train //包的路径
Confirm properties configuration:
groupId: com.sohu.train
artifactId: maven-01
version:1.0-SNAPSHOT
package: com.sohu.
以上是关于01-Maven教程的主要内容,如果未能解决你的问题,请参考以下文章