`

maven 五分钟 cook

阅读更多

1 分钟:Installation

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

First, download Maven and unzip it to your desired installation directory, for example C:\maven in windows, or /usr/local/maven in Linux. After this, add the system variable M2_HOME as well as the $M2_HOME/bin directory to your system path. Type the following in a terminal or command prompt:

mvn --version

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

  Maven version: 2.0.4

Depending upon your network setup, you may require extra configuration. Check out the Guide to Configuring Maven if necessary.

2 分钟:Creating a Project

On your command line, execute the following maven goal:

mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-app

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 create 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 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:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.   <modelVersion>4.0.0modelVersion>  
  4.   <groupId>com.mycompany.appgroupId>  
  5.   <artifactId>my-appartifactId>  
  6.   <packaging>jarpackaging>  
  7.   <version>1.0-SNAPSHOTversion>  
  8.   <name>Maven Quick Start Archetypename>  
  9.   <url>http://maven.apache.orgurl>  
  10.   <dependencies>  
  11.     <dependency>  
  12.       <groupId>junitgroupId>  
  13.       <artifactId>junitartifactId>  
  14.       <version>3.8.1version>  
  15.       <scope>testscope>  
  16.     dependency>  
  17.   dependencies>  
  18. project>  

What did I just do?

You executed the Maven goal archetype:create , 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 concieve 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".

3分钟: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 Oct 05 21:16:04 CDT 2006
[INFO] Final Memory: 3M/6M
[INFO] ------------------------------------------------------------------------

Unlike the first command executed (archetype:create ) 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!

4分钟: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 .

5分钟: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 .

分享到:
评论
8 楼 kldwq2002 2008-08-22  
kimmking 写道
cnfree 写道
Maven是好东西,以前公司的项目用Ant build 一次要2个多小时,用Maven只花了几十分钟。


if it's maven's magic,maven can rule the earth soon.

but i guess it's a mistake about someone who written the ant-code in your office.


如果你不是一个老外,在中文论坛里不要用英文。
7 楼 kimmking 2008-08-20  
cnfree 写道
Maven是好东西,以前公司的项目用Ant build 一次要2个多小时,用Maven只花了几十分钟。


if it's maven's magic,maven can rule the earth soon.

but i guess it's a mistake about someone who written the ant-code in your office.
6 楼 hintcnuie 2008-08-20  
You are right. It is indeed from Maven official website.But I did this only because it can save me some time to find this article again.
5 楼 manysysy 2008-08-04  
楼主辛苦了,但是你这好象是直接从官方网站上复制下来的哦
4 楼 hantsy 2008-06-29  
cnfree 写道
Maven是好东西,以前公司的项目用Ant build 一次要2个多小时,用Maven只花了几十分钟。


这是扯蛋。。。
maven 似乎没有这样的能力
3 楼 manysysy 2008-06-28  
Maven是好东西,以前公司的项目用Ant build 一次要2个多小时,用Maven只花了几十分钟。

2个小时?不会吧,说清楚一点啊
2 楼 cnfree 2007-10-29  
Maven是好东西,以前公司的项目用Ant build 一次要2个多小时,用Maven只花了几十分钟。
1 楼 沧海遗梦 2007-10-26  
So cool
可是我的英文太菜还是找中文的看吧

相关推荐

    maven安装maven安装maven安装maven安装maven安装

    maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装...

    5分钟熟悉Maven

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

    maven cook book

    ### Maven Cook Book 知识点解析 #### 一、Maven简介 Maven是一个项目管理和理解工具,由Apache软件基金会支持。它可以帮助开发者构建、管理Java项目,通过一系列标准化的生命周期和构建流程来提高开发效率。 ###...

    maven 3.5.2 maven 3.5.2 maven 3.5.2

    5. **支持Java 9**: Maven 3.5.2兼容Java 9,允许开发者在新的JDK环境下使用Maven。 **Maven 的使用流程** 1. **创建项目**: 使用`mvn archetype:generate`命令可以快速创建一个基于模板的新项目。 2. **编辑POM*...

    maven3.6maven3.6maven3.6

    Maven是Java世界中的一款强大的项目管理和构建工具,它极大地简化了软件项目的构建、依赖管理和文档生成过程。在本文中,我们将深入探讨Maven 3.6版本,这是Maven的一个重要迭代,带来了许多改进和优化。 Maven的...

    开源工具Maven3.9.4版本压缩包

    Maven3.9.4版本压缩包,仅供学习参考,更新版本请前往Maven官方下载;Maven3.9.4版本压缩包,仅供学习参考,更新版本请前往Maven官方下载;Maven3.9.4版本压缩包,仅供学习参考,更新版本请前往Maven官方下载;Maven...

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

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

    使用Maven导入Maven工程的视频教程

    使用Maven导入Maven工程的视频教程 仅供学习交流! 后续会持续分享相关资源,记得关注哦! 使用Maven导入Maven工程的视频教程 使用Maven导入Maven工程的视频教程 使用Maven导入Maven工程的视频教程 使用Maven导入...

    Windoiws的maven3.8.8

    ### 五、常用Maven命令 - **install**: 编译源码,打包,并将结果安装到本地仓库。 - **clean**: 清除目标目录(target)中的所有生成文件。 - **compile**: 编译项目源码。 - **test**: 运行项目的所有测试。 - **...

    maven API maven API

    5. **仓库管理 (Repository Management)**: Maven使用本地仓库(Local Repository)存储项目依赖,同时与远程仓库(Remote Repository)交互,如中央仓库(Central Repository),获取或上传项目构件。 **二、Maven...

    apache-maven-3.6.0_apache-maven-3.6.0_maven压缩包_maven3.6.0下载_

    Apache Maven 是一个强大的项目管理和构建工具,主要用于Java项目。它基于项目对象模型(Project Object Model,POM)的概念,能够管理项目的构建、报告和文档。Maven 3.6.0是该工具的一个稳定版本,提供了许多改进...

    Maven笔记Maven笔记Maven笔记

    Maven的安装: (首先保证JDK版本在1.6以上) 1: 通过配置MAVEN_HOME 和 %% %MAVEN_HOME%\bin 然后进行mvn -version 测试 掌握 -Xms 与 -Xmx的相关配置 ... 5: target --项目输出管理 6: pom.xml

    Maven很Maven仓库

    【Maven很Maven仓库】这个标题暗示了讨论的核心是关于Maven以及它与Maven仓库的关系。Maven是一个在Java开发中广泛使用的项目管理和综合工具,它通过一个声明式的配置来管理项目的构建、报告和文档。Maven仓库则是...

    maven之开源中国Maven库

    【标题】"maven之开源中国Maven库" 指的是使用 Maven,一个流行的Java项目管理和集成工具,与开源中国的Maven仓库进行交互。开源中国Maven库是中国的一个开源软件资源平台,提供了大量的开源Java库,使得开发者可以...

    Maven安装包Maven安装包

    Maven安装包Maven安装包Maven安装包Maven安装包Maven安装包Maven安装包Maven安装包Maven安装包Maven安装包Maven安装包Maven安装包Maven安装包Maven安装包Maven安装包Maven安装包Maven安装包Maven安装包Maven安装包...

    Maven全版本资源,Maven 3.0.5-3.8.5,每个版本包含4个文件,Maven3全资源打包下载,Maven全集

    apache-maven-3.0.5 apache-maven-3.1.1 apache-maven-3.2.5 apache-maven-3.3.9 apache-maven-3.5.4 apache-maven-3.6.3 apache-maven-3.8.5 每个版本包含4个文件: apache-maven-3.8.5-bin.tar.gz apache-maven-...

    Mac的maven安装包apache-maven-3.6.3.zip

    5. **Maven的生命周期和插件** Maven的生命周期由一系列阶段(如编译、测试、打包、部署)组成,每个阶段对应一个或者多个目标(goals)。插件是实现这些目标的组件,例如`maven-compiler-plugin`用于编译Java源...

    maven教程-maven教程-maven教程

    Maven是Java领域广泛使用的项目管理工具,它能够帮助开发者完成项目的构建、文档生成、报告、依赖管理和软件生命周期管理等工作。通过本文的介绍,我们可以了解到Maven的基本概念、安装配置、基本使用和深入应用详解...

    maven.rar apache-maven-3.5.4

    Apache Maven 是一个强大的项目管理和构建工具,主要用于Java项目。它基于项目对象模型(Project Object Model,POM),能够管理项目的构建、报告和文档,通过一套统一的构建生命周期和插件系统,极大地简化了软件...

    maven 3.8.8 解压安装版

    Maven项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的项目管理工具软件。 Maven 除了以程序构建能力为特色之外,还提供高级项目管理工具。由于 Maven 的缺省构建规则有较高的可重用性,...

Global site tag (gtag.js) - Google Analytics