Maven in 5 Minutes(5分钟学会使用Maven)
2013-04-07 14:39
429 查看
Prerequisites(预备知识)
You must have an understanding of how to install software on your computer. If you do not know how to do this, please ask someone at your office, school, etc or pay someone to explain this to you. The Maven mailinglists are not the best place to ask for this advice.
--
你必须清楚怎么在你的计算机上安装软件。如果不清楚,请通过各种途径来弄清楚这个问题。通过发邮件给Maven并不是你解决此问题的最佳的方式。
Installation(安装)
Maven is a Java tool, so you must have Java installedin order to proceed.
First, download Maven and follow the installation
instructions. After that, type the following in a terminal or in a command prompt:
--
Maven是一个java工具,为了能够使用Maven,你的计算机必须安装java(jdk)。
首先,下载Maven安装包并按照安装说明完成各步骤。然后在终端或是在命令提示符下运行以下代码:
mvn --version
It should print out your installed version of Maven, for example:
--
它将会打印出你所安装的Maven的版本,例如:
Apache Maven 3.0.3 (r1075438; 2011-02-28 18:31:09+0100) Maven home: D:\apache-maven-3.0.3\bin\.. Java version: 1.6.0_25, vendor: Sun Microsystems Inc. Java home: E:\Program Files\Java\jdk1.6.0_25\jre Default locale: nl_NL, platform encoding: Cp1252 OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
Depending upon your network setup, you may require extra configuration. Check out the Guide
to Configuring Maven if necessary.
If you are using Windows, you should look at Windows Prerequisites to
ensure that you are prepared to use Maven on Windows.
--
根据你的网路设置,你可能需要其他的配置。如果必要的话请查看Maven配置指南。
如果你正在使用的是Windows系统,你应该先查看【Windows
Prerequisites】来确保你已经做好了在Windows系统上使用Maven的准备工作。
Creating a Project(创建一个项目)
You will need somewhere for your project to reside, create a directory somewhere and start a shell in that directory. On your command line, execute the following Maven goal:--
你需要一个保存项目的文件夹,在任意路径下创建一个文件夹并在该目录下启动shell。在命令行执行下面的Maven命令:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
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.
--
如果你仅仅只是安装了Maven,在第一次运行时将会需要等待一段时间。这是因为Maven正在下载最新的组件(插件,jar包,其他文件)到你本地的仓库。在运行成功之前,也有可能你需要多次执行以上命令。这是因为在你下载完成之前远程服务器能会响应超时。不用担心,有方法来解决这个问题。
You will notice that the generate goal created a directory with the same name given as the artifactId. Change into that directory.
--
你将会发现这个生成的目标会创建一个与你给定的artifactId的值同名的目录。进入这个目录。
cd my-app
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 file is the project's Project Object Model, or POM.
--
src/main/java目录包含了项目的源码, src/test/java 目录包含了测试来源,pom.xml文件时项目的【项目对象模型】或称为【POM】
The POM(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 POMis 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:
--
在Maven中,pom.xml文件是一个项目配置的核心。它是一个单一的配置文件,它按你的需要包含了建立一个项目的大量信息。POM是庞大的,它的复杂性也是令人生畏的,但是你没有必要去弄懂它所有的错综复杂的东西,只需要有效地运用就行了。这个项目的POM是:
<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.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>Maven Quick Start Archetype</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> </dependencies> </project>
What did I just do?(我刚才做了什么?)
You executed the Maven goal archetype:generate, and passed in various parameters to that goal. The prefix archetype is the plugin thatcontains the goal. If you are familiar with Ant, you may conceive 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".
--
你执行了Maven目标archetype:generate ,并在各种参数时达到这一目标。前缀原型是包含了这个目标的插件。如果您熟悉Ant,你可以想象这是类似于一个任务。这个目标基于一个原型创建了一个简单的项目。我只想说,现在一个插件是一个一般常见的目的收集器。例如jboss
maven插件,其目的是“处理各种jboss项目”。
Build the Project(构建项目)
mvn package
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 Jul 07 21:34:52 CEST 2011 [INFO] Final Memory: 3M/6M [INFO] ------------------------------------------------------------------------
Unlike the first command executed (archetype:generate) 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:
--
不像第一次执行的命令,你会发现第二个命令只是一个单词 - package。这是一个阶段而不是一个目标。一个有序的阶段是构建生命周期的一个步骤。当一个阶段被给定后,Maven将执行序列中的每一个阶段,包括一个定义。例如,如果我们执行compile 阶段,这个阶段将真正得到执行的是:
验证
生成来源
处理来源
生成资源
处理资源
编译
你使用以下命令测试新编译和打包的JAR:
java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
Which will print the quintessential:
--
得到的打印结果:
Hello World!
Running Maven Tools(运行Maven工具)
Maven Phases(Maven 阶段)
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.
--
尽管几乎没有一个全面的列表,这些都是最常见的缺省生命周期阶段执行的。
验证: 验证项目是否正确,所有必须的信息是否可用。
编译: 编译项目的源代码。
测试: 使用合适的单元测试框架测试编译的源代码. 这些测试不应该要求代码被打包或者被部署。
打包: 把编译的代码打包成可分配的格式, 例如: JAR。
集成测试:
如果必要的话,处理和部署包到一个集成测试可以运行的环境中。
核实: 执行一些检查,确认包是有效的和符合质量标准的。
安装: 安装包到本地仓库, 作为本地其他项目的一个依赖项。
部署: 部署到一个集成或者释放的环境中, 拷贝最终版本的包到远程仓库, 来与其他开发者和项目共享。
这里有两个不在以上默认列表之列的Maven生命周期。他们是
清除: 清除事先构建的组件。
站点: 为此项目生成站点说明文档。
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(生成站点)
mvn site
This phase generates a site based upon information on the project's pom. You can look at the documentation generated under target/site.
--
这个阶段,基于项目pom信息生成了一个站点。你能够在target/site目录下查看生成的文档。
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 youhave just performed. Check out the Maven Getting Started Guide.
--
希望这个简要概述已经激起了你对Maven多功能性的兴趣。注意,这个一个非常简短的快速开始的指南。关于你刚刚执行的一系列操作,现在你准备来了解更全面的细节。请阅读Maven入门指南。
【英语水平超烂,勉强翻译出来了,凑合着看吧!!】
相关文章推荐
- maven:使用mvn archetype:generate创建项目,停留在 "Generating project in Batch mode"
- 5分钟学会使用Less预编译器
- 5分钟学会 Thymeleaf 的基本使用
- 五分钟学会HTML5 [Learn HTML5 in 5 Minutes!]
- Maven 使用Intellij IDEA部署添加Maven Module出现 'pom.xml' already exists in VFS
- 5分钟学会使用DataHub接入实时数据到MaxCompute(原ODPS)
- JAVA WEB 工程 SSH框架在Jenkins中打包前使用IDFC Maven ProGuard Plug-in混淆的错误的解决
- 使用maven构建 ssm项目 tomcat7插件运行报错, Invalid byte tag in constant pool: 60
- maven使用命令创建项目失败[ERROR] Could not find goal 'create' in plugin org.apache.maven.plugins:maven-archety
- Eclipse和Maven的结合使用--Please make sure the -vm option in eclipse.ini
- Teach Yourself Drupal in 24 Minutes -Minute1 使用Drupal
- Maven使用心得之debug in eclipse
- 5分钟学会使用DataHub接入实时数据到MaxCompute(原ODPS)
- Teach Yourself Drupal in 24 Minutes –Minute5 使用Drupal Nodes
- Teach Yourself Drupal in 24 Minutes –Minute12 使用Blogs和Newsletters
- 在使用eclipse 运行 maven install的时候,出现这个错误:Unable to locate the Javac Compiler in:…
- maven-in-five-minutes
- 快速入门:十分钟学会PythonTutorial - Learn Python in 10 minutes
- Maven in 5 Minutes(Windows)
- Eclipse和Maven的结合使用--Please make sure the -vm option in eclipse.ini