English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Maven uses archetype (prototype) to create a custom project structure, forming a Maven project template.
In the previous chapters, we learned that Maven uses the following command to quickly create a java project:
mvn archetype:generate
An archetype, also known as a prototype, is a Maven plugin, to be precise, a project template, whose task is to create a project structure based on a template. We will use the quickstart archetype plugin to create a simple java application.
Let's open the command console, navigate to C:\> MVN directory and execute the following mvn command:
C:\MVN> mvn archetype:generate
Maven will start processing and ask to select the required archetype:
[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-原型(-) 601: remote -> org.trailsframework:trails-secure-原型(-) 602: remote -> org.tynamo:tynamo-原型(-) 603: remote -> org.wicketstuff.scala:wicket-scala-原型(-) 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-公共的-组件-archetype (使其更容易创建用于创建XWiki组件的Maven项目。) 609: remote -> org.xwiki.rendering:xwiki-渲染-archetype-宏 (使其更容易创建用于创建XWiki渲染宏的Maven项目。) 610: remote -> org.zkoss:zk-archetype-组件(ZK组件原型) 611: remote -> org.zkoss:zk-archetype-webapp(ZK webapp原型) 612: remote -> ru.circumflex:circumflex-原型(-) 613: remote -> se.vgregion.javg.maven.archetypes:javg-minimal-原型(-) 614: remote -> sk.seges.sesam:sesam-注解-原型(-) 选择一个数字或应用过滤器 (格式:[groupId:]artifactId,区分大小写,包含): 203:
按下 Enter 选择默认选项 (203:maven-archetype-quickstart)。
选择org.apache.maven.archetypes:maven-archetype-quickstart版本: 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 选择一个数字: 6:
按下 Enter 选择默认选项 (6:maven-archetype-quickstart:1.1)
Maven将询问项目细节。请按照要求输入项目细节。如果要使用默认值,则直接按Enter键。您也可以输入自己的值。
定义属性'groupId'的值:: com.companyname.insurance 定义属性'artifactId'的值:: health 定义属性'version'的值: 1.0-SNAPSHOT 定义属性'package'的值:com.companyname.insurance
Maven will ask to confirm the project details, press}} Enter or press Y
Confirm properties configuration: groupId: com.companyname.insurance artifactId: health version: 1.0-SNAPSHOT package: com.companyname.insurance Y:
Now Maven will start creating the project structure, as shown below:
[INFO] ----------------------------------------------------------------------- [INFO] Using the following parameters for creating the 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] -----------------------------------------------------------------------
Now navigate to the C:\ > MVN directory. You will see a java application project named health, just like the artifactId name established when the project was created. Maven will create a project with a standard directory layout, as shown below:
Maven automatically generates a pom.xml file for the project as follows:
<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>
Maven will automatically generate a test java file App.java.
Path: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!"); } }
Maven will automatically generate a java file AppTest.java.
Path is: 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 ); } }
That's it. Now you can see the power of Maven. You can use simple maven commands to create any type of project and start your development.