1 分钟:Installation
Maven is a Java tool, so you must have Java installed in order to proceed
First, download Maven and unzip it to your desired installation directory, for example C:\maven in windows, or /usr/local/maven in Linux. After this, add the system variable M2_HOME as well as the $M2_HOME/bin directory to your system path. Type the following in a terminal or command prompt:
It should print out your installed version of Maven, for example:
Depending upon your network setup, you may require extra configuration. Check out the Guide to Configuring Maven if necessary.
2 分钟:Creating a Project
On your command line, execute the following maven goal:
mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-app
If you have just installed Maven, it may take a while on the first run. This is because Maven is downloading the most recent artifacts (plugin jars and other files) into your local repository. You may also need to execute the command a couple of times before it succeeds. This is because the remote server may time out before your downloads are complete. Don't worry, there are ways to fix that.
You will notice that the create goal created a directory with the same name given as the artifactId. Change into that directory.
Under this directory you will notice the following standard project structure .
my-app
|-- pom.xml
`-- src
|-- main
| `-- java
| `-- com
| `-- mycompany
| `-- app
| `-- App.java
`-- test
`-- java
`-- com
`-- mycompany
`-- app
`-- AppTest.java
The src/main/java directory contains the project source code, the src/test/java directory contains the test source, and the pom.xml is the project's Project Object Model, or POM.
The POM
The pom.xml file is the core of a project's configuration in Maven. It is a single configuration file that contains the majority of information required to build a project in just the way you want. The POM is huge and can be daunting in its complexity, but it is not necessary to understand all of the intricacies just yet to use it effectively. This project's POM is:
- <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.0modelVersion>
- <groupId>com.mycompany.appgroupId>
- <artifactId>my-appartifactId>
- <packaging>jarpackaging>
- <version>1.0-SNAPSHOTversion>
- <name>Maven Quick Start Archetypename>
- <url>http://maven.apache.orgurl>
- <dependencies>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>3.8.1version>
- <scope>testscope>
- dependency>
- dependencies>
- project>
What did I just do?
You executed the Maven goal archetype:create , and passed in various parameters to that goal. The prefix archetype is the plugin that contains the goal. If you are familiar with Ant , you may concieve of this as similar to a task. This goal created a simple project based upon an archetype. Suffice it to say for now that a plugin is a collection of goals with a general common purpose. For example the jboss-maven-plugin, whose purpose is "deal with various jboss items".
3分钟:Build the Project
The command line will print out various actions, and end with the following:
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Thu Oct 05 21:16:04 CDT 2006
[INFO] Final Memory: 3M/6M
[INFO] ------------------------------------------------------------------------
Unlike the first command executed (archetype:create ) you may notice the second is simply a single word - package . Rather than a goal, this is a phase . A phase is a step in the build lifecycle , which is an ordered sequence of phases. When a phase is given, Maven will execute every phase in the sequence up to and including the one defined. For example, if we execute the compile phase, the phases that actually get executed are:
- validate
- generate-sources
- process-sources
- generate-resources
- process-resources
- compile
You may test the newly compiled and packaged JAR with the following command:
java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
Which will print the quintessential:
Hello World!
4分钟:Running Maven Tools
Maven Phases
Although hardly a comprehensive list, these are the most common default lifecycle phases executed.
- validate : validate the project is correct and all necessary information is available
- compile : compile the source code of the project
- test : test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
- package : take the compiled code and package it in its distributable format, such as a JAR.
- integration-test : process and deploy the package if necessary into an environment where integration tests can be run
- verify : run any checks to verify the package is valid and meets quality criteria
- install : install the package into the local repository, for use as a dependency in other projects locally
- deploy : done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
There are two other Maven lifecycles of note beyond the default list above. They are
- clean : cleans up artifacts created by prior builds
- site : generates site documentation for this project
Phases are actually mapped to underlying goals. The specific goals executed per phase is dependant upon the packaging type of the project. For example, package executes jar:jar if the project type is a JAR, and war:war is the project type is - you guessed it - a WAR.
An interesting thing to note is that phases and goals may be executed in sequence.
mvn clean dependency:copy-dependencies package
This command will clean the project, copy dependencies, and package the project (executing all phases up to package , of course).
Generating the Site
This phase generates a site based upon information on the project's pom. You can look at the documentation generated under target/site .
5分钟:Conclusion
We hope this quick overview has piqued your interest in the versitility of Maven. Note that this is a very truncated quick-start guide. Now you are ready for more comprehensive details concerning the actions you have just performed. Check out the Maven Getting Started Guide .
分享到:
相关推荐
maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装...
maven cook book from maven support.
maven 熟悉 官方文档 中文翻译, 经过5分钟的修炼就可以基本上对Maven上手了。
Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令
maven3.0maven3.0maven3.0maven3.0maven3.0maven3.0maven3.0maven3.0maven3.0maven3.0
Maven3.9.4版本压缩包,仅供学习参考,更新版本请前往Maven官方下载;Maven3.9.4版本压缩包,仅供学习参考,更新版本请前往Maven官方下载;Maven3.9.4版本压缩包,仅供学习参考,更新版本请前往Maven官方下载;Maven...
maven 工具 maven 工具 maven 工具 maven 工具 maven 工具
maven教程maven教程maven教程maven教程maven教程maven教程maven教程maven教程maven教程maven教程maven教程maven教程maven教程maven教程
apache-maven-3.0.5 apache-maven-3.1.1 apache-maven-3.2.5 apache-maven-3.3.9 apache-maven-3.5.4 apache-maven-3.6.3 apache-maven-3.8.5 每个版本包含4个文件: apache-maven-3.8.5-bin.tar.gz apache-maven-...
maven
Maven仓库apache-maven-3.2.5
Maven常用命令 Maven Maven学习
maven api
apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-...
Maven项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的项目管理工具软件。 Maven 除了以程序构建能力为特色之外,还提供高级项目管理工具。由于 Maven 的缺省构建规则有较高的可重用性,...
maven3.6maven3.6maven3.6
maven打包 maven打jar包详细步骤
maven教程,目录如下: Maven安装配置 Maven启用代理访问 Maven本地资源库 Maven中央存储库 如何从Maven远程存储库下载? Maven添加远程仓库 Maven依赖机制 定制库到Maven本地资源库 使用Maven创建Java项目 使用...
Maven最新版是款项目管理和构建自动化工具。Maven包含了一个项目对象模型 (Project Object Model),一组标准集合,一个项目生命周期(Project Lifecycle),一个依赖管理系统(Dependency Management System)。 安装...
maven资源包maven资源包maven资源包maven资源包maven资源包maven资源包maven资源包maven资源包maven资源包maven资源包