欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Maven官网上Maven in 5 Minutes 文档翻译

程序员文章站 2022-05-07 22:27:51
...

Maven in 5 Minutes
Maven 5分钟教程
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
请你问一下你的同事、同学,或者花钱问一下其他的人给你解释一下,给Maven组织发邮件不是很好的建议。
not the best place to ask for this advice.
Installation
安装
Maven is a Java tool, so you must have Java installed in order to proceed.
Maven 是一个java工具,为了开始使用Maven请先安装java
First, download Maven and follow the installation instructions. After that, type the following in a terminal or in a command prompt:
首先下载Maven并根据提示指引完成安装,然后在命令提示行窗口执行下面的脚本:
mvn --version

It should print out your installed version of Maven, for example:
你的屏幕上应该会出现你安装的Maven的版本信息,例如:
Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 14:51:28+0100)
Maven home: D:\apache-maven-3.0.5\bin\..
Java version: 1.6.0_25, vendor: Sun Microsystems Inc.
Java home: C:\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.
基于你的网络环境,你可能需要进行额外的配置。如果需要查看指南配置Maven。
If you are using Windows, you should look at Windows Prerequisites to ensure that you are prepared to use Maven on Windows.
如果你用的是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:
为了存放你的项目,你需要创建一个目录,并在此目录执行一个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.
如果你刚刚完成安装Maven,第一次运行可能需要等待一段时间。
This is because Maven is downloading the most recent artifacts (plugin jars and other files) into your local repository.
这是因为Maven正在下载最近的工具(插件JAr包和其他的文件)到你本地的库
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.
你会发现,generate goal命令创建的目录名称与给定的“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,
这个“src/main/java”目录,包含了项目的源代码
the src/test/java directory contains the test source, and the pom.xml file is the project's Project Object Model, or POM.
这个"src/test/java"目录包含了测试代码,这个pom.xml文件是项目的项目对象模型(POM)
The POM

The pom.xml file is the core of a project's configuration in Maven.
pom.xml文件是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,
POM 是一个宏大的并且它的复杂性可能会使你退缩的
but it is not necessary to understand all of the intricacies just yet to use it effectively. This project's POM is:
但是有效的使用它也并不需要理解全部的复杂内容,一个项目的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.
你执行了Maven目标原型命令:通过多样的参数生成了那个目标。
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.
如果你熟悉Ant,你可以假定它与一个任务相似。
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".
例如jboss-maven-plugin插件,它的目的是处理众多的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.
不像第一次只想能够的命令(archetype:generate),你也许会发现第二次执行只是一个简单的单词-package。
Rather than a goal, this is a phase.
而不是一个goal,这是一个阶段。
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.
当一个环节需要执行,Maven将有顺序的执行明定义中包含每一个子环节。
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:
你可以通过下面的命令测试新编译和打包的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
    validate:验证这个项目是正确的并且全部主要的信息都是可用
    compile: compile the source code of the project
    compile: 编译这个项目的源代码
    test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
    test:用合适的测试框架测试编译后的代码
    package: take the compiled code and package it in its distributable format, such as a JAR.
    package: 取编译后的代码并按照可分配的格式进行打包,如jar
    integration-test: process and deploy the package if necessary into an environment where integration tests can be run
    integration-test:如果需要的话,处理和部署项目包到集成测试环境中。
    verify: run any checks to verify the package is valid and meets quality criteria
    verify: 运行一些检查来验证这个包是有效的质量检测。

    install: install the package into the local repository, for use as a dependency in other projects locally
    install:安装这个包到本地库中,为用作本地其他项目的依赖。

    deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
    deploy: 在集成或者发布环境处理,拷贝这个最终的包到远程库用来和其他的开发人员或者项目所分享

There are two other Maven lifecycles of note beyond the default list above. They are
还有另外两个Maven生命周期超出了上面缺省命令列表,它们是:
    clean: cleans up artifacts created by prior builds
    clean:

    site: generates site documentation for this project
    site:为项目生成设置文档

Phases are actually mapped to underlying goals.
这些环节实际上应设在目标底层。
The specific goals executed per phase is dependant upon the packaging type of the project.
不同的goals 执行的每一个环节取决于项目的打包类型
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.
例如,打包执行jar命令如果这个项目是类型是JAR,和war命令,如果你猜测这个项目的类型是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.
这个环节以项目的POM文件为基础生成一个文档,你可以查看这个生成文档通过target/site.

Conclusion
结论

We hope this quick overview has piqued your interest in the versatility of Maven.
我们希望这快速概述激起了你对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入门指南。