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 工程
在 target\generated-sources\archetype 目錄下有個 pom.xml,IDEA 可以直接拖放到 Maven Projects 面板上,就可以導入了

 

5. 編輯 target\generated-sources\archetype\src\main\resources\META-INF\maven\archetype-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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM