本文介绍了安装Maven以及使用Maven创建第一个工程的入门文章。(笔者也是刚刚接触maven,不当之处,请指正)
1. 首先进入Apache Maven的网站,http://maven.apache.org,在Maven网站的主页面中下载Maven的bin文件,下载后解压缩到本地目录,解压後的路径记作${MAVEN_HOME},例如:C:\maven-2.0.7,将${MAVEN_HOME}\bin设置到系统的path环境变量中。
启动一个命令行提示符,输入mvn –version,如果安装正确,将显示maven的版本信息,根据平台及系统环境的不同,显示一些附加消息,例如:
C:\>mvn --version
Maven version: 2.0.7
Java version: 1.5.0_09
OS name: "windows 2000" version: "5.0" arch: "x86"
2. 查看Maven命令的帮助信息(mvn --help)
在命令行提示符中输入mvn –help,将打印命令帮助信息:
C:\maven-2.0.7\sample>mvn --help
usage: mvn [options] [<goal(s)>] [<phase(s)>]
Options:
-q,--quiet 静态输入,只显示错误信息
-C,--strict-checksums 结束构建,如果checksums不匹配
-c,--lax-checksums 警告,如果checksums不匹配
-P,--activate-profiles 激活以分号分隔的profiles列表
-ff,--fail-fast 在重构过程中第一个失败的地方停止
-fae,--fail-at-end 只在构建后失败,允许所有未受影响的构建继续执行
-B,--batch-mode 以批处理的模式运行
-fn,--fail-never 从不使构建失败
-up,--update-plugins Synonym for cpu
-N,--non-recursive 不遍历子工程
-npr,--no-plugin-registry 不使用~/.m2/plugin-registry.xml文件作为插件版本
-U,--update-snapshots 强制进行远程存储仓库更新和缩略检查
-cpu,--check-plugin-updates 强制相关注册插件的更新检查
-npu,--no-plugin-updates 不强制相关注册插件的更新检查
-D,--define 定义系统属性
-X,--debug 生成操作的debug日志
-e,--errors 生成操作的错误信息
-f,--file 强制使用可变POM文件
-h,--help 显示帮助信息
-o,--offline 脱机工作
-r,--reactor 执行项目的目标
-s,--settings 修改用户配置文件路径
-v,--version 显示版本信息
其中对应的英文帮助如下,请参考:
C:\maven-2.0.7\sample>mvn --help
usage: mvn [options] [<goal(s)>] [<phase(s)>]
Options:
-q,--quiet Quiet output - only show errors
-C,--strict-checksums Fail the build if checksums don't match
-c,--lax-checksums Warn if checksums don't match
-P,--activate-profiles Comma-delimited list of profiles to
activate
-ff,--fail-fast Stop at first failure in reactorized builds
-fae,--fail-at-end Only fail the build afterwards; allow all
non-impacted builds to continue
-B,--batch-mode Run in non-interactive (batch) mode
-fn,--fail-never NEVER fail the build, regardless of project
result
-up,--update-plugins Synonym for cpu
-N,--non-recursive Do not recurse into sub-projects
-npr,--no-plugin-registry Don't use ~/.m2/plugin-registry.xml for
plugin versions
-U,--update-snapshots Forces a check for updated releases and
snapshots on remote repositories
-cpu,--check-plugin-updates Force upToDate check for any relevant
registered plugins
-npu,--no-plugin-updates Suppress upToDate check for any relevant
registered plugins
-D,--define Define a system property
-X,--debug Produce execution debug output
-e,--errors Produce execution error messages
-f,--file Force the use of an alternate POM file.
-h,--help Display help information
-o,--offline Work offline
-r,--reactor Execute goals for project found in the
reactor
-s,--settings Alternate path for the user settings file
-v,--version Display version information
3. Maven本地配置文件模型
用户自定义的maven配置文件,包含不需要和pom.xml文件一起发布的内容,例如开发者标志,本地设置(例如代理设置)。设置文件的默认位置为~/.m2/settings.xml,其中~表示用户主目录。(windows中为C:\Documents and Settings\Administrator)。Settings文件格式如下:
<settings>
<localRepository/>本地存储目录Default: ~/.m2/repository
<interactiveMode/>Maven是否可以和用户交互Default: true
<usePluginRegistry/>Maven是否使用plugin-registry.xml来管理插件
<offline/>决定Maven是否连接网络,这将影响到artifact的下载,发布等Default: false
<proxies>代理列表,用来连接Internet
<proxy>
<active/>是否为活动的true
<protocol/>协议http
<username/>用户名proxyuser
<password/>用户密码proxypass
<port/>端口80
<host/>主机…
<nonProxyHosts/>不需要使用代理的主机名,一般为局域网的地址
<id/>可选参数
</proxy>
</proxies>
<servers>服务器设置,主要是授权的方法
<server>
<username/>用户名
<password/>密码
<privateKey/>私钥路径/path/to/private/key
<passphrase/> 可选的,如果不使用保留为空
<filePermissions/>生成文件的权限
<directoryPermissions/>生成目录的权限
<configuration/>针对传输层的附加协议
<id/>标志号
</server>
</servers>
<mirrors> 一系列镜像地址用于从远程存储位置下载artifacts
<mirror>
<mirrorOf/> 存储仓库服务器ID
<name/>可选的名字,用于描述服务器
<url/>存储仓库服务器URL地址
<id/>
</mirror>
</mirrors>
<profiles>可以通过不同方式激活的一系列可以修改构建过程的profile文件,
<profile>
<activation>条件逻辑
<activeByDefault/>是否默认激活profile
<jdk/>匹配jdk存在时profile被激活
<os>匹配OS存在时profile被激活
<name/>OS名称
<family/>OS家族
<arch/>OS框架
<version/>OS版本
</os>
<property>系统属性匹配时,激活profile
<name/>名称
<value/>值
</property>
<file>文件存在时profile被激活
<missing/>应该缺失的文件名
<exists/>应该存在的文件名
</file>
</activation>
<properties/>扩展属性文件,内容格式为key.value
<repositories>远程存储位置
<repository>
<releases>如何处理释放版本下载
<enabled/>是否使用reposity下载这种类型的artifact
<updatePolicy/>下载更新的频率"always", "daily" (default), "interval:XXX" (in minutes) or "never" (only if it doesn't exist locally).
<checksumPolicy/>验证artifact失败是的处理:"fail" or "warn"
</releases>
<snapshots>如何处理临时版本下载
<enabled/>是否使用reposity下载这种类型的artifact
<updatePolicy/>下载更新的频率"always", "daily" (default), "interval:XXX" (in minutes) or "never" (only if it doesn't exist locally).
<checksumPolicy/>验证artifact失败是的处理:"fail" or "warn"
</snapshots>
<id/>reposity的唯一标志符
<name/> repository的易于理解的名称
<url/>repository的URL
<layout/>“legacy”或者“default”
</repository>
</repositories>
<pluginRepositories>插件存储位置
<pluginRepository>
<releases>
<enabled/>
<updatePolicy/>
<checksumPolicy/>
</releases>
<snapshots>
<enabled/>
<updatePolicy/>
<checksumPolicy/>
</snapshots>
<id/>
<name/>
<url/>
<layout/>
</pluginRepository>
</pluginRepositories>
<id/>
</profile>
</profiles>
<activeProfiles/>
<pluginGroups/>
</settings>
4. 修改${MAVEN_HOME}\conf\settings.xml文件的内容,设置以下内容:
<localRepository>C:/maven-2.0.7/sample/.m2/repository</localRepository>
<offline>true</offline>
5. 创建一个Project
在命令行窗口输入
mvn –o archetype:create -DgroupId=com.mycompany.app -DartifactId=my-app
命令行控制台打印输出信息如下:
C:\maven-2.0.7\sample>mvn -o archetype:create -DgroupId=com.mycompany.app -Darti
factId=my-app
[INFO]
NOTE: Maven is executing in offline mode. Any artifacts not already in your local
repository will be inaccessible.
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] -------------------------------------------------------------------------
---
[INFO] Building Maven Default Project
[INFO] task-segment: [archetype:create] (aggregator-style)
[INFO] -------------------------------------------------------------------------
---
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus
.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] **************************************************************
[INFO] Starting Jakarta Velocity v1.4
[INFO] RuntimeInstance initializing.
[INFO] Default Properties File: org\apache\velocity\runtime\defaults\velocity.pr
operties
[INFO] Default ResourceManager initializing. (class org.apache.velocity.runtime.
resource.ResourceManagerImpl)
[INFO] Resource Loader Instantiated: org.codehaus.plexus.velocity.ContextClassLo
aderResourceLoader
[INFO] ClasspathResourceLoader : initialization starting.
[INFO] ClasspathResourceLoader : initialization complete.
[INFO] ResourceCache : initialized. (class org.apache.velocity.runtime.resource.
ResourceCacheImpl)
[INFO] Default ResourceManager initialization complete.
[INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Literal
[INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Macro
[INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Parse