课程表

Maven课程

工具箱
速查手册

Maven 工程模版

当前位置:免费教程 » 软件/图像 » Maven

Maven 使用原型(Archetype)概念为用户提供了大量不同类型的工程模版(614 个)。Maven 使用下面的命令帮助用户快速创建 java 项目。

  1. mvn archetype:generate

什么是原型?

原型是一个 Maven 插件,它的任务是根据模板创建一个项目结构。我们将使用 quickstart 原型插件创建一个简单的 java 应用程序。

使用工程模板

让我们打开命令控制台,跳转到 C:\ > MVN 目录并执行以下 mvn 命令

  1. C:\MVN>mvn archetype:generate

Maven 将开始处理,并要求选择所需的原型

  1. INFO] Scanning for projects...
  2. [INFO] Searching repository for plugin with prefix: 'archetype'.
  3. [INFO] -------------------------------------------------------------------
  4. [INFO] Building Maven Default Project
  5. [INFO]task-segment: [archetype:generate] (aggregator-style)
  6. [INFO] -------------------------------------------------------------------
  7. [INFO] Preparing archetype:generate
  8. ...
  9. 600: remote -> org.trailsframework:trails-archetype (-)
  10. 601: remote -> org.trailsframework:trails-secure-archetype (-)
  11. 602: remote -> org.tynamo:tynamo-archetype (-)
  12. 603: remote -> org.wicketstuff.scala:wicket-scala-archetype (-)
  13. 604: remote -> org.wicketstuff.scala:wicketstuff-scala-archetype
  14. Basic setup for a project that combines Scala and Wicket,
  15. depending on the Wicket-Scala project.
  16. Includes an example Specs test.)
  17. 605: remote -> org.wikbook:wikbook.archetype (-)
  18. 606: remote -> org.xaloon.archetype:xaloon-archetype-wicket-jpa-glassfish (-)
  19. 607: remote -> org.xaloon.archetype:xaloon-archetype-wicket-jpa-spring (-)
  20. 608: remote -> org.xwiki.commons:xwiki-commons-component-archetype
  21. (Make it easy to create a maven project for creating XWiki Components.)
  22. 609: remote -> org.xwiki.rendering:xwiki-rendering-archetype-macro
  23. (Make it easy to create a maven project for creating XWiki Rendering Macros.)
  24. 610: remote -> org.zkoss:zk-archetype-component (The ZK Component archetype)
  25. 611: remote -> org.zkoss:zk-archetype-webapp (The ZK wepapp archetype)
  26. 612: remote -> ru.circumflex:circumflex-archetype (-)
  27. 613: remote -> se.vgregion.javg.maven.archetypes:javg-minimal-archetype (-)
  28. 614: remote -> sk.seges.sesam:sesam-annotation-archetype (-)
  29. Choose a number or apply filter
  30. (format: [groupId:]artifactId, case sensitive contains): 203:

按下 Enter 选择默认选项 (203:maven-archetype-quickstart)

Maven 将询问原型的版本

  1. Choose org.apache.maven.archetypes:maven-archetype-quickstart version:
  2. 1: 1.0-alpha-1
  3. 2: 1.0-alpha-2
  4. 3: 1.0-alpha-3
  5. 4: 1.0-alpha-4
  6. 5: 1.0
  7. 6: 1.1
  8. Choose a number: 6:

按下 Enter 选择默认选项 (6:maven-archetype-quickstart:1.1)

Maven 将询问工程细节。按要求输入工程细节。如果要使用默认值则直接按 Enter 键。你也可以输入自己的值。

  1. Define value for property 'groupId': : com.companyname.insurance
  2. Define value for property 'artifactId': : health
  3. Define value for property 'version': 1.0-SNAPSHOT:
  4. Define value for property 'package': com.companyname.insurance:

Maven 将要求确认工程细节。按 enter 或按 Y

  1. Confirm properties configuration:
  2. groupId: com.companyname.insurance
  3. artifactId: health
  4. version: 1.0-SNAPSHOT
  5. package: com.companyname.insurance
  6. Y:

现在 Maven 将开始创建工程结构,显示如下:

  1. [INFO] -----------------------------------------------------------------------
  2. [INFO] Using following parameters for creating project
  3. from Old (1.x) Archetype: maven-archetype-quickstart:1.1
  4. [INFO] -----------------------------------------------------------------------
  5. [INFO] Parameter: groupId, Value: com.companyname.insurance
  6. [INFO] Parameter: packageName, Value: com.companyname.insurance
  7. [INFO] Parameter: package, Value: com.companyname.insurance
  8. [INFO] Parameter: artifactId, Value: health
  9. [INFO] Parameter: basedir, Value: C:\MVN
  10. [INFO] Parameter: version, Value: 1.0-SNAPSHOT
  11. [INFO] project created from Old (1.x) Archetype in dir: C:\MVN\health
  12. [INFO] -----------------------------------------------------------------------
  13. [INFO] BUILD SUCCESSFUL
  14. [INFO] -----------------------------------------------------------------------
  15. [INFO] Total time: 4 minutes 12 seconds
  16. [INFO] Finished at: Fri Jul 13 11:10:12 IST 2012
  17. [INFO] Final Memory: 20M/90M
  18. [INFO] -----------------------------------------------------------------------

创建的项目

现在转到 C:\ > MVN 目录。你会看到一个名为 health 的 java 应用程序项目,就像在项目创建的时候建立的 artifactId 名称一样。 Maven 将创建一个有标准目录布局的工程,如下所示:

创建 POM.xml

Maven 为工程生成一个 POM.xml文件,如下所列:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  4. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.companyname.insurance</groupId>
  7. <artifactId>health</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <packaging>jar</packaging>
  10. <name>health</name>
  11. <url>http://maven.apache.org</url>
  12. <properties>
  13. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14. </properties>
  15. <dependencies>
  16. <dependency>
  17. <groupId>junit</groupId>
  18. <artifactId>junit</artifactId>
  19. <version>3.8.1</version>
  20. <scope>test</scope>
  21. </dependency>
  22. </dependencies>
  23. </project>

创建 App.java

Maven 生成一个 java 源文件示例,工程的 App.java 如下所示:

路径:C:\ > MVN > health > src > main > java > com > companyname > insurance > App.java

  1. package com.companyname.insurance;
  2. /**
  3. * Hello world!
  4. *
  5. */
  6. public class App
  7. {
  8. public static void main( String[] args )
  9. {
  10. System.out.println( "Hello World!" );
  11. }
  12. }

创建 AppTest.java

Maven 生成一个 java 源文件示例,工程的 AppTest.java 如下所示:

路径为: C:\ > MVN > health > src > test > java > com > companyname > insurance > AppTest.java

  1. package com.companyname.insurance;
  2. import junit.framework.Test;
  3. import junit.framework.TestCase;
  4. import junit.framework.TestSuite;
  5. /**
  6. * Unit test for simple App.
  7. */
  8. public class AppTest
  9. extends TestCase
  10. {
  11. /**
  12. * Create the test case
  13. *
  14. * @param testName name of the test case
  15. */
  16. public AppTest( String testName )
  17. {
  18. super( testName );
  19. }
  20. /**
  21. * @return the suite of tests being tested
  22. */
  23. public static Test suite()
  24. {
  25. return new TestSuite( AppTest.class );
  26. }
  27. /**
  28. * Rigourous Test :-)
  29. */
  30. public void testApp()
  31. {
  32. assertTrue( true );
  33. }
  34. }

就这样。现在你可以看到 Maven 的强大之处。你可以用 maven 简单的命令创建任何类型的工程,并且可以启动您的开发。

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号