建立Maven模板專案

壹頁書發表於2016-01-10
假如已經存在了一個專案,
http://blog.itpub.net/29254281/viewspace-1974603/

這個專案已經搭建好了springmvc,spring和MyBatis的環境
把這個專案轉為Maven模板,以後新建專案可以直接從模板生成,省去專案搭建的步驟.


檢視幫助資訊
E:\workspace\songod>mvn archetype:help
archetype:create-from-project
  Creates an archetype project from the current project.

  This goal reads your source and resource files, the values of its parameters,
  and properties you specify in a .property file, and uses them to create a
  Maven archetype project using the maven-archetype packaging. If you build the
  resulting project, it will create the archetype. You can then use this
  archetype to create new projects that resemble the original.

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

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

  GAV
    The GAV values for the current project are replaced by properties: groupId,
    artifactId, and version. The user chooses new values for these when
    generating a project from the archetype.
  package
    All the files under one specified Java (or cognate) package are relocated to
    a project that the user chooses when generating a project. References to the
    class name are replaced by a property reference. For example, if the current
    project's sources are in the package org.apache.saltedpeanuts, then any
    example of the string org.apache.saltedpeanuts is replaced with the Velocity
    property reference ${packageName}. When the user generates a project, this
    is in turn replaced by his or her choice of a package.
  custom properties
    You may identify additional strings that should be replaced by parameters.
    To add custom properties, you must use the propertyFile parameter to specify
    a property file. See the documentation for propertyFile for the details.

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

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


在專案的pom檔案中,增加外掛.


然後在專案路徑下執行
E:\workspace\songod>mvn archetype:create-from-project

Maven會根據這個專案生成一個模板專案.(target\generated-sources\archetype)

進入模板專案,打包、安裝模板專案.
E:\workspace\songod\target\generated-sources\archetype>mvn clean install

在控制檯可以看到如下日誌
[INFO] Installing E:\workspace\songod\target\generated-sources\archetype\target\songod-archetype-0.0.1-SNAPSHOT.jar to C:\Users\new\.m2\repository\com\vv\songod-archetype\0.0.1-SNAPSHOT\songod-archetype-0.0.1-SNAPSHOT.jar
[INFO] Installing E:\workspace\songod\target\generated-sources\archetype\pom.xml to C:\Users\new\.m2\repository\com\vv\songod-archetype\0.0.1-SNAPSHOT\songod-archetype-0.0.1-SNAPSHOT.pom

可以看到這個模板專案作為
songod-archetype-0.0.1-SNAPSHOT.jar已經安裝到了本地倉庫.
如果公司內部使用,可以將其釋出到Nexus私服

使用剛才的模板建立新專案.
因為模板是SNAPSHOT的,所以需要勾選 Include snapshot archetypes 選項.
找到剛才製作的模板,建立專案.


新專案建立之後,spring、springmvc和MyBatis都配置好了.

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-1974624/,如需轉載,請註明出處,否則將追究法律責任。

相關文章