当前位置 : 首页 » 文章分类 :  开发  »  Apache-Maven-Archetype

Apache-Maven-Archetype

Archetype是maven的一个插件,用来生成项目框架(骨架,模版),例如maven-archetype-quickstart就是最简单的Maven项目模板,只需提供基本的元素,如groupId,artifactId,version等,它就能生成项目的基本结构及POM文件。


常用Archetype

maven-archetype-quickstart:默认的Archetype,基本内容包括:

  • 一个包含junit依赖声明的pom.xml
  • src/main/java主代码目录及一个名为App的类
  • src/test/java测试代码目录及一个名为AppTest的测试用例

maven-archetype-webapp:一个最简单的Maven war项目模板,当需要快速创建一个Web应用的时候可以使用它。生成的项目内容包括:

  • 一个packaging为war且带有junit依赖声明的pom.xml
  • src/main/webapp/目录
  • src/main/webapp/index.jsp文件
  • src/main/webapp/WEB-INF/web.xml文件

交互模式创建工程

使用archetype创建project包括3个步骤:

  • 选择archetype
    输入命令mvn archetype:generate后,Archetype插件会列出一个Archetype列表供用户选择,选择想要使用的Archetype,输入对应编号,或输入filter过滤器筛选archetype。
    命令行会提供一个默认的编号,其对应的Archetype为maven-archetype-quickstart,提示Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 15:中的15即maven-archetype-quickstart的编号。
  • 配置archetype
    选择archetype后,按照提示输入一些基本参数,包括groupId,artifactId(工程名),version,package等
  • 创建工程
    输入archetype配置信息后,Archetype插件就会在当前目录下创建工程框架,下载依赖包

使用过滤器filter筛选archetype列表

mvn archetype:generate命令列出的archetype列表太长,不容易找到想要的archetype,可以使用过滤器filter来筛选archetype,格式为:[groupId:]artifactId,如果不加冒号,只匹配artifactId。过滤器是大小写敏感的。

有两种使用filter的方式:

  • 在命令参数中指定:例如mvn archetype:generate -Dfilter=org.apache:struts
  • 在交互式提示符中指定:输入mvn archetype:generate命令列出archetype列表后,会提示Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 15:,在此提示符后输入过滤器:org.apache:struts

交互模式创建archetype工程实例

例如创建maven-archetype-quickstart工程,groupId为com.company,artifactId为project,version为1.0,package为com.company.project

$ mvn archetype:generate
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [archetype:create] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] Preparing archetype:generate
[INFO] No goals needed for project - skipping
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] [archetype:generate]
Choose archetype:
1: internal -> org.appfuse.archetypes:appfuse-basic-jsf (AppFuse archetype for creating a web application with Hibernate, Spring and JSF)
... ...
10: internal -> org.apache.maven.archetypes:maven-archetype-j2ee-simple (A simple J2EE Java application)
... ...
15: internal -> org.apache.maven.archetypes:maven-archetype-quickstart ()
16: internal -> org.apache.maven.archetypes:maven-archetype-site-simple (A simple site generation project)
17: internal -> org.apache.maven.archetypes:maven-archetype-site (A more complex site project)
18: internal -> org.apache.maven.archetypes:maven-archetype-webapp (A simple Java web application)
19: internal -> org.apache.struts:struts2-archetype-starter (A starter Struts 2 application with Sitemesh, DWR, and Spring)
... ...
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 15: 
Choose org.apache.maven.archetypes:maven-archetype-quickstart version: 
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
Choose a number: 6: 
Define value for groupId: : com.company 
Define value for artifactId: : project
Define value for version: : 1.0
Define value for package: : com.company.project
Confirm properties configuration:
groupId: com.company
artifactId: project
version: 1.0
package: com.company.project
 Y: : 
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating OldArchetype: maven-archetype-quickstart:RELEASE
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.company
[INFO] Parameter: packageName, Value: com.company.project
[INFO] Parameter: package, Value: com.company.project
[INFO] Parameter: artifactId, Value: project
[INFO] Parameter: basedir, Value: /home/local/rafale/projects/tmp
[INFO] Parameter: version, Value: 1.0
[INFO] OldArchetype created in dir: /home/local/rafale/projects/tmp/project
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 54 seconds
[INFO] Finished at: Fri Aug 26 23:01:01 GMT 2011
[INFO] Final Memory: 10M/25M
[INFO] ------------------------------------------------------------------------

创建完后项目project的目录树

$ tree project
project
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- company
    |               `-- project
    |                   `-- App.java
    `-- test
        `-- java
            `-- com
                `-- company
                    `-- project
                        `-- AppTest.java

11 directories, 3 files

命令模式创建工程

mvn archetype:generate命令加-B参数可禁用交互模式,同时需提供如下参数:
选择archetype需要的archetypeGroupId,archetypeArtifactId,以及archetypeVersion参数
配置archetype需要的groupId,artifactId,version,package参数

例如:mvn archetype:generate -B -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.1 -DgroupId=com.company -DartifactId=project -Dversion=1.0-SNAPSHOT -Dpackage=com.company.project

$ mvn archetype:generate -B -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.1 -DgroupId=com.company -DartifactId=project -Dversion=1.0-SNAPSHOT -Dpackage=com.company.project
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>>
[INFO] 
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<<
[INFO] 
[INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
[INFO] Archetype repository missing. Using the one from [org.apache.maven.archetypes:maven-archetype-quickstart:1.1] found in catalog remote
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.1
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.company
[INFO] Parameter: packageName, Value: com.company.project
[INFO] Parameter: package, Value: com.company.project
[INFO] Parameter: artifactId, Value: project
[INFO] Parameter: basedir, Value: /Users/maven/dev
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /Users/maven/dev/project
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.184s
[INFO] Finished at: Mon Feb 04 11:53:24 CET 2013
[INFO] Final Memory: 9M/265M
[INFO] ------------------------------------------------------------------------

其中多数参数有默认值可不指定:
mvn archetype:generate -B -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.mycompany.app -DartifactId=my-app-simple -Dversion=1.0


参考


上一篇 Hexo博客(14)添加来必力评论系统

下一篇 Redis-基础

阅读
1,356
阅读预计7分钟
创建日期 2017-07-06
修改日期 2017-08-31
类别
百度推荐