maven-archetype-plugin 的正确打开方式

1.  准备好一个编辑好的模板工程

2. 在 pom.xml 中添加 maven-archetype-plugin 插件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-archetype-plugin</artifactId>
    <version>2.4</version>
</plugin>

3. 执行: mvn archetype:create-from-project,生成对应的 archetype 工程
(IDEA 可以直接在 Maven Projects 面板上点击)

 

4. 导入刚刚生成的 archetype 工程
在 targetgenerated-sourcesarchetype 目录下有个 pom.xml,IDEA 可以直接拖放到 Maven Projects 面板上,就可以导入了

5. 编辑 targetgenerated-sourcesarchetypesrcmain esourcesMETA-INFmavenarchetype-metadata.xml,设置模板工程需要使用到的文件,删除不必要配置(比如:pom 中 maven-archetype-plugin 的配置)。

我这里设置成如下:
(为了避免每次生成 archetype 工程都重新编辑 archetype-metadata.xml,可以将修改好的 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="template-sf-boot"
                      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" packaged="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/main/assembly</directory>
            <includes>
                <include>**/**</include>
            </includes>
        </fileSet>
    </fileSets>
</archetype-descriptor>
View Code

filtered="true" 表示这些文件需要替换里面的占位符: ${}

6. mvn install 这个 archetype(注意:是 archetype 工程)

 

7. 通过 archetype 生成工程
有三种方式:
a. maven 命令
mvn archetype:generate -DarchetypeRepository=local -DarchetypeGroupId=com.xx.xx -DarchetypeArtifactId=template-xxx-archetype -DarchetypeVersion=1.0-SNAPSHOT -DgroupId=com.kvn.demo -DartifactId=demo-test
(关闭对话模式,添加参数:-DinteractiveMode=false )

b. IDEA 或者 eclipse

参考: https://www.cnblogs.com/fengpingfan/p/5179735.html
https://github.com/alonWang/maven-custom-archetype

原文地址:https://www.cnblogs.com/kevin-yuan/p/12059857.html