`
king_tt
  • 浏览: 2110044 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Maven 入门 (2)—— 创建Maven项目

 
阅读更多

读这篇文章之前请先确保你成功安装了maven,如果你还没安装成功,请先看:Maven 入门 (1)—— 安装这里的文章安装成功再继续。


因为maven在执行一些插件目标的时候可能会从中心数据库下载依赖的文件,因此,使用maven的时候请保持联网状态。

现在如果你已经迫不及待了,那么可以尝试在命令行终端先输入:

mvn clean install

这时候会打印正在下载各种文件。是不是感觉很爽呢?好了,下面我们开始进入正题:


1、先创建一个Maven项目。

在终端输入命令然后执行:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
和之前一样,现在它也是在不停下载各种所需要的文件。
等到命令结束后,你发现当前目录下多了一个叫my-app的目录,它里面有pom.xml和src目录,如下图:
完整目录结构如下:
是不是很神奇嘞?
神奇归神奇,刚刚那个命令那么复杂,它到底是什么意思,做了什么操作?别急,我们慢慢看。

首先,你刚刚执行了一个maven的目标"archetype:generate",并传了不同的参数给那个目标。"archetype"是一个插件,它包含了"generate"这个目标。如果你熟悉Ant,你可以假设它就是ant中的task。这个目标创建基于原型之上的简单工程。总之就是,插件是一系列通用目标的集合。例如jboss-maven-plugin,就是用于处理各种不同的jboss项目。

接着,我们来看看 -DgroupId  -DartifactId -DarchetypeArtifactId -DinteractiveMode 这几个属性
其实它们是Java执行时的参数。
groupId:一般填的是项目名,有的人喜欢填公司名。这里说下,一个公司一般是有多个项目的,如果这里填公司名,那么artifactId只能填项目名了。而version毫无疑问是版本号。这样在项目的划分上粒度比较粗。因此,我一般groupId是填到项目。比如:com.taobao.shop。
artifactId:上面填了项目,那么这里就可以填模块了。比如:login
version:这里填的是本模块版本。
OK上面的groupId、artifactId、version(简称GAV)构成了maven中的“定位系统“,理论上所有的依赖包都可以通过GAV找到。
我们接着看其它参数,
archetypeArtifactId:这个参数其实是指定一个模版来初始化生成你的项目。比如你是一个普通的java应用程序,还是一个web程序,都有对应的模版。
如果你还是不懂怎么生成一个maven项目,那么,你也可以通过命令行:

mvn help:describe -Dplugin=archetype

一样的,这里help也是一个插件,然后describe是目标,而-Dplugin参数指明了describe要查看的是archetype插件的信息。


这样就能获得archetype这个插件的帮助。

Name: Maven Archetype Plugin

Description: Maven Archetype is a set of tools to deal with archetypes, i.e.

an abstract representation of a kind of project that can be instantiated into

a concrete customized Maven project. An archetype knows which files will be

part of the instantiated project and which properties to fill to properly

customize the project.

Group Id: org.apache.maven.plugins

Artifact Id: maven-archetype-plugin

Version: 2.2

Goal Prefix: archetype


This plugin has 8 goals:


archetype:crawl

Description: Crawl a Maven repository (filesystem, not HTTP) and creates a

catalog file.


archetype:create

Description: The archetype creation goal looks for an archetype with a

given groupId, artifactId, and version and retrieves it from the remote

repository. Once the archetype is retrieved, it is then processed against a

set of user parameters to create a working Maven project.

Deprecated. Please use the generate mojo instead.


archetype:create-from-project

Description: Creates an archetype project from the current project.

This goal reads your source and resource files, the values of its

parameters, and properties you specify in a .property file, and uses them

to create a Maven archetype project using the maven-archetype packaging. If

you build the resulting project, it will create the archetype. You can then

use this archetype to create new projects that resemble the original.

The maven-archetype-plugin uses Velocity to expand template files, and this

documentation talks about 'Velocity Properties', which are values

substituted into Velocity templates. See The Velocity User's Guide for more

information.

This goal modifies the text of the files of the current project to form the

Velocity template files that make up the archetype.

GAV

The GAV values for the current project are replaced by properties:

groupId, artifactId, and version. The user chooses new values for these

when generating a project from the archetype.

package

All the files under one specified Java (or cognate) package are relocated

to a project that the user chooses when generating a project. References

to the class name are replaced by a property reference. For example, if

the current project's sources are in the package

org.apache.saltedpeanuts, then any example of the string

org.apache.saltedpeanuts is replaced with the Velocity property reference

${packageName}. When the user generates a project, this is in turn

replaced by his or her choice of a package.

custom properties

You may identify additional strings that should be replaced by

parameters. To add custom properties, you must use the propertyFile

parameter to specify a property file. See the documentation for

propertyFile for the details.

Note that you may need to edit the results of this goal. This goal has no

way to exclude unwanted files, or add copyright notices to the Velocity

templates, or add more complex elements to the archetype metadata file.

This goal also generates a simple integration-test that exercises the

generated archetype.


archetype:generate

Description: Generates a new project from an archetype, or updated the

actual project if using a partial archetype. If the project is fully

generated, it is generated in a directory corresponding to its artifactId.

If the project is updated with a partial archetype, it is done in the

current directory.


archetype:help

Description: Display help information on maven-archetype-plugin.

Call

mvn archetype:help -Ddetail=true -Dgoal=<goal-name>

to display parameter details.


archetype:integration-test

Description: Execute the archetype integration tests, consisting in

generating projects from the current archetype and optionally comparing

generated projects with reference copy.

Each IT consists of a sub-directory in src/test/resources/projects

containing:

- a goal.txt file, containing a list of goals to run against the generated

project (can be empty, content ignored before maven-archetype-plugin

2.1),

- an archetype.properties file, containing properties for project

generation,

- an optional reference/ directory containing a reference copy of the

expected project created from the IT.

Notice that it is expected to be run as part as of a build after the

package phase and not directly as a goal from CLI.


archetype:jar

Description: Build a JAR from the current Archetype project.


archetype:update-local-catalog

Description: Updates the local catalog


For more information, run 'mvn help:describe [...] -Ddetail'


[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 3.851s

[INFO] Finished at: Tue Dec 24 02:45:02 CST 2013

[INFO] Final Memory: 11M/156M

[INFO] ------------------------------------------------------------------------

我们注意到:archetype这个插件本身也有个help目标,似乎能查找更多帮助信息。

那么我们可以尝试用下面命令来查找generate目标的信息:

mvn archetype:help -Ddetail=true -Dgoal=generate

然后,这时候返回的内容是:

cmatoMacBook-Pro:~ cjunhong$ mvn archetype:help -Ddetail=true -Dgoal=generate

[INFO] Scanning for projects...

[INFO]

[INFO] ------------------------------------------------------------------------

[INFO] Building Maven Stub Project (No POM) 1

[INFO] ------------------------------------------------------------------------

[INFO]

[INFO] --- maven-archetype-plugin:2.2:help (default-cli) @ standalone-pom ---

[INFO] org.apache.maven.plugins:maven-archetype-plugin:2.2

Maven Archetype Plugin

Maven Archetype is a set of tools to deal with archetypes, i.e. an abstract

representation of a kind of project that can be instantiated into a concrete

customized Maven project. An archetype knows which files will be part of the

instantiated project and which properties to fill to properly customize the

project.

archetype:generate

Generates a new project from an archetype, or updated the actual project if

using a partial archetype. If the project is fully generated, it is generated

in a directory corresponding to its artifactId. If the project is updated with

a partial archetype, it is done in the current directory.

Available parameters:

archetypeArtifactId

The archetype's artifactId.

Expression: ${archetypeArtifactId}

archetypeCatalog (Default: remote,local)

The archetype catalogs to use to build a list and let the user choose

from. It is a comma separated list of catalogs. Catalogs use following

schemes:

- 'file://...' with archetype-catalog.xml automatically appended when

pointing to a directory

- 'http://...' or 'https://...' with archetype-catalog.xml always appended

- 'local' which is the shortcut for 'file://~/.m2/archetype-catalog.xml'

- 'remote' which is the shortcut for Maven Central repository, ie

'http://repo1.maven.org/maven2'

- 'internal' which is an internal catalog

Since 2.0-alpha-5, default value is no longer internal,local but

remote,local. If Maven Central repository catalog file is empty, internal

catalog is used instead.

Expression: ${archetypeCatalog}

archetypeGroupId

The archetype's groupId.

Expression: ${archetypeGroupId}

archetypeRepository

The archetype's repository.

Expression: ${archetypeRepository}

archetypeVersion

The archetype's version.

Expression: ${archetypeVersion}

basedir

(no description available)

Expression: ${basedir}

filter

Applying some filter on displayed archetypes list: format is artifactId or

groupId:artifactId.

- org.apache: -> displays all archetypes which contain org.apache in

groupId

- :jee or jee -> displays all archetypes which contain jee in artifactId

- org.apache:jee -> displays all archetypes which contain org.apache in

groupId AND jee in artifactId

Expression: ${filter}

goals

Additional goals to immediately run on the project created from the

archetype.

Expression: ${goals}

interactiveMode (Default: ${settings.interactiveMode})

User settings use to check the interactiveMode.

Required: Yes

Expression: ${interactiveMode}

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 1.088s

[INFO] Finished at: Tue Dec 24 02:56:42 CST 2013

[INFO] Final Memory: 9M/156M

[INFO] ------------------------------------------------------------------------

看到没有,连我们刚刚没有解释的interactiveMode参数,它里面也都有了说明了。

所以,要善于利用help插件来查找某个插件的信息,然后如果得知这个插件有help目标查找更详细信息,那么就更好了!

分享到:
评论

相关推荐

    Maven实战——入门篇

    maven入门,maven环境的搭建以及maven配置到eclipse以及pom.xml文件配置等

    apache maven入门教程

    在JAVA界,可能大家比较熟悉ANT,它提供了强大的功能,让我们的开发工作变得如此有趣,APACHE又推出了新一代项目管理工具——MAVEN。它提供了比ANT更强大的管理功能,可以使用命令行工具来生成一个新的项目,管理已...

    Maven实战入门篇

    Maven实战——入门篇.

    Maven从入门到精通的吐血之作.pdf

    Maven从入门到精通;Maven——web 应用进阶——简单多模块项目;

    maven资料大全

    Maven3培训教程.pptx MavenQuickReferenceCard.pdf MavenTheDefinitiveGuide.pdf Maven实战.pdf Maven实战——入门篇.pdf Maven学习.doc 持续集成之路——搭建Maven私服.doc 利用m2eclipse生成WTP项目.pdf

    Maven入门学习教程(下)

    在网易视频云:Maven入门学习教程(上)中,我们为各位简单介绍了Maven,并且谈到了Maven如何下载和安装。今天,网易视频云来教各位Maven该如何使用!1、Maven项目的目录约定MavenProjectRoot(项目根目录)|----src||...

    springBoot 入门(一)—— 使用idea创建第一个springBoot项目

    创建项目 welcome page is login……… application.properties #修改tomcat的默认的端口号,将8080改为8888 server.port=8888 现在我们看到整个程序是报错的。现在我们右键项目reimport一下 然后程序错误便...

    SSM整合——基于 IDEA Maven项目的 Spring + SpringMVC + MyBatis MVC架构整合.zip

    SSM框架学习宝典:入门、进阶、精通,全方位代码项目资 一、探索SSM的无限可能 SSM(Spring + Spring MVC + MyBatis)框架作为Java开发中的黄金组合,为开发者提供了强大的技术支持和丰富的功能。本系列资料将带您...

    Maven课件操作演示笔记以及上课课件.zip

    ⑤在子工程中找到被父工程管理的依赖信息,删除版本号部分 ⑥在父工程中统一修改已管理的依赖信息的版本号,看是否能够控制所有子工程 第三个Maven工程 ①设置通过Maven创建的工程的JDK版本——一劳永逸

    学生管理系统——SSM框架入门(包括代码可运行、设计文档、答辩pp、数据库)

    其实现了登录注册、教师学生基本信息的管理,项目采用IDEA作为编译器,采用maven进行项目管理,数据库使用的是MySQL8.0。SSM框架版本在pom文件中有相关说明,适合学习SSM框架入门者使用。大佬别下载,良心代码...

    JAVA基础项目从入门到精通

    由于其可用于Maven,因此大家只需要面向选定的构建工具添加关联性即可使用。 如果大家使用过Kik并听说过其遭遇的leftPad问题,那么Strman可能是个更好的选择——其能够返回特定长度的新字符串,且自动填充开头部分...

    erp后台初版系统.zip

    ———————————————— 版权声明:本文为CSDN博主「肖朋伟」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:...

    java8看不到源码-revomatico:Revomatico实习

    设计变更—— 当前设计 设计2 设计1 积压 先决条件 安装了java 启动idm服务器 安装了 Maven 与 maven 进行 prj 为 Windows 安装的cmder 能够像在 bash 中一样使用 curl 安装了 git 卷曲 检查服务器 安装idm 检查连接...

    JavaEE开发的颠覆者SpringBoot实战[完整版].part2

    第1 章 Spring 基础 2 1.1 Spring 概述 2 1.1.1 Spring 的简史 2 1.1.2 Spring 概述 3 1.2 Spring 项目快速搭建 5 1.2.1 Maven 简介 6 1.2.2 Maven 安装 6 1.2.3 Maven 的pom.xml 7 1.2.4 Spring 项目的搭建 9 1.3 ...

    swing界面设计之JTree

    项目创建好后,加入类库: 41 然后写一个persistence bean: 41 Java代码 41 我称这个类为HibernateValidationUI,代码如下: 43 Java代码 43 类中用到的Java2DIconFactory代码如下: 47 Java代码 47 Factory类 49 ...

    JavaEE开发的颠覆者SpringBoot实战[完整版].part3

    第1 章 Spring 基础 2 1.1 Spring 概述 2 1.1.1 Spring 的简史 2 1.1.2 Spring 概述 3 1.2 Spring 项目快速搭建 5 1.2.1 Maven 简介 6 1.2.2 Maven 安装 6 1.2.3 Maven 的pom.xml 7 1.2.4 Spring 项目的搭建 9 1.3 ...

    day01_eesy_01mybatis.zip

    第一步:创建maven工程并导入坐标 第二步:创建实体类和dao接口 第三步:创建Mybatis的主配置文件 sqlMapConfig.xml 第四步:创建映射配置文件 IUserDao.xml 环境搭建的注意事项: 第一个:创建...

    JavaEE开发的颠覆者SpringBoot实战[完整版].part1

    第1 章 Spring 基础 2 1.1 Spring 概述 2 1.1.1 Spring 的简史 2 1.1.2 Spring 概述 3 1.2 Spring 项目快速搭建 5 1.2.1 Maven 简介 6 1.2.2 Maven 安装 6 1.2.3 Maven 的pom.xml 7 1.2.4 Spring 项目的搭建 9 1.3 ...

    InOutBoard:InOut Board 作为 Web 应用程序

    此应用程序实现了“进/出板”——一种 20 世纪的设备,人们在办公室使用它来记录他们的进出。 输入/输出板通常包括以下信息,以表格形式呈现: 人名 他们的状态,即在办公室内外 一条评论 我编写这个应用程序是为了...

    跑腿源码java-sakai-scripts:我用来让我作为Sakai开发人员的生活更轻松的脚本的Repo

    2GB RAM 系统上运行,但使用 4GB 或更大的应用程序服务器要舒服得多。 Mac 入门(根据需要) 使用命令安装 git xcode-select --install 确保你安装了 Java-8 JDK(不是 JRE)——如果你想在 Sakai 10 上工作,不要...

Global site tag (gtag.js) - Google Analytics