使用Maven Archetype插件制作项目脚手架(一)

Archetype是一个Maven项目模板工具包。通过Archetype我们可以快速搭建Maven项目。比如我们在ide里面创建项目时,可以选择很多maven内置的Archetype,我们最常用的可能是maven-archetype-quickstart

a58dc2a0aa4d348644d7f40f928e0f1a.png

当然maven提供了能力,让我们自定义项目结构,比如下图如果我们想要基于Archetype来自动生成下面多模块结构的脚手架,我们可以先手动建立下面的代码结构

c7b966a73047a1aca98e23b92af84a07.png

然后把下面插件加入到上面项目主pom中:

<build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-archetype-plugin</artifactId>
                    <version>3.0.1</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.6.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                    <configuration>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

然后在该项目的根目录下执行如下命令,来生成Archetype的模板:

mvn archetype:create-from-project

执行完毕后,在\target\generated-sources\archetype目录下会生成内容如下,该内容就是Archetype模板:

f74f141ae27c0a1c25cf74008ca8b131.png

在该目录下执行mvn install把模板安装到本地仓库,然后就可以执行下面命令来生成自己应用的项目结构了:

mvn archetype:generate -DarchetypeGroupId=com.jiaduo -DarchetypeArtifactId=project-template-archetype -DarchetypeVersion=1.0-SNAPSHOT -DarchetypeCatalog=local -DgroupId=com.jiaduo -DartifactId=order -Dversion=1.0-SNAPSHOT -DinteractiveMode=false

生成后的项目结构如下:

eb6df2670716e5d5211fcada716ca80d.png

当然如果你想要使用代码的调用方式,可参考如下:

public class Main {


    public static void main(String[] args) {


        String DgroupId ="com.jiaduo";
        String DartifactId ="Order";
        String Dversion ="1.0-SNAPSHOT";
        String targetDir ="/user/workspace";


        MavenCli cli = new MavenCli();
        System.setProperty("maven.multiModuleProjectDirectory","/user");
        int r = cli.doMain(new String[]{"archetype:generate",
                "-DgroupId="+DgroupId,
                "-DartifactId="+DartifactId,
                "-Dversion="+Dversion,


                "-DarchetypeGroupId=com.template",
                "-DarchetypeArtifactId=project-template-archetype",
                "-DarchetypeVersion=1.0-SNAPSHOT",
                "-DinteractiveMode=false"}, targetDir, System.out, System.out);


        System.out.println(r);
        if (r !=0){
            throw new RuntimeException("生成工程结构失败");
        }else{
            System.out.println("生成工程结构成功");
        }
    }


}

猜你喜欢

转载自blog.csdn.net/m0_37055174/article/details/135190147
今日推荐