maven自定义archetype
在开发过程中我们经常会创建一系列结构类似的新项目,这些项目结构和基础配置基本或完全一致,maven就提供了archetype类型来规定新建项目的结构及基础配置,利用archetype就可以快速简单的搭建新项目。
一、创建Maven项目的一般步骤
一般情况下创建一个maven项目会遵循以下步骤:
1.选择项目类型,类型为maven项目
2.选择是否跳过使用archetype模板类型创建,这里选择否
3.进入archetype选择界面,这里会列出所有可以使用的archetype类型,选择其中一种,其中maven-archetype-quickstart和maven-archetype-webapp就是我们经常用到的。
在这里列出了所有本机默认或已配置的archetype类型,也可以点击右侧的Configure...按钮可以进入archetype配置界面,或者直接从Preferences也可进入,界面如下:
4.完成创建
二、自己定义archetype
一般情况下我们使用内置的archetype类型就基本够用了,但是每一个公司或团体都有自己的规范与架构,这时就需要一套符合本地需求的archetype类型配置,所以我们就需要自定义一套archetype。
创建archetype必须已存在项目为基础,这个项目可以是专门新创建的模板项目,也可以是已发布的存在项目,我们这里已一个新创建的模板项目为例。
1.首先创建模板项目,将以此项目作为模板创建archetype
2.创建完成后的项目结构如下所示:
项目创建好之后把你要想作为模板的内容都添加和配置好,无论是java、xml、properties、html等等均可。
3.开始创建archetype配置文件,在父项目(archetype-parent)上右键-->Run As-->Maven build
填入构建参数:archetype:create-from-project,运行即可。
构建完成后会显示如下类似信息:
......省略 [INFO] Copying 2 resources [INFO] [INFO] --- maven-archetype-plugin:2.4:jar (default-jar) @ archetype-parent-archetype --- [INFO] Building archetype jar: D:\workspace_2\archetype-parent\target\generated-sources\archetype\target\archetype-parent-archetype-1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.519 s [INFO] Finished at: 2018-06-22T18:17:01+08:00 [INFO] ------------------------------------------------------------------------ [INFO] Archetype project created in D:\workspace_2\archetype-parent\target\generated-sources\archetype [INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary: [INFO] [INFO] archetype-parent 1.0-SNAPSHOT ...................... SUCCESS [ 10.438 s] [INFO] archetype-web ...................................... SKIPPED [INFO] archetype-biz 1.0-SNAPSHOT ......................... SKIPPED [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 11.304 s [INFO] Finished at: 2018-06-22T18:17:01+08:00 [INFO] ------------------------------------------------------------------------
三 生成archetype
1.构建成功后会在target目录下生成相关配置文件,如下所示:
2.其中archetype的pom.xml与archetype-metadata.xml这两个文件是需要我们根据实际情况进行修改的。
pom.xml文件原始内容:
<?xml version="1.0" encoding="UTF-8"?> <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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.archetype.example</groupId> <artifactId>archetype-parent-archetype</artifactId> <version>1.0-SNAPSHOT</version> <packaging>maven-archetype</packaging> <name>archetype-parent-archetype</name> <build> <extensions> <extension> <groupId>org.apache.maven.archetype</groupId> <artifactId>archetype-packaging</artifactId> <version>2.4</version> </extension> </extensions> <pluginManagement> <plugins> <plugin> <artifactId>maven-archetype-plugin</artifactId> <version>2.4</version> </plugin> </plugins> </pluginManagement> </build> </project>
pom文件中groupId、artifactId、version与name这4个属性需要我们进行修改,其中因为我们的项目版本声明的是1.0-SNAPSHOT,所以这里也继承为1.0-SNAPSHOT,需要修改为1.0或其他,否则在生成archetype之后不能直接显示,需要勾选相关选项才行。其他3个属性根据自己的需求修改为合理的即可。
修改完成后如下:
<?xml version="1.0" encoding="UTF-8"?> <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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.archetype.example</groupId> <artifactId>example-archetype-web</artifactId> <version>1.0</version> <packaging>maven-archetype</packaging> <name>example-archetype-web</name> ...省略 </project>
archetype-metadata.xml文件原始内容:
<?xml version="1.0" encoding="UTF-8"?> <archetype-descriptor xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0 http://maven.apache.org/xsd/archetype-descriptor-1.0.0.xsd" name="archetype-parent" xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <fileSets> <fileSet encoding="UTF-8"> <directory>.settings</directory> <includes> <include>**/*.prefs</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8"> <directory></directory> <includes> <include>.project</include> </includes> </fileSet> </fileSets> <modules> <module id="archetype-web" dir="archetype-web" name="archetype-web"> <fileSets> <fileSet filtered="true" encoding="UTF-8"> <directory>src/main/java</directory> <includes> <include>**/*.java</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8"> <directory>src/main/webapp</directory> <includes> <include>**/*.xml</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8"> <directory>.settings</directory> <includes> <include>**/*.xml</include> </includes> </fileSet> <fileSet encoding="UTF-8"> <directory>.settings</directory> <includes> <include>**/*.container</include> <include>**/*.component</include> <include>**/*.name</include> <include>**/*.jsdtscope</include> <include>**/*.prefs</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8"> <directory></directory> <includes> <include>.classpath</include> <include>.project</include> </includes> </fileSet> </fileSets> </module> <module id="archetype-biz" dir="archetype-biz" name="archetype-biz"> <fileSets> <fileSet encoding="UTF-8"> <directory>.settings</directory> <includes> <include>**/*.prefs</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8"> <directory></directory> <includes> <include>.classpath</include> <include>.project</include> </includes> </fileSet> </fileSets> </module> </modules> </archetype-descriptor>
而archetype-metadata.xml文件需要修改的内容就较多了,其中<fileSet>标签的作用就是过滤和包含哪些文件的作用,这里我们直接修改为“全部”即可,如下:
<fileSets> <fileSet filtered="true" encoding="UTF-8"> <directory>src/main/java</directory> <includes> <include>**/*.**</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8"> <directory>src/main/resources</directory> <includes> <include>**/**</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8"> <directory>src/test/java</directory> <includes> <include>**/*.**</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8"> <directory>src/test/resources</directory> <includes> <include>**/*.**</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8"> <directory></directory> <includes> <include>.**</include> </includes> </fileSet> </fileSets>
修改完成后的archetype-metadata.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?> <archetype-descriptor xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0 http://maven.apache.org/xsd/archetype-descriptor-1.0.0.xsd" name="archetype-parent" xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <fileSets> <fileSet filtered="true" encoding="UTF-8"> <directory>src/main/java</directory> <includes> <include>**/*.**</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8"> <directory>src/main/resources</directory> <includes> <include>**/**</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8"> <directory>src/test/java</directory> <includes> <include>**/*.**</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8"> <directory>src/test/resources</directory> <includes> <include>**/*.**</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8"> <directory></directory> <includes> <include>.**</include> </includes> </fileSet> </fileSets> <modules> <module id="archetype-web" dir="archetype-web" name="archetype-web"> <fileSets> <fileSet filtered="true" encoding="UTF-8"> <directory>src/main/java</directory> <includes> <include>**/*.**</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8"> <directory>src/main/resources</directory> <includes> <include>**/**</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8"> <directory>src/test/java</directory> <includes> <include>**/*.**</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8"> <directory>src/test/resources</directory> <includes> <include>**/*.**</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8"> <directory></directory> <includes> <include>.**</include> </includes> </fileSet> </fileSets> </module> <module id="archetype-biz" dir="archetype-biz" name="archetype-biz"> <fileSets> <fileSet filtered="true" encoding="UTF-8"> <directory>src/main/java</directory> <includes> <include>**/*.**</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8"> <directory>src/main/resources</directory> <includes> <include>**/**</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8"> <directory>src/test/java</directory> <includes> <include>**/*.**</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8"> <directory>src/test/resources</directory> <includes> <include>**/*.**</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8"> <directory></directory> <includes> <include>.**</include> </includes> </fileSet> </fileSets> </module> </modules> </archetype-descriptor>
3.安装archetype
pom与archetype-metadata.xml两个文件修改完成后,进入到target\generated-sources\archetype目录下执行构建命令:
mvn install
构建完成后显示类似信息:
...省略 [INFO] Installing D:\workspace_2\archetype-parent\target\generated-sources\archetype\pom.xml to C:\Users\Administrator\. m2\repository\com\archetype\example\example-archetype-web\1.0\example-archetype-web-1.0.pom [INFO] [INFO] --- maven-archetype-plugin:2.4:update-local-catalog (default-update-local-catalog) @ example-archetype-web --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 31.300 s [INFO] Finished at: 2018-06-25T11:44:43+08:00 [INFO] Final Memory: 16M/114M [INFO] ------------------------------------------------------------------------
构建成功后我们的仓库目录下的archetype-catalog.xml文件会将此archetype添加,出现以下内容说明添加成功。内容如下:
<?xml version="1.0" encoding="UTF-8"?> <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" xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <archetypes> <archetype> <groupId>com.archetype.example</groupId> <artifactId>example-archetype-web</artifactId> <version>1.0</version> <description>example-archetype-web</description> </archetype> </archetypes> </archetype-catalog>
archetype-catalog.xml所在位置为:maven仓库位置\archetype-catalog.xml(我这里为:C:\Users\Administrator\.m2\archetype-catalog.xml)。
构建成功之后我们就可以在创建Maven项目的时候选择这个模板来创建新项目了,如下所示:
三、上传archetype至nexus私服
有些公司或团体会提供自己的私服,如果将自定义的archetype上传至私服将给使用者带来极大的便利。
1.打开nexus服务器主页
进入主页后,点击右上角“Log In”按钮进行登录:
2.上传
选择合适的Repositories,我这里选择了3rd party,然后点击上传:
然后选择pom类型上传,选择本地仓库中的pom文件,如下:
最后选择jar文件,并上传:
上传成功后会在私服的根目录下的archetype-catalog.xml中添加该archetype,内容类似于我们本地仓库。
通过链接或者download按钮可以获取该文件的内容及地址,我这里为:http://nexus.example.com/nexus/service/local/repositories/thirdparty/content/archetype-catalog.xml
3.Eclipse中添加archetype类型
打开Eclipse的Maven配置页,点击Add Remote Catalog按钮,添加远程archetype配置,如下所示:
验证并添加完成后会在archetypes列表中显示这个远程配置信息,如下:
至此所有配置完成可以使用了。
上一篇: MySQL SQL语句