打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
maven自定义骨架的过程,已经使用详解

maven骨架是骨架插件实现的,整个骨架的流程用下面的整个流程图表示在准确不过了,图来源maven官网。



maven骨架介绍:

使用mave做创建项目的时候,很自然用用到maven自带的骨架,例如:

Maven Archetypes

Maven provides several achetype artifacts:

Archetype ArtifactIdsDescription
maven-archetype-archetypeAn archetype to generate a sample archetype.
maven-archetype-j2ee-simpleAn archetype to generate a simplifed sample J2EE application.
maven-archetype-mojo (deprecated)Deprecated in favour of maven-archetype-plugin, which has a better name.
maven-archetype-pluginAn archetype to generate a sample Maven plugin.
maven-archetype-plugin-siteAn archetype to generate a sample Maven plugin site.
maven-archetype-portletAn archetype to generate a sample JSR-268 Portlet.
maven-archetype-quickstartAn archetype to generate a sample Maven project.
maven-archetype-simpleAn archetype to generate a simple Maven project.
maven-archetype-siteAn archetype to generate a sample Maven site which demonstrates some of the supported document types like APT, XDoc, and FML and demonstrates how to i18n your site.
maven-archetype-site-simpleAn archetype to generate a sample Maven site.
maven-archetype-webappAn archetype to generate a sample Maven Webapp project.

常见的如:maven-archetype-webapp,maven-archetype-simple。


maven骨架插件所有的命令如下:

Plugin Documentation

Goals available for this plugin:
GoalDescription
archetype:crawlCrawl a Maven repository (filesystem, not HTTP) and creates acatalog file.
archetype:createDeprecated. Please use the generate mojo instead.
archetype:create-from-project

Creates an archetype project from the current project.

This goal reads your source and resource files, the values ofits parameters, and properties you specify in a.property file, and uses them to create a Mavenarchetype project using the maven-archetype packaging. If you buildthe resulting project, it will create the archetype. You can thenuse this archetype to create new projects that resemble theoriginal.

The maven-archetype-plugin uses Velocity to expand templatefiles, and this documentation talks about 'Velocity Properties',which are values substituted into Velocity templates. SeeTheVelocity User's Guide for more information.

This goal modifies the text of the files of the current projectto form the Velocity template files that make up the archetype.

GAV
The GAV values for the current project are replaced byproperties: groupId, artifactId, and version. The user chooses newvalues for these when generating a project from the archetype.
package
All the files under one specified Java (or cognate) package arerelocated to a project that the user chooses when generating aproject. References to the class name are replaced by a propertyreference. For example, if the current project's sources are in thepackage org.apache.saltedpeanuts, then any example ofthe string org.apache.saltedpeanuts is replaced withthe Velocity property reference ${packageName}. Whenthe user generates a project, this is in turn replaced by his orher choice of a package.
custom properties
You may identify additional strings that should be replaced byparameters. To add custom properties, you must use thepropertyFile parameter to specify a property file. Seethe documentation forpropertyFile for thedetails.

Note that you may need to edit the results of this goal. Thisgoal has no way to exclude unwanted files, or add copyright noticesto the Velocity templates, or add more complex elements to thearchetype metadata file.

This goal also generates a simple integration-test thatexercises the generated archetype.

archetype:generateGenerates a new project from an archetype, or updates the actualproject if using a partial archetype. If the project is fullygenerated, it is generated in a directory corresponding to itsartifactId. If the project is updated with a partial archetype, itis done in the current directory.
archetype:helpDisplay help information on maven-archetype-plugin.
Call mvn archetype:help -Ddetail=true-Dgoal=<goal-name> to display parameter details.
archetype:integration-test

Execute the archetype integration tests, consisting ingenerating projects from the current archetype and optionallycomparing generated projects with reference copy.

Each IT consists of a sub-directory insrc/test/resources/projects containing:

  • a goal.txt file, containing a list of goals to runagainst the generated project (can be empty, content ignored beforemaven-archetype-plugin 2.1),
  • an archetype.properties file, containingproperties for project generation,
  • an optional reference/ directory containing areference copy of the expected project created from the IT.
Notice that it is expected to be run as part as of a build afterthe package phase and not directly as a goal from CLI.
archetype:jarBuild a JAR from the current Archetype project.
archetype:update-local-catalogUpdates the local catalog


首先创建自己的骨架之前先必会的是一个命令是:

archetype:generate

          此命令是作用是:通过骨架信息创建maven项目。

  1. $ mvn archetype:generate  
  2. [INFO] Scanning for projects...  
  3. [INFO] Searching repository for plugin with prefix: 'archetype'.  
  4. [INFO] ------------------------------------------------------------------------  
  5. [INFO] Building Maven Default Project  
  6. [INFO]    task-segment: [archetype:create] (aggregator-style)  
  7. [INFO] ------------------------------------------------------------------------  
  8. [INFO] Preparing archetype:generate  
  9. [INFO] No goals needed for project - skipping  
  10. [INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.  
  11. [INFO] Setting property: velocimacro.messages.on => 'false'.  
  12. [INFO] Setting property: resource.loader => 'classpath'.  
  13. [INFO] Setting property: resource.manager.logwhenfound => 'false'.  
  14. [INFO] [archetype:generate]  
  15. Choose archetype:  
  16. 1: internal -> org.appfuse.archetypes:appfuse-basic-jsf (AppFuse archetype for creating a web application with Hibernate, Spring and JSF)  
  17. 2: internal -> org.appfuse.archetypes:appfuse-basic-spring (AppFuse archetype for creating a web application with Hibernate, Spring and Spring MVC)  
  18. 3: internal -> org.appfuse.archetypes:appfuse-basic-struts (AppFuse archetype for creating a web application with Hibernate, Spring and Struts 2)  
  19. 4: internal -> org.appfuse.archetypes:appfuse-basic-tapestry (AppFuse archetype for creating a web application with Hibernate, Spring and Tapestry 4)  
  20. 5: internal -> org.appfuse.archetypes:appfuse-core (AppFuse archetype for creating a jar application with Hibernate and Spring and XFire)  
  21. 6: internal -> org.appfuse.archetypes:appfuse-modular-jsf (AppFuse archetype for creating a modular application with Hibernate, Spring and JSF)  
  22. 7: internal -> org.appfuse.archetypes:appfuse-modular-spring (AppFuse archetype for creating a modular application with Hibernate, Spring and Spring MVC)  
  23. 8: internal -> org.appfuse.archetypes:appfuse-modular-struts (AppFuse archetype for creating a modular application with Hibernate, Spring and Struts 2)  
  24. 9: internal -> org.appfuse.archetypes:appfuse-modular-tapestry (AppFuse archetype for creating a modular application with Hibernate, Spring and Tapestry 4)  
  25. 10: internal -> org.apache.maven.archetypes:maven-archetype-j2ee-simple (A simple J2EE Java application)  
  26. 11: internal -> org.apache.maven.archetypes:maven-archetype-marmalade-mojo (A Maven plugin development project using marmalade)  
  27. 12: internal -> org.apache.maven.archetypes:maven-archetype-mojo (A Maven Java plugin development project)  
  28. 13: internal -> org.apache.maven.archetypes:maven-archetype-portlet (A simple portlet application)  
  29. 14: internal -> org.apache.maven.archetypes:maven-archetype-profiles ()  
  30. 15: internal -> org.apache.maven.archetypes:maven-archetype-quickstart ()  
  31. 16: internal -> org.apache.maven.archetypes:maven-archetype-site-simple (A simple site generation project)  
  32. 17: internal -> org.apache.maven.archetypes:maven-archetype-site (A more complex site project)  
  33. 18: internal -> org.apache.maven.archetypes:maven-archetype-webapp (A simple Java web application)  
  34. 19: internal -> org.apache.struts:struts2-archetype-starter (A starter Struts 2 application with Sitemesh, DWR, and Spring)  
  35. 20: internal -> org.apache.struts:struts2-archetype-blank (A minimal Struts 2 application)  
  36. 21: internal -> org.apache.struts:struts2-archetype-portlet (A minimal Struts 2 application that can be deployed as a portlet)  
  37. 22: internal -> org.apache.struts:struts2-archetype-dbportlet (A starter Struts 2 portlet that demonstrates a simple CRUD interface with db backing)  
  38. 23: internal -> org.apache.struts:struts2-archetype-plugin (A Struts 2 plugin)  
  39. Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 15:   
  40. Choose org.apache.maven.archetypes:maven-archetype-quickstart version:   
  41. 1: 1.0-alpha-1  
  42. 2: 1.0-alpha-2  
  43. 3: 1.0-alpha-3  
  44. 4: 1.0-alpha-4  
  45. 5: 1.0  
  46. 6: 1.1  
  47. Choose a number: 6:   
  48. Define value for groupId: : com.company   
  49. Define value for artifactId: : project  
  50. Define value for version: : 1.0  
  51. Define value for package: : com.company.project  
  52. Confirm properties configuration:  
  53. groupId: com.company  
  54. artifactId: project  
  55. version: 1.0  
  56. package: com.company.project  
  57.  Y: :   
  58. [INFO] ----------------------------------------------------------------------------  
  59. [INFO] Using following parameters for creating OldArchetype: maven-archetype-quickstart:RELEASE  
  60. [INFO] ----------------------------------------------------------------------------  
  61. [INFO] Parameter: groupId, Value: com.company  
  62. [INFO] Parameter: packageName, Value: com.company.project  
  63. [INFO] Parameter: package, Value: com.company.project  
  64. [INFO] Parameter: artifactId, Value: project  
  65. [INFO] Parameter: basedir, Value: /home/local/rafale/projects/tmp  
  66. [INFO] Parameter: version, Value: 1.0  
  67. [INFO] OldArchetype created in dir: /home/local/rafale/projects/tmp/project  
  68. [INFO] ------------------------------------------------------------------------  
  69. [INFO] BUILD SUCCESSFUL  
  70. [INFO] ------------------------------------------------------------------------  
  71. [INFO] Total time: 54 seconds  
  72. [INFO] Finished at: Fri Aug 26 23:01:01 GMT 2011  
  73. [INFO] Final Memory: 10M/25M  
  74. [INFO] ------------------------------------------------------------------------  

它会让你选择你需要的maven骨架,然后输入groupId,packageName,package,artifactId,version。之后就在你所在的目录创建了maven项目。


根据咱们自定义的项目创建咱们自己的maven骨架,也就是第二个重要的命令:

archetype:create-from-project

自定义项目的目录结构:

此项目的pom文件内容:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   <groupId>com.lgy.archetype-demo</groupId>  
  5.   <artifactId>archetype-demo</artifactId>  
  6.   <packaging>war</packaging>  
  7.   <version>1.0.0-RELEASE</version>  
  8.   <name>archetype-demo Maven Webapp</name>  
  9.   <url>http://maven.apache.org</url>  
  10.     
  11.   <dependencies>  
  12.     <dependency>  
  13.       <groupId>junit</groupId>  
  14.       <artifactId>junit</artifactId>  
  15.       <version>3.8.1</version>  
  16.       <scope>test</scope>  
  17.     </dependency>  
  18.   </dependencies>  
  19.     
  20.   <build>  
  21.     <finalName>archetype-demo</finalName>  
  22.   </build>  
  23. </project>  

注意version类型一定是RELEASE版本


进入此项目的根目录,执行命令:

  1. mvn archetype:create-from-project  
  2. [INFO] Scanning for projects...  
  3. [INFO] Searching repository for plugin with prefix: 'archetype'.  
  4. [INFO] ------------------------------------------------------------------------  
  5. [INFO] Building project  
  6. [INFO]    task-segment: [archetype:create-from-project]  
  7. [INFO] ------------------------------------------------------------------------  
  8. [INFO] Preparing archetype:create-from-project  
  9. [INFO] No goals needed for project - skipping  
  10. [INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.  
  11. [INFO] Setting property: velocimacro.messages.on => 'false'.  
  12. [INFO] Setting property: resource.loader => 'classpath'.  
  13. [INFO] Setting property: resource.manager.logwhenfound => 'false'.  
  14. [INFO] [archetype:create-from-project]  
  15. [INFO] Setting default archetype's groupId: com.company  
  16. [INFO] Setting default archetype's artifactId: project  
  17. [INFO] Setting default archetype's version: 1.0-SNAPSHOT  
  18. [INFO] Setting default groupId: com.company  
  19. [INFO] Setting default artifactId: project  
  20. [INFO] Setting default version: 1.0-SNAPSHOT  
  21. [INFO] Setting default package: com.company.project  
  22. [INFO] Archetype created in target/generated-sources/archetype  
  23. [INFO] ------------------------------------------------------------------------  
  24. [INFO] BUILD SUCCESSFUL  
  25. [INFO] ------------------------------------------------------------------------  
  26. [INFO] Total time: 4 seconds  
  27. [INFO] Finished at: Fri Feb 22 21:49:52 GMT 2008  
  28. [INFO] Final Memory: 10M/26M  
  29. [INFO] ------------------------------------------------------------------------  


此时会在该项目的target目录下发现如下文件:

---target
    ---generated-sources
            ---archetype
                ---our project
进入到archetype目录下,也就是到达我们项目的根目录执行:

  1. $ cd target/generated-sources/archetype/  
  2. $ mvn install  
  3. [INFO] Scanning for projects...  
  4. [INFO] ------------------------------------------------------------------------  
  5. [INFO] Building project  
  6. [INFO]    task-segment: [install]  
  7. [INFO] ------------------------------------------------------------------------  
  8. [INFO] [resources:resources]  
  9. [INFO] Using default encoding to copy filtered resources.  
  10. [INFO] [resources:testResources]  
  11. [INFO] Using default encoding to copy filtered resources.  
  12. [INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.  
  13. [INFO] Setting property: velocimacro.messages.on => 'false'.  
  14. [INFO] Setting property: resource.loader => 'classpath'.  
  15. [INFO] Setting property: resource.manager.logwhenfound => 'false'.  
  16. [INFO] [archetype:jar]  
  17. [INFO] [archetype:add-archetype-metadata]  
  18. [INFO] [archetype:integration-test]  
  19. [INFO] [install:install]  
  20. [INFO] Installing /home/local/rafale/projects/tmp/project/target/generated-sources/archetype/target/project-1.0-SNAPSHOT.jar to /home/rafale/.m2/repository/com/company/project/1.0-SNAPSHOT/project-1.0-SNAPSHOT.jar  
  21. [INFO] [archetype:update-local-catalog]  
  22. [INFO] ------------------------------------------------------------------------  
  23. [INFO] BUILD SUCCESSFUL  
  24. [INFO] ------------------------------------------------------------------------  
  25. [INFO] Total time: 10 seconds  
  26. [INFO] Finished at: Fri Feb 22 21:51:57 GMT 2008  
  27. [INFO] Final Memory: 14M/35M  
  28. [INFO] ------------------------------------------------------------------------  

此时咱们的maven骨架就已经搭建好了。


执行mvn:archetype:crawl命令,第三个重要的命令:

              会发现在咱们的本地仓库的根目录生成archetype-catalog.xml骨架配置文件。查看此文件内容如下:

  1. <archetype>  
  2.       <groupId>com.lgy.archetype-demo</groupId>  
  3.       <artifactId>archetype-demo-archetype</artifactId>  
  4.       <version>1.0.0-RELEASE</version>  
  5.       <description>archetype-demo</description>  
  6.     </archetype>  

引用该骨架文件,在eclipse中选择该骨架,则可发现项目创建后就是咱们的骨架项目模板。


*当使用archetype:generate命令创建maven骨架项目的时候,发现有的时候骨架太多,咱们就可以使用filter过lv掉,参考maven该命令参数:

例如:

mvn archetype:generate -DarchetypeCatelog=local        本地

mvn archetype:generate -DarchetypeCatelog=romote        远程

mvn archetype:generate -DarchetypeCatelog=http:localhost:8080/archetype-catalog.xml        远程


参考资料https://maven.apache.org/archetype/maven-archetype-plugin/usage.html maven官网

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
我的Maven之旅(6)-通过Maven创建项目
Seven simple reasons to use AppFuse
Appfuse2再论
Appfuse 2.0简单开始和源码的下载
maven
J2EE开发之常用开源项目介绍
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服