文档章节

官网Maven简介

k
 kim366
发布于 2016/05/13 19:11
字数 1117
阅读 0
收藏 0
点赞 2
评论 0

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.


    
  1. my-app
  2. |-- pom.xml
  3. `-- src
  4. |-- main
  5. | `-- java
  6. | `-- com
  7. | `-- mycompany
  8. | `-- app
  9. | `-- App.java
  10. `-- test
  11. `-- java
  12. `-- com
  13. `-- mycompany
  14. `-- app
  15. `-- 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:


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

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.


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


     
  1. 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://blog.csdn.net/oyangyujun/article/details/48809841

共有 人打赏支持
k
粉丝 1
博文 129
码字总数 0
作品 0
朝阳
[struts2学习笔记] 第一节 关于struts2的简单认知

本文地址:http://blog.csdn.net/sushengmiyan/article/details/40298287 官方文档: http://struts.apache.org/release/2.3.x/docs/home.html 本文作者:sushengmiyan --------------------......

技术mix呢
2017/10/12
0
0
30.Apollo源代码在mac下编译

一. Apollo简介 Apache Apollo是一个代理服务器,其是在ActiveMQ基础上发展而来的,可以支持STOMP, AMQP, MQTT, Openwire, SSL, and WebSockets 等多种协议。 官网链接 自己使用Apollo主要是...

rongwei84n
2017/07/25
0
0
使用Docker搭建SonarQube检测代码质量

SonarQube 简介 Sonar是一个用于代码质量管理的开源平台,用于管理源代码的质量,可以从七个维度检测代码质量 可以通过插件形式,支持包括java,C#,C/C++,PL/SQL,Cobol,JavaScrip,Groovy等等二...

嘿嘿!!
2017/09/25
0
0
Dubbo分布式框架:(二)Zookeeper实战

一.Zookeeper简介 讲到Dubbo不得不说它的核心组件Zookeeper,ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它...

HaleyLiu
05/04
0
0
xStream 学习笔记 - Java对象与XML相互转换简单示例

官网地址:http://x-stream.github.io/download.html 下载地址:http://repo.maven.apache.org/maven2/com/thoughtworks/xstream/xstream/ 文档地址:http://x-stream.github.io/javadoc/ind......

kindcao
2016/06/02
51
0
maven---使用maven创建项目

maven简介:maven是apache一个项目管理工具,最大的优点就是可以轻松管理项目依赖,只要手动配置就能添加删除依赖,在使用框架或者其他jar包的工程中非常有用 maven使用: maven使用纯JAVA开...

shadowalker1990
2014/02/25
0
0
Lombok 安装、入门 -spice up your java

简介 官网地址:https://projectlombok.org 提供的注解:https://projectlombok.org/features/index.html 下载链接:https://projectlombok.org/download.html 安装方式: 使用 lombok 是需要......

cloud-coder
2015/11/22
0
0
利用MRUnit进行MapReduce单元测试

一、MRUnit简介 官网地址:https://mrunit.apache.org/ Apache MRUnit ™ is a Java library that helps developers unit test Apache Hadoop map reduce jobs. MRUnit是一个帮助开发者测试m......

cloud-coder
2014/06/29
0
1
2015 年度新增开源软件排名 TOP 100

本榜单包含 2015 年开源中国新收录的 5977 款开源软件中,根据软件本身的关注度、活跃程度进行排名前 100 名的软件。从这份榜单中或许可以了解到最新业界的趋势。 1、SwitchyOmega 项目简介:...

oschina
2016/01/12
56.8K
48
gradle的安装,配置,构建,研究,初体验......(入职一周研究的第一个大知识点)

(1)Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建工具。它使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,抛弃了基于XML的各种繁琐配置。更多简介请百度,谢谢;...

别叫小伙
2017/07/10
0
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

【面试题】盲人坐飞机

有100位乘客乘坐飞机,其中有一位是盲人,每位乘客都按自己的座位号就坐。由于盲人看不见自己的座位号,所以他可能会坐错位置,而自己的座位被占的乘客会随便找个座位就坐。问所有乘客都坐对...

garkey
今天
0
0
谈谈神秘的ES6——(二)ES6的变量

谈谈神秘的ES6——(二)ES6的变量 我们在《零基础入门JavaScript》的时候就说过,在ES5里,变量是有弊端的,我们先来回顾一下。 首先,在ES5中,我们所有的变量都是通过关键字var来定义的。...

JandenMa
今天
1
0
arts-week1

Algorithm 594. Longest Harmonious Subsequence - LeetCode 274. H-Index - LeetCode 219. Contains Duplicate II - LeetCode 217. Contains Duplicate - LeetCode 438. Find All Anagrams ......

yysue
今天
0
0
NNS拍卖合约

前言 关于NNS的介绍,这里就不多做描述,相关的信息可以查看NNS的白皮书http://doc.neons.name/zh_CN/latest/nns_background.html。 首先nns中使用的竞价货币是sgas,关于sgas介绍可以戳htt...

红烧飞鱼
今天
1
0
Java IO类库之管道流PipeInputStream与PipeOutputStream

一、java管道流介绍 在java多线程通信中管道通信是一种重要的通信方式,在java中我们通过配套使用管道输出流PipedOutputStream和管道输入流PipedInputStream完成线程间通信。多线程管道通信的...

老韭菜
今天
0
0
用Python绘制红楼梦词云图,竟然发现了这个!

Python在数据分析中越来越受欢迎,已经达到了统计学家对R的喜爱程度,Python的拥护者们当然不会落后于R,开发了一个个好玩的数据分析工具,下面我们来看看如何使用Python,来读红楼梦,绘制小...

猫咪编程
今天
1
0
Java中 发出请求获取别人的数据(阿里云 查询IP归属地)

1.效果 调用阿里云的接口 去定位IP地址 2. 代码 /** * 1. Java中远程调用方法 * http://localhost:8080/mavenssm20180519/invokingUrl.action * @Title: invokingUrl * @Description: * @ret......

Lucky_Me
今天
1
0
protobuf学习笔记

相关文档 Protocol buffers(protobuf)入门简介及性能分析 Protobuf学习 - 入门

OSC_fly
昨天
0
0
Mybaties入门介绍

Mybaties和Hibernate是我们在Java开发中应用的比较多的两个ORM框架。当然,目前Mybaties正在慢慢取代Hibernate,这是因为相比较Hibernate而言Mybaties性能更好,响应更快,更加灵活。我们在开...

王子城
昨天
2
0
编程学习笔记之python深入之装饰器案例及说明文档[图]

编程学习笔记之python深入之装饰器案例及说明文档[图] 装饰器即在不对一个函数体进行任何修改,以及不改变整体的原本意思的情况下,增加函数功能的新函数,因为这个新函数对旧函数进行了装饰...

原创小博客
昨天
1
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部