maven骨架是骨架插件實現(xiàn)的,整個骨架的流程用下面的整個流程圖表示在準確不過了,圖來源maven官網(wǎng)。
maven骨架介紹:
使用mave做創(chuàng)建項目的時候,很自然用用到maven自帶的骨架,例如:
Maven Archetypes
Maven provides several achetype artifacts:
常見的如:maven-archetype-webapp,maven-archetype-simple。
maven骨架插件所有的命令如下:
Plugin Documentation
Goals available for this plugin:
Goal | Description |
---|
archetype:crawl | Crawl a Maven repository (filesystem, not HTTP) and creates acatalog file. |
archetype:create | Deprecated. 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 the
propertyFile 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:generate | Generates 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:help | Display 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:jar | Build a JAR from the current Archetype project. |
archetype:update-local-catalog | Updates the local catalog |
首先創(chuàng)建自己的骨架之前先必會的是一個命令是:
archetype:generate
此命令是作用是:通過骨架信息創(chuàng)建maven項目。
- $ mvn archetype:generate
- [INFO] Scanning for projects...
- [INFO] Searching repository for plugin with prefix: 'archetype'.
- [INFO] ------------------------------------------------------------------------
- [INFO] Building Maven Default Project
- [INFO] task-segment: [archetype:create] (aggregator-style)
- [INFO] ------------------------------------------------------------------------
- [INFO] Preparing archetype:generate
- [INFO] No goals needed for project - skipping
- [INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
- [INFO] Setting property: velocimacro.messages.on => 'false'.
- [INFO] Setting property: resource.loader => 'classpath'.
- [INFO] Setting property: resource.manager.logwhenfound => 'false'.
- [INFO] [archetype:generate]
- Choose archetype:
- 1: internal -> org.appfuse.archetypes:appfuse-basic-jsf (AppFuse archetype for creating a web application with Hibernate, Spring and JSF)
- 2: internal -> org.appfuse.archetypes:appfuse-basic-spring (AppFuse archetype for creating a web application with Hibernate, Spring and Spring MVC)
- 3: internal -> org.appfuse.archetypes:appfuse-basic-struts (AppFuse archetype for creating a web application with Hibernate, Spring and Struts 2)
- 4: internal -> org.appfuse.archetypes:appfuse-basic-tapestry (AppFuse archetype for creating a web application with Hibernate, Spring and Tapestry 4)
- 5: internal -> org.appfuse.archetypes:appfuse-core (AppFuse archetype for creating a jar application with Hibernate and Spring and XFire)
- 6: internal -> org.appfuse.archetypes:appfuse-modular-jsf (AppFuse archetype for creating a modular application with Hibernate, Spring and JSF)
- 7: internal -> org.appfuse.archetypes:appfuse-modular-spring (AppFuse archetype for creating a modular application with Hibernate, Spring and Spring MVC)
- 8: internal -> org.appfuse.archetypes:appfuse-modular-struts (AppFuse archetype for creating a modular application with Hibernate, Spring and Struts 2)
- 9: internal -> org.appfuse.archetypes:appfuse-modular-tapestry (AppFuse archetype for creating a modular application with Hibernate, Spring and Tapestry 4)
- 10: internal -> org.apache.maven.archetypes:maven-archetype-j2ee-simple (A simple J2EE Java application)
- 11: internal -> org.apache.maven.archetypes:maven-archetype-marmalade-mojo (A Maven plugin development project using marmalade)
- 12: internal -> org.apache.maven.archetypes:maven-archetype-mojo (A Maven Java plugin development project)
- 13: internal -> org.apache.maven.archetypes:maven-archetype-portlet (A simple portlet application)
- 14: internal -> org.apache.maven.archetypes:maven-archetype-profiles ()
- 15: internal -> org.apache.maven.archetypes:maven-archetype-quickstart ()
- 16: internal -> org.apache.maven.archetypes:maven-archetype-site-simple (A simple site generation project)
- 17: internal -> org.apache.maven.archetypes:maven-archetype-site (A more complex site project)
- 18: internal -> org.apache.maven.archetypes:maven-archetype-webapp (A simple Java web application)
- 19: internal -> org.apache.struts:struts2-archetype-starter (A starter Struts 2 application with Sitemesh, DWR, and Spring)
- 20: internal -> org.apache.struts:struts2-archetype-blank (A minimal Struts 2 application)
- 21: internal -> org.apache.struts:struts2-archetype-portlet (A minimal Struts 2 application that can be deployed as a portlet)
- 22: internal -> org.apache.struts:struts2-archetype-dbportlet (A starter Struts 2 portlet that demonstrates a simple CRUD interface with db backing)
- 23: internal -> org.apache.struts:struts2-archetype-plugin (A Struts 2 plugin)
- Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 15:
- 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:
- Define value for groupId: : com.company
- Define value for artifactId: : project
- Define value for version: : 1.0
- Define value for package: : com.company.project
- Confirm properties configuration:
- groupId: com.company
- artifactId: project
- version: 1.0
- package: com.company.project
- Y: :
- [INFO] ----------------------------------------------------------------------------
- [INFO] Using following parameters for creating OldArchetype: maven-archetype-quickstart:RELEASE
- [INFO] ----------------------------------------------------------------------------
- [INFO] Parameter: groupId, Value: com.company
- [INFO] Parameter: packageName, Value: com.company.project
- [INFO] Parameter: package, Value: com.company.project
- [INFO] Parameter: artifactId, Value: project
- [INFO] Parameter: basedir, Value: /home/local/rafale/projects/tmp
- [INFO] Parameter: version, Value: 1.0
- [INFO] OldArchetype created in dir: /home/local/rafale/projects/tmp/project
- [INFO] ------------------------------------------------------------------------
- [INFO] BUILD SUCCESSFUL
- [INFO] ------------------------------------------------------------------------
- [INFO] Total time: 54 seconds
- [INFO] Finished at: Fri Aug 26 23:01:01 GMT 2011
- [INFO] Final Memory: 10M/25M
- [INFO] ------------------------------------------------------------------------
它會讓你選擇你需要的maven骨架,然后輸入groupId,packageName,package,artifactId,version。之后就在你所在的目錄創(chuàng)建了maven項目。
根據(jù)咱們自定義的項目創(chuàng)建咱們自己的maven骨架,也就是第二個重要的命令:
archetype:create-from-project
自定義項目的目錄結(jié)構(gòu):
此項目的pom文件內(nèi)容:
- <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/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.lgy.archetype-demo</groupId>
- <artifactId>archetype-demo</artifactId>
- <packaging>war</packaging>
- <version>1.0.0-RELEASE</version>
- <name>archetype-demo Maven Webapp</name>
- <url>http://maven.apache.org</url>
-
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
-
- <build>
- <finalName>archetype-demo</finalName>
- </build>
- </project>
注意version類型一定是RELEASE版本
進入此項目的根目錄,執(zhí)行命令:
- mvn archetype:create-from-project
- [INFO] Scanning for projects...
- [INFO] Searching repository for plugin with prefix: 'archetype'.
- [INFO] ------------------------------------------------------------------------
- [INFO] Building project
- [INFO] task-segment: [archetype:create-from-project]
- [INFO] ------------------------------------------------------------------------
- [INFO] Preparing archetype:create-from-project
- [INFO] No goals needed for project - skipping
- [INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
- [INFO] Setting property: velocimacro.messages.on => 'false'.
- [INFO] Setting property: resource.loader => 'classpath'.
- [INFO] Setting property: resource.manager.logwhenfound => 'false'.
- [INFO] [archetype:create-from-project]
- [INFO] Setting default archetype's groupId: com.company
- [INFO] Setting default archetype's artifactId: project
- [INFO] Setting default archetype's version: 1.0-SNAPSHOT
- [INFO] Setting default groupId: com.company
- [INFO] Setting default artifactId: project
- [INFO] Setting default version: 1.0-SNAPSHOT
- [INFO] Setting default package: com.company.project
- [INFO] Archetype created in target/generated-sources/archetype
- [INFO] ------------------------------------------------------------------------
- [INFO] BUILD SUCCESSFUL
- [INFO] ------------------------------------------------------------------------
- [INFO] Total time: 4 seconds
- [INFO] Finished at: Fri Feb 22 21:49:52 GMT 2008
- [INFO] Final Memory: 10M/26M
- [INFO] ------------------------------------------------------------------------
此時會在該項目的target目錄下發(fā)現(xiàn)如下文件:
---target
---generated-sources
---archetype
---our project
進入到archetype目錄下,也就是到達我們項目的根目錄執(zhí)行:
- $ cd target/generated-sources/archetype/
- $ mvn install
- [INFO] Scanning for projects...
- [INFO] ------------------------------------------------------------------------
- [INFO] Building project
- [INFO] task-segment: [install]
- [INFO] ------------------------------------------------------------------------
- [INFO] [resources:resources]
- [INFO] Using default encoding to copy filtered resources.
- [INFO] [resources:testResources]
- [INFO] Using default encoding to copy filtered resources.
- [INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
- [INFO] Setting property: velocimacro.messages.on => 'false'.
- [INFO] Setting property: resource.loader => 'classpath'.
- [INFO] Setting property: resource.manager.logwhenfound => 'false'.
- [INFO] [archetype:jar]
- [INFO] [archetype:add-archetype-metadata]
- [INFO] [archetype:integration-test]
- [INFO] [install:install]
- [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
- [INFO] [archetype:update-local-catalog]
- [INFO] ------------------------------------------------------------------------
- [INFO] BUILD SUCCESSFUL
- [INFO] ------------------------------------------------------------------------
- [INFO] Total time: 10 seconds
- [INFO] Finished at: Fri Feb 22 21:51:57 GMT 2008
- [INFO] Final Memory: 14M/35M
- [INFO] ------------------------------------------------------------------------
此時咱們的maven骨架就已經(jīng)搭建好了。
執(zhí)行mvn:archetype:crawl命令,第三個重要的命令:
會發(fā)現(xiàn)在咱們的本地倉庫的根目錄生成archetype-catalog.xml骨架配置文件。查看此文件內(nèi)容如下:
- <archetype>
- <groupId>com.lgy.archetype-demo</groupId>
- <artifactId>archetype-demo-archetype</artifactId>
- <version>1.0.0-RELEASE</version>
- <description>archetype-demo</description>
- </archetype>
引用該骨架文件,在eclipse中選擇該骨架,則可發(fā)現(xiàn)項目創(chuàng)建后就是咱們的骨架項目模板。
*當使用archetype:generate命令創(chuàng)建maven骨架項目的時候,發(fā)現(xiàn)有的時候骨架太多,咱們就可以使用filter過lv掉,參考maven該命令參數(shù):
例如:
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官網(wǎng)