maven 项目模板
maven 使用 archetype(原型) 来创建自定义的项目结构,形成 maven 项目模板。
maven 使用下面的命令来快速创建 java 项目:
mvn archetype:generate
1. 什么是 archetype
archetype 也就是原型,是一个 maven 插件,准确说是一个项目模板,它的任务是根据模板创建一个项目结构。我们将使用 quickstart 原型插件创建一个简单的 java 应用程序。
2. 使用项目模板
让我们打开命令控制台,跳转到 c:\> mvn 目录并执行以下 mvn 命令:
c:\mvn> mvn archetype:generate
maven 将开始处理,并要求选择所需的原型:
[info] scanning for projects... [info] searching repository for plugin with prefix: 'archetype'. [info] ------------------------------------------------------------------- [info] building maven default project [info]task-segment: [archetype:generate] (aggregator-style) [info] ------------------------------------------------------------------- [info] preparing archetype:generate ... 600: remote -> org.trailsframework:trails-archetype (-) 601: remote -> org.trailsframework:trails-secure-archetype (-) 602: remote -> org.tynamo:tynamo-archetype (-) 603: remote -> org.wicketstuff.scala:wicket-scala-archetype (-) 604: remote -> org.wicketstuff.scala:wicketstuff-scala-archetype basic setup for a project that combines scala and wicket, depending on the wicket-scala project. includes an example specs test.) 605: remote -> org.wikbook:wikbook.archetype (-) 606: remote -> org.xaloon.archetype:xaloon-archetype-wicket-jpa-glassfish (-) 607: remote -> org.xaloon.archetype:xaloon-archetype-wicket-jpa-spring (-) 608: remote -> org.xwiki.commons:xwiki-commons-component-archetype (make it easy to create a maven project for creating xwiki components.) 609: remote -> org.xwiki.rendering:xwiki-rendering-archetype-macro (make it easy to create a maven project for creating xwiki rendering macros.) 610: remote -> org.zkoss:zk-archetype-component (the zk component archetype) 611: remote -> org.zkoss:zk-archetype-webapp (the zk wepapp archetype) 612: remote -> ru.circumflex:circumflex-archetype (-) 613: remote -> se.vgregion.javg.maven.archetypes:javg-minimal-archetype (-) 614: remote -> sk.seges.sesam:sesam-annotation-archetype (-) choose a number or apply filter (format: [groupid:]artifactid, case sensitive contains): 203:
按下 enter 选择默认选项 (203:maven-archetype-quickstart)。
3. maven 将询问原型的版本
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:
按下 enter 选择默认选项 (6:maven-archetype-quickstart:1.1)
maven 将询问项目细节。按要求输入项目细节。如果要使用默认值则直接按 enter 键。你也可以输入自己的值。
define value for property 'groupid': : com.companyname.insurance define value for property 'artifactid': : health define value for property 'version': 1.0-snapshot define value for property 'package': com.companyname.insurance
maven 将要求确认项目细节,按 enter 或按 y
confirm properties configuration: groupid: com.companyname.insurance artifactid: health version: 1.0-snapshot package: com.companyname.insurance y:
现在 maven 将开始创建项目结构,显示如下:
[info] ----------------------------------------------------------------------- [info] using following parameters for creating project from old (1.x) archetype: maven-archetype-quickstart:1.1 [info] ----------------------------------------------------------------------- [info] parameter: groupid, value: com.companyname.insurance [info] parameter: packagename, value: com.companyname.insurance [info] parameter: package, value: com.companyname.insurance [info] parameter: artifactid, value: health [info] parameter: basedir, value: c:\mvn [info] parameter: version, value: 1.0-snapshot [info] project created from old (1.x) archetype in dir: c:\mvn\health [info] ----------------------------------------------------------------------- [info] build successful [info] ----------------------------------------------------------------------- [info] total time: 4 minutes 12 seconds [info] finished at: fri jul 13 11:10:12 ist 2012 [info] final memory: 20m/90m [info] -----------------------------------------------------------------------
4. 创建的项目
现在转到 c:\ > mvn 目录。你会看到一个名为 health 的 java 应用程序项目,就像在项目创建的时候建立的 artifactid 名称一样。 maven 将创建一个有标准目录布局的项目,如下所示:
5. 创建 pom.xml
maven 为项目自动生成一个 pom.xml文件,如下所示:
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.companyname.insurance</groupid> <artifactid>health</artifactid> <version>1.0-snapshot</version> <packaging>jar</packaging> <name>health</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> </properties> <dependencies> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
6. app.java
maven 会自动生成一个测试的 java 文件 app.java。
路径:c:\mvn\consumerbanking\src\main\java\com\companyname\bank
package com.companyname.insurance; /** * hello world! * */ public class app { public static void main( string[] args ) { system.out.println( "hello world!" ); } }
7. apptest.java
maven 会自动生成一个 java 文件 apptest.java。
路径为: c:\mvn\consumerbanking\src\test\java\com\companyname\bank
package com.companyname.insurance; import junit.framework.test; import junit.framework.testcase; import junit.framework.testsuite; /** * unit test for simple app. */ public class apptest extends testcase { /** * create the test case * * @param testname name of the test case */ public apptest( string testname ) { super( testname ); } /** * @return the suite of tests being tested */ public static test suite() { return new testsuite( apptest.class ); } /** * rigourous test :-) */ public void testapp() { asserttrue( true ); } }
就这样。现在你可以看到 maven 的强大之处。你可以用 maven 简单的命令创建任何类型的项目,并且可以启动您的开发。