学习笔记:Maven的ArcheType的学习笔记

摘要:
    Archetype是什么?它由哪些文件组成?如何创建和安装自己的archtype,如何使用自己创建的archetype?



一、Archetype是什么
    Archetype其实就是一个Maven的Project模板,以方便用户快速创建maven的Project。有官方提供的Archetype,用户也可以创建自己的Archetype,比如我们公司的所有工程都使用相同的groupId,都将包含相同的库文件,那么我们就可以创建一个自己的archetype,然后大家下载过去安装在自己的本地。或者放在一个内部服务器上,然后大家在自己的eclipse上配置好该服务器catogries.xml文件的路径;

    下图是官方提供的Archetype的生命周期,现在只要了解,后续将详细介绍。



二、Archetype工程的组成
一个典型的archetype包含如下组成部分:
1)Archetype descriptor(archetype.xml),这个文件位于路径src/main/resources/META-INF/maven/那就除了所有将要包含在archetype中的文件,并且给他们做了分类以便Archetype的生成机制可以正确的处理它们;
2)Archetype插件将要拷贝的原型文件,位于路径,src/main/resources/archetype-resources/;
3)pom.xml原型文件,位于路径:src/main/resources/archetype-resources
4)这个工程本身自己的pom.xml;


三、如何生成Archetype
    首先基于一个用户已有的工程,使用mvn archetype:create-from-project 就可以在该project的target\generate-sources\archetype生成了一个archetype。
    比如我刚刚在gs-rest-service-yang工程内运行了mvn archetype:create-from-project命令,然后在target子目录下声称了generated-sources文件夹,文件夹内包含了archetype文件夹,该文件夹的内容如下:
  1. pom.xml
  2. ├─src
  3. ├─main
  4. └─resources
  5. ├─archetype-resources
  6. .classpath
  7. .project
  8. pom.xml
  9. ├─.settings
  10. org.eclipse.core.resources.prefs
  11. org.eclipse.jdt.core.prefs
  12. org.eclipse.m2e.core.prefs
  13. └─src
  14. ├─main
  15. └─java
  16. Application.java
  17. Quote.java
  18. Value.java
  19. └─test
  20. └─java
  21. AppTest.java
  22. └─META-INF
  23. └─maven
  24. archetype-metadata.xml
  25. └─test
  26. └─resources
  27. └─projects
  28. └─basic
  29. archetype.properties
  30. goal.txt
  31. └─target
  32. gs-rest-service-yang-archetype-0.0.1-SNAPSHOT.jar
  33. ├─classes
  34. ├─archetype-resources
  35. .classpath
  36. .project
  37. pom.xml
  38. ├─.settings
  39. org.eclipse.core.resources.prefs
  40. org.eclipse.jdt.core.prefs
  41. org.eclipse.m2e.core.prefs
  42. └─src
  43. ├─main
  44. └─java
  45. Application.java
  46. Quote.java
  47. Value.java
  48. └─test
  49. └─java
  50. AppTest.java
  51. └─META-INF
  52. └─maven
  53. archetype-metadata.xml
  54. ├─gs-rest-service-yang-archetype-0.0.1-SNAPSHOT
  55. ├─archetype-resources
  56. .classpath
  57. .project
  58. pom.xml
  59. ├─.settings
  60. org.eclipse.core.resources.prefs
  61. org.eclipse.jdt.core.prefs
  62. org.eclipse.m2e.core.prefs
  63. └─src
  64. ├─main
  65. └─java
  66. Application.java
  67. Quote.java
  68. Value.java
  69. └─test
  70. └─java
  71. AppTest.java
  72. └─META-INF
  73. └─maven
  74. archetype-metadata.xml
  75. └─test-classes
  76. └─projects
  77. └─basic
  78. archetype.properties
  79. goal.txt
可以看到上面有一些.setting, .class, .project的文件,这些是多余的。
所以我们在搭建好样板工程之后,在使用mvn archetype:create-from-project命令之前,要先把项目中不相关的工程文件、中间文件删除。
清除之后重新声称的结构如下:

  1. pom.xml
  2. ├─src
  3. ├─main
  4. └─resources
  5. ├─archetype-resources
  6. pom.xml
  7. └─src
  8. ├─main
  9. └─java
  10. Application.java
  11. Quote.java
  12. Value.java
  13. └─test
  14. └─java
  15. AppTest.java
  16. └─META-INF
  17. └─maven
  18. archetype-metadata.xml
  19. └─test
  20. └─resources
  21. └─projects
  22. └─basic
  23. archetype.properties
  24. goal.txt
  25. └─target
  26. gs-rest-service-yang-archetype-0.0.1-SNAPSHOT.jar
  27. ├─classes
  28. ├─archetype-resources
  29. pom.xml
  30. └─src
  31. ├─main
  32. └─java
  33. Application.java
  34. Quote.java
  35. Value.java
  36. └─test
  37. └─java
  38. AppTest.java
  39. └─META-INF
  40. └─maven
  41. archetype-metadata.xml
  42. └─test-classes
  43. └─projects
  44. └─basic
  45. archetype.properties
  46. goal.txt

四、生成Archetype如何安装
    在上文创建了archetype之后,进入到target\generate-sources\archetype目录,然后在命令行执行:
    mvn install
    则输出如下信息,特别是installing这段,表示就安装成功了。
  1. [INFO] Scanning for projects...
  2. [INFO]
  3. [INFO] ------------------------------------------------------------------------
  4. [INFO] Building gs-rest-service-yang-archetype 0.0.1-SNAPSHOT
  5. [INFO] ------------------------------------------------------------------------
  6. [INFO]
  7. [INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ gs-rest-service-yang-archetype ---
  8. [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
  9. [INFO] Copying 6 resources
  10. [INFO]
  11. [INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ gs-rest-service-yang-archetype ---
  12. [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
  13. [INFO] Copying 2 resources
  14. [INFO]
  15. [INFO] --- maven-archetype-plugin:3.0.0:jar (default-jar) @ gs-rest-service-yang-archetype ---
  16. [INFO] Building archetype jar: D:\WORK\personal\2.src\3.spring\gs-rest-service-yang-2\target\generated-sources\archetype
  17. \target\gs-rest-service-yang-archetype-0.0.1-SNAPSHOT
  18. [INFO]
  19. [INFO] --- maven-archetype-plugin:3.0.0:integration-test (default-integration-test) @ gs-rest-service-yang-archetype ---
  20. [INFO] Processing Archetype IT project: basic
  21. [INFO] ----------------------------------------------------------------------------
  22. [INFO] Using following parameters for creating project from Archetype: gs-rest-service-yang-archetype:0.0.1-SNAPSHOT
  23. [INFO] ----------------------------------------------------------------------------
  24. [INFO] Parameter: groupId, Value: archetype.it
  25. [INFO] Parameter: artifactId, Value: basic
  26. [INFO] Parameter: version, Value: 0.1-SNAPSHOT
  27. [INFO] Parameter: package, Value: it.pkg
  28. [INFO] Parameter: packageInPathFormat, Value: it/pkg
  29. [INFO] Parameter: version, Value: 0.1-SNAPSHOT
  30. [INFO] Parameter: package, Value: it.pkg
  31. [INFO] Parameter: groupId, Value: archetype.it
  32. [INFO] Parameter: artifactId, Value: basic
  33. [INFO] Project created from Archetype in dir: D:\WORK\personal\2.src\3.spring\gs-rest-service-yang-2\target\generated-so
  34. urces\archetype\target\test-classes\projects\basic\project\basic
  35. [INFO]
  36. [INFO] --- maven-install-plugin:2.5.2:install (default-install) @ gs-rest-service-yang-archetype ---
  37. [INFO] Installing D:\WORK\personal\2.src\3.spring\gs-rest-service-yang-2\target\generated-sources\archetype\target\gs-re
  38. st-service-yang-archetype-0.0.1-SNAPSHOT.jar to C:\Users\yangg\.m2\repository\com\huajiesolar\gs-rest-service-yang-arche
  39. type\0.0.1-SNAPSHOT\gs-rest-service-yang-archetype-0.0.1-SNAPSHOT.jar
  40. [INFO] Installing D:\WORK\personal\2.src\3.spring\gs-rest-service-yang-2\target\generated-sources\archetype\pom.xml to C
  41. :\Users\yangg\.m2\repository\com\huajiesolar\gs-rest-service-yang-archetype\0.0.1-SNAPSHOT\gs-rest-service-yang-archetyp
  42. e-0.0.1-SNAPSHOT.pom
  43. [INFO]
  44. [INFO] --- maven-archetype-plugin:3.0.0:update-local-catalog (default-update-local-catalog) @ gs-rest-service-yang-arche
  45. type ---
  46. [INFO] ------------------------------------------------------------------------
  47. [INFO] BUILD SUCCESS
  48. [INFO] ------------------------------------------------------------------------
  49. [INFO] Total time: 2.073 s
  50. [INFO] Finished at: 2017-02-21T22:35:47+08:00
  51. [INFO] Final Memory: 14M/153M
  52. [INFO] ------------------------------------------------------------------------

安装之后主要做了两件事情:
1) 将文件安装到mvn缓存目录,在我本机就是C:\Users\yangg\.m2\repository\com\huajiesolar\gs-rest-service-yang-archetype,该文件夹下的目录结构如下:
  1. | maven-metadata-local.xml
  2. |
  3. \---0.0.1-SNAPSHOT
  4. gs-rest-service-yang-archetype-0.0.1-SNAPSHOT.jar
  5. gs-rest-service-yang-archetype-0.0.1-SNAPSHOT.pom
  6. maven-metadata-local.xml
  7. _remote.repositories   
2)创建了文件 C:\Users\yangg\.m2\repository\archetype-catalog.xml文件,文件内容如下:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <archetype-catalog xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0 http://maven.apache.org/xsd/archetype-catalog-1.0.0.xsd"
  3. xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  5. <archetypes>
  6. <archetype>
  7. <groupId>com.huajiesolar</groupId>
  8. <artifactId>gs-rest-service-yang-archetype</artifactId>
  9. <version>0.0.1-SNAPSHOT</version>
  10. <description>Parent pom providing dependency and plugin management for applications
  11. built with Maven</description>
  12. </archetype>
  13. </archetypes>
  14. </archetype-catalog>

五、使用上文创建的archetype
有2种使用方式:
1)通过命令行使用
    找到一个空目录,然后执行如下命令:
    mvn archetype:generate -DarchetypeCatalog=local
    此时会给出上文安装的archetype,选择即可,如下:
  1. [INFO] Scanning for projects...
  2. [INFO]
  3. [INFO] ------------------------------------------------------------------------
  4. [INFO] Building Maven Stub Project (No POM) 1
  5. [INFO] ------------------------------------------------------------------------
  6. [INFO]
  7. [INFO] >>> maven-archetype-plugin:3.0.0:generate (default-cli) > generate-sources @ standalone-pom >>>
  8. [INFO]
  9. [INFO] <<< maven-archetype-plugin:3.0.0:generate (default-cli) < generate-sources @ standalone-pom <<<
  10. [INFO]
  11. [INFO] --- maven-archetype-plugin:3.0.0:generate (default-cli) @ standalone-pom ---
  12. [INFO] Generating project in Interactive mode
  13. [INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
  14. Choose archetype:
  15. 1: local -> com.huajiesolar:gs-rest-service-yang-archetype (Parent pom providing dependency and plugin management for applications
  16. built with Maven)
  17. Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): :
2)通过eclipse使用
    因为C:\Users\yangg\.m2\repository\archetype-catalog.xml是新创建的,所以需要在ecliplse的 file -》new-》maven projects对话框中,在选择archetype页面上,如下图
 点击上文的config按钮,弹出如下界面:
 点击add local catalog ...按钮,在弹出的对话框中选择上文新声称的catalog文件:
 点击Ok即可。

然后在界面上就可以选择新创建的archetype模板了。
 

  《Maven实战》











posted @ 2017-02-21 23:01  拿走不谢  阅读(5098)  评论(0编辑  收藏  举报