Maven in 5 Minutes

仅供自己使用,
原文:http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

Prerequisites


Installation

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。在命令行执行如下:
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组件到本地仓库,你可能得执行好多次。因为远程服务可能响应超时在你下载的时候。别担心,有方法修复它。
You will notice that the generate goal created a directory with the same name given as the artifactId. Change into that directory.
你会看到创建一个和artifactId一样名字的文件夹。切到这个目录下。
Under this directory you will notice the following standard project structure.
你会看到一个标准的工程结构
xxxx
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是工程对象模型文件。
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:

它是一个单一的配置文件中包含建立一个项目的大部分信息,但是没必要理解全部复杂内容,这个工程的POM文件:
xxxx
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 that contains 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".

刚才都做了什么
你执行了MAVENxxx,并给这个goal传入了几个参数, archetype前缀是包含这个goal的插件,如果你熟悉Ant,你可能才行这是一个任务。这个goal创建了一个简单的工程基于一个原型。
可以确切的说一个插件是一些有共同目标的goals的集合。例如JBoss Maven插件,其目的是“处理各种JBoss项目”。
Build the Project
构建工程
mvn package
The command line will print out various actions, and end with the following:
这个命令输出各种操作,以下面结尾:
xxx
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:
不像第一个命令,你发现只是一个简单的单词,不是一个goal,是一个短语。一个短语是构建生命周期的一步。构建生命周期是一个短语序列。xxx例如,如果我们执行compile短语,实际上执行了:
  1. validate
  2. generate-sources
  3. process-sources
  4. generate-resources
  5. process-resources
  6. 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!
你可能通过下面的命令,尝试编译和打包成jar:
java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
就会输出经典的:
Hello World!

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 if the project type is - you guessed it - a WAR.
短语实际上对应潜在的goals。每个短语执行的特定goals是依赖于项目的包装类型。例如 package执行jar:jar如果工程是个JAR,如果你认为工程是个WAR则执行war:war。

An interesting thing to note is that phases and goals may be executed in sequence.
一个有趣的事情是:短语和goals可能按序执行。
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 versatility 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 Getting Started Guide吧!


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26614996/viewspace-1653627/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/26614996/viewspace-1653627/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值