`

5 分钟 Maven

阅读更多

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.

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

If you are using Windows, you should look at Windows Prerequisites to ensure that you are prepared to use Maven on Windows.

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!

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.

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.

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.

 

来源: 

http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

分享到:
评论

相关推荐

    5分钟熟悉Maven

    本指南将带您快速熟悉Maven的基本操作,只需5分钟即可基本掌握。 **1.1 Maven的安装** Maven作为一款Java工具,因此首先需要安装Java环境。具体步骤如下: - **Java安装**:确保您的系统已安装Java。您可以访问...

    Maven2 5分钟学习教程(中文)----maven2 官方文档翻译

    **Maven 2 5分钟学习教程(中文)——官方文档翻译** Maven是一个强大的Java项目管理工具,它简化了构建、依赖管理和项目生命周期的管理。这篇5分钟学习教程是Maven 2官方文档的中文翻译,旨在帮助初学者快速理解和...

    5分钟熟悉Maven之中文版官方文档翻译

    本文将基于中文版官方文档的翻译,引导您在5分钟内熟悉Maven的基本操作流程,包括安装、创建新项目、理解项目对象模型(POM)以及构建项目。 #### 安装Maven Maven作为一款Java工具,其安装前提条件为已安装Java环境...

    maven 环境搭建

    在首次使用 Maven 时,Eclipse 会自动构建 Maven 索引,这个过程可能需要几分钟的时间。也可以手动触发索引构建过程,在 Eclipse 的 Maven 视图中右键点击索引条目,选择 “Rebuild Index”。 #### 八、创建简单的 ...

    maven in action

    1. **Maven 五分钟入门**:快速了解 Maven 的基本使用方法。 2. **入门指南**:详细介绍如何搭建 Maven 环境,编写第一个 Maven 项目等。 3. **POM 参考**:深入讲解 pom.xml 文件的各个元素及其作用。 4. **配置...

    在CentOS7上用Nexus3搭建Maven私服.doc

    6. 首次启动可能需要约1分钟,然后可以通过浏览器访问Nexus3管理网站。 五、仓库管理 1. 登录Nexus3管理界面。 2. 可以选择新增jar存储路径,非必须操作。 3. 创建proxy代理仓库,将默认的中央仓库地址更换为阿里云...

    maven简单实用教程

    Maven的官方网站提供了丰富的学习资源,包括5分钟测试、入门教程、构建指南、POM和settings的参考文档,以及一本名为“Better Builds with Maven”的免费电子书,帮助开发者快速掌握Maven。 3. Maven与Ant的区别 ...

    解决maven projects plugin出现红线问题

    maven projects plugin出现红线怎么办,网上给许多比较蹩脚的方案。经过研究发现一个简洁的方法,一分钟就能搞定。

    Maven之远程仓库的配置详解

    其他可选值包括`never`(从不检查)、`always`(每次构建都检查)以及`interval:X`(每隔X分钟检查一次)。 - `checksumPolicy`:定义Maven处理校验和验证失败时的行为。默认值为`warn`,意味着显示警告信息并继续...

    maven工程-基于springboot定时任务

    ") // 每5分钟执行一次 public void reportCurrentTime() { System.out.println("当前时间:" + new Date()); } ``` 这里的cron表达式遵循Unix Cron格式,允许设置精确到秒的任务执行时间。 在实际项目中,我们...

    jenkins+maven-tomcat插件自动部署

    在“构建触发器”部分,勾选“Poll SCM”,这样Jenkins会在指定的时间间隔(如每分钟)检查Git仓库是否有代码变更。如果代码有更新,Jenkins将自动触发构建。你还可以配置“Build when a change is pushed to Git”...

    maven-quartz(定时任务)最简单版本

    ")) // 每5分钟执行一次 .build(); // 获取Scheduler实例并启动 SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler scheduler = schedulerFactory.getScheduler(); scheduler.start(); ...

    maven 通用配置

    合法的值有 `always`(每次构建都检查)、`daily`(每天检查一次)、`interval:10`(每隔10分钟检查一次)和 `never`(不检查)。 ### 8. 安装插件 Maven 插件可以在 `settings.xml` 的 `&lt;pluginGroups&gt;` 部分进行...

    使用jekins自动构建部署java maven项目

    4. **构建触发器**:设置定时构建的表达式,例如 `H/15 * * * *` 表示每 15 分钟构建一次。 5. **构建环境**:无需特别配置。 6. **构建前配置**:无需特别配置。 7. **构建配置**: - Root POM 设置为项目的顶层 ...

    apache-maven

    - **Maven 5 分钟入门:** - 简单介绍如何快速上手 Maven,包括安装、创建项目、构建等基本操作。 - **起步指南:** - 提供详细的教程,帮助初学者逐步了解 Maven 的工作原理和最佳实践。 - **POM 参考:** - ...

    CentOS7下svn+tomcat9.0+maven3.3+jenkins实现web项目自动构建与远程发布

    配置构建触发器,选择“Poll SCM”,设定定时检查Subversion仓库的频率,例如`H/5 * * * *`表示每5分钟检查一次。这样,每次代码提交时,Jenkins都会自动执行构建。 最后,配置构建后操作,选择“Deploy war/ear to...

    appengine-maven-repository:托管在Google App-Engine上,由Google Cloud Storage支持并在不到5分钟的时间内部署的免费私有Maven存储库

    托管在Google App-Engine上,由Google Cloud Storage支持的私有Maven存储库,支持在不到5分钟的时间内部署HTTP基本身份验证和简约的用户访问控制。 为什么呢 私人Maven仓库不应该花费你,也不要求你成为一个设置,...

    spring--quartz的定时调度的maven部署

    -- 每5分钟执行一次 --&gt; ``` `cronExpression`参数是Cron表达式,用于设置定时任务的执行周期。 最后,当我们的项目打包成JAR或WAR文件后,可以通过Maven的`exec-maven-plugin`插件来运行这个定时任务服务。或者...

    Maven2使用 搭建持续集成环境

    - `&lt;schedule&gt;`:设定构建间隔,例如每 5 分钟一次。 - `&lt;maven2&gt;`:指定 Maven2 的构建命令,如执行 `clean site package` 命令来清理、生成站点文档并打包项目。 - `&lt;log&gt;`:定义日志目录,并合并特定的报告...

Global site tag (gtag.js) - Google Analytics