【转】Maven的安装与使用(ubuntu)

原文: http://www.cnblogs.com/yunwuzhan/p/5900311.html

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

Maven in 5 Minutes

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:

  1. mvn --version

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

  1. Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 14:51:28+0100)
  2. Maven home: D:\apache-maven-3.0.5\bin\..
  3. Java version: 1.6.0_25, vendor: Sun Microsystems Inc.
  4. Java home: C:\Program Files\Java\jdk1.6.0_25\jre
  5. Default locale: nl_NL, platform encoding: Cp1252
  6. 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:

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

  1. cd my-app

Under this directory you will notice the following standard project structure.

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:

  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.0</modelVersion>
  4. <groupId>com.mycompany.app</groupId>
  5. <artifactId>my-app</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>Maven Quick Start Archetype</name>
  9. <url>http://maven.apache.org</url>
  10. <dependencies>
  11. <dependency>
  12. <groupId>junit</groupId>
  13. <artifactId>junit</artifactId>
  14. <version>4.8.2</version>
  15. <scope>test</scope>
  16. </dependency>
  17. </dependencies>
  18. </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

  1. mvn package

The command line will print out various actions, and end with the following:

  1. ...
  2. [INFO] ------------------------------------------------------------------------
  3. [INFO] BUILD SUCCESSFUL
  4. [INFO] ------------------------------------------------------------------------
  5. [INFO] Total time: 2 seconds
  6. [INFO] Finished at: Thu Jul 07 21:34:52 CEST 2011
  7. [INFO] Final Memory: 3M/6M
  8. [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:

  1. java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App

Which will print the quintessential:

  1. Hello World!

------------------------------------------------------------------------------------------------------------

Maven的安装与使用(ubuntu)

一.安装Maven

    1.下载Maven,http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz。

    2.解压,移动到相应文件夹:

        

        

    3.在/etc/profile中配置对应环境变量:

        

    4.mvn -v查看版本信息

        

二、Maven的使用及基本了解

    创建java项目:

    

    生成对应目录结构:

    

    1)Maven插件:mvn archetype:generate 就表示调用maven-archetype-plugin的generate目标,实际上就是让maven-archetype-plugin生成一个很简单的项目结构,ubuntu中Ctrl+H可查看隐藏文件,我的插件在/home/zhanyunwu/.m2/repository/org/apache/maven/plugins/maven-archetype-plugin目录下。我们同样可以使用其他插件:,在项目helloworld路径下运行,可发现在项目根目录下多了.classpath和.project文件,该插件使得项目可以导入到eclipse中去。还可以使用archetype插件选择其他模板建立如javaweb项目。

    2)Maven生命周期:除了自己调用插件,也可把相应插件目标与生命周期阶段绑定,来调用插件。例如 在helloworld路径下mvn package 会依次执行默认生命周期中直到包括 package 阶段前的所有阶段的插件目标。如下图,调用编译,测试,打包等插件。

    

时间: 2024-10-12 20:00:20

【转】Maven的安装与使用(ubuntu)的相关文章

Maven的安装与使用(ubuntu)

一.安装Maven 1.下载Maven,http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz. 2.解压,移动到相应文件夹: 3.在/etc/profile中配置对应环境变量: 4.mvn -v查看版本信息 二.Maven的使用及基本了解 创建java项目: 生成对应目录结构: 1)Maven插件:mvn archetype:generate 就表

ubuntu maven环境安装配置

一.环境说明: 二.下载 maven 三.解压安装 四.设置环境变量 五.配置用户范围setting.xml 一.环境说明: 操作系统:Ubuntu 12.04.2 LTS(32位) maven:apache-maven-3.2.2 安装maven前请先安装好jdk,jdk安装可参见:http://my.oschina.net/hongdengyan/blog/150342. 二.下载 maven 下载链接:http://maven.apache.org/download.cgi 选择最新版本的

Maven的安装、配置及使用入门

本书代码下载 大家可以从我的网站下载本书的代码:http://www.juvenxu.com/mvn-in-action/,也可以通过我的网站与我取得联系,欢迎大家与我交流任何关于本书的问题和关于Maven的问题. 咖啡与工具 本书相当一部分的内容是在苏州十全街边的Solo咖啡馆完成的,老板Yin亲手烘焙咖啡豆.并能做出据说是苏州最好的咖啡,这小桥流水畔的温馨小 屋能够帮我消除紧张和焦虑,和Yin有一句没一句的聊天也是相当的轻松.Yin还教会了我如何自己研磨咖啡豆.手冲滴率咖啡,让我能够每天在家

Maven简介 &amp; 安装

近期开发的云平台项目使用了Maven 最为项目开发的管理工具,那么何为Maven呢?有何用途以及它的实现原理是什么呢?带着好奇,我们一起来学习Maven,共同揭开它的神秘面纱! 一,何为Maven Maven是一个采用纯java编写的开源项目管理工具.采用一种被称为Project  object Model(POM)概念来管理项目,所有的项目配置信息都被定义在pom.xml文件中,通过这个pom文件,就可以管理我们对所有包的依赖.例如,我们使用一些开源框架,第三方工具的jar包,以及这些jar包

Maven学习笔记(二) :Maven的安装与配置

在Windows上安装Maven:  1.  首先检查JDK安装 通过命令行执行命令:echo %JAVA_HOME%和 java  -version,可以查看当前java的安装目录及java的版本,maven要求JDK的版本必须在1.4以上. 2. 下载Maven 前往maven的下载页面:http://maven.apache.org/download.cgi下载最新或稳定版的maven,如:apache-maven-3.2.3-bin.zip. 3. 安装Maven 将压缩文件解压到你想要

Maven的安装以及使用

1.  maven的介绍 2.  maven的安装配置 3.  安装maven插件m2e 4.  创建maven工程 5.  Maven的核心概念 a)         坐标 b)         依赖管理 c)         生命周期 d)         插件 e)         继承 f)          聚合 6.  maven的仓库管理以及私服(nexus)的搭建 一.Maven简介以及使用背景 why: 1.都是同样的代码,为什么在我的机器上可以编译执行,而在他的机器上就不行?

MyEclipse下Maven的安装配置

Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. 发文时,绝大多数开发人员都把 Ant 当作 Java 编程项目的标准构建工具.遗憾的是,Ant 的项目管理工具(作为 make的替代工具)不能满足绝大多数开发人员的需要.通过检查 Ant 构建文件,很难发现项目的相关性信息和其它信息(如开发人员/拥有者.版本或站点主页). Maven 除了以程序构建能力为特色之外,还提供 Ant 所缺少的高级项目管理工具.由于 Maven 的缺省构建规

maven:安装m2eclipse插件

离线安装: 1.下载插件包:http://pan.baidu.com/s/1c0cRHsO 2.解压到:D:\downloads\m2eclipse 3.在eclipse的dropins目录下,新建maven.link文件 4.编辑maven.link,指向插件位置:path=D:/downloads/m2eclipse 5.重启eclipse即可 maven:安装m2eclipse插件,布布扣,bubuko.com

Maven基本安装与配置

百度Maven,进入Maven官网,点击Download 点击下载Binary zip包,下载到电脑上相应的位置即可. 找到下载文件,进行解压,解压到相应的文件夹下面,并且记住路径. 打开系统->高级系统设置 点击环境变量 点击新建 新建系统变量,变量名为M2_HOME,变量值为Maven解压后的文件夹,点击确定 再配置Path变量,新建%M2_HOME%bin,点击确定 点击确定,进行环境变量的应用 最后来测试一下Maven是否安装完成,进入cmd命令行,输入mvn -version,如出现以