五分钟快速搞定maven(maven in 5 minutes)

五分钟快速搞定maven(maven in 5 minutes)

说明:本文是我本人根据maven官网相关文档翻译而来,难免会有纰漏,还望纠错。原文出处maven官网http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html,。

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 mailing lists are not the best place to ask for this advice.


预备知识

你必须清楚怎么在电脑里面安装软件。如果你不知道如何做到这一点,请向你的同事、同学等请教,或者你可以付费请别人教你一下。这个关于Maven的知识列表可不是你询问如何安装软件的好地方。



Installation

Maven is a Java tool, so you must have Java installed in order to proceed.

First, download Maven and follow the installation instructions. After that, type the following in a terminal or in a command prompt:

mvn --version

It should print out your installed version of Maven, for example:

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是一个Java工具,所以首先你得确保安装了Java,以便于后续安装的进行。

首先下载Maven并且遵循下载说明。然后,在终端或者命令行输入以下命令:

mvn --version
会打印出您的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"

这(以上结果)取决于你的网络设置,你可能需要额外的配置(maven的环境变量配置)。如有必要,请参见Maven配置指南如果你使用的是Windows系统,请阅读Windows预备知识以确保你准备在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:

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.

You will notice that the generate goal created a directory with the same name given as the artifactId. Change into that directory.

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.

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.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 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".

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:

  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!

创建一个工程

你需要找一个地方存放你的工程。创建一个文件夹目录在某个地方并在此目录下用终端开启一个shell窗口(windows使用cmd命令行)

。在此命令行下,执行如下Maven命令:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
如果你刚刚安装好maven,第一次运行的时候可能需要一段时间。这是由于Maven需要下载最新的一些依赖组件(jar包以及其他文件)
到本地存储库。你可能还需要多执行几次这个命令。这是因为maven远程服务器可能在你下载完成之前出现超时。别慌,有办法去解决这个问题。

你将注意到目标文件夹下会创建一个与artifactId同名的目录文件。切换到这个目录下:

cd my-app

在这个文件夹下你会注意到如下标准工程结构:

my-app
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- mycompany
    |               `-- app
    |                   `-- App.java
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java

src/main/java目录下包含工程的源文件,src/test/java 目录下包含测试源代码文件,而pom.xml这个文件是该工程的工程对象模型,或称之为POM。

POM

pom.xml文件是maven工程的核心配置文件。它是一个独立的文件,包含了创建一个工程的主要信息,正如你所想的那种创建方式。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>

我刚才做了什么?

你执行了maven命令archetype:generate,并在使用了一些参数予以通过。archetype 是maven命令包含的一个插件。如果你的Ant熟悉的话,你可以把此次任务想象成是一样的。这个命令创建了一个简单的基于原型的项目。我只想说,一个插件就是一系列的用于一般常见目的的命令。例如jboss-maven-plugin,它的目的是用于“处理各种jboss项目”。

构建这个工程

mvn package

这条命令将会打印出一系列的动作,并且以如下结尾:
 ...
[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] ------------------------------------------------------------------------

不像第一个执行过的命令( archetype:generate),你会注意到这条命令仅仅需要一个单词-package。这是一个阶段而不简单是一个命令。一个阶段是一个构建生命周期中的一个步骤,这是一个有序序列的阶段。当一个阶段给定后,Maven将执行序列中的每一个阶段,包括一个定义。例如,如果我们执行compile阶段,该阶段实际执行中包含:

  1. validate
  2. generate-sources
  3. process-sources
  4. generate-resources
  5. process-resources
  6. compile

你可以用如下的命令来测试新编译、打包过的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 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
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.

运行Maven工具

Maven阶段
尽管几乎没有一个全面的列表,这些都是最常见的缺省生命周期阶段执行:

  • 验证: 验证工程是正确的并且所有的必要的信息都是可用的
  • 编译: 编译工程的源代码
  • 测试: 使用一个合适的单元测试框架去测试编译好的源代码。这些测试不需要代码是打包好的或者已部署好的。
  • 打包: 把编译好的代码打包成分配好的格式,比如Jar包的形式。
  • 集成测试:如有必要,将已打包的程序在集成测试环境中测试流程,部署。
  • 验证: 运行任何检查,确认方案是有效的和符合质量标准
  • 安装:安装包到本地存储库, 可以在其它工程里进行本地化的调用
  • 部署: 在集成或者生产环境下完成,备份最后的一个包到远程存储库上,与其它开发者、项目共享。
另外还有两个其它的Maven生命周期,它们是:

  • 清理: 清理之前创建的组件
  • 生成site: 生成这个工程的网站文档
阶段实际上是映射到底层的命令。每个阶段执行的特定命令是取决于工程的打包类型。例如,如果工程类型是jar类型,那么执行jar:jar命令,而war:war对应的工程类型是——正如你想到的——一个war包。

值得注意是,阶段和目标可能依次执行。

mvn clean dependency:copy-dependencies package

这条命令将会清理工程,拷贝依赖,并且打包工程(执行所有阶段直到打包)。

生成站点

mvn 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 you have just performed. Check out the Maven Getting Started Guide.

总结

我们希望这个快速概览已经激发了你对Maven便利性的兴趣。请注意这是一个删节版的快速指引。对于你刚刚执行的结果,你可以准备去了解更多更全面的细节。看看Maven快速指南吧。


原文出处:http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值