全站最帅😎
发布于 2023-08-25 / 526 阅读
0
0

从Maven自定义模板快速创建项目

  • 1. 操作步骤
  • 2. 注意事项

1. 操作步骤

1.1 在模板项目下新建 archetype.properties 文件

excludePatterns=.idea/*,/**/.idea/*,/**/node_modules/**/*,readme.md,archetype.properties,*.bat,*.iml,/**/*.iml,*.ps1,pom-xml-flattened,/**/pom-xml-flattened

1.2 执行 mvn archetype:create-from-project -D archetype.properties=./archetype.properties
在项目根目录下生成 target 目录
image.png

1.3 cd 到 archetype 目录,执行如下命令

# 将模板安装到本地仓库
mvn -D maven.test.skip clean install
# 将模板上传到远程仓库
mvn deploy

1.4 从模板生成项目

参数说明
-DgroupId 需要生成项目的组id
-DartifactId 需要生成项目的名称
-Dpackage 生成项目的包名
-DarchetypeRepository 可选local或者远程仓库

  • 从本地仓库快速生成项目
mvn org.apache.maven.plugins:maven-archetype-plugin:3.2.1:generate \
-DgroupId=com.das \
-DartifactId=test \
-Dpackage=com.das.test \
-DarchetypeGroupId=com.das\
-DarchetypeArtifactId=demo-archetype \
-DarchetypeVersion=1.0.0-SNAPSHOT \
-DinteractiveMode=false \
-DarchetypeRepository=local -X
  • 从远程仓库快速生成项目
mvn org.apache.maven.plugins:maven-archetype-plugin:3.2.1:generate  \
-DgroupId=com.das \
-DartifactId=test \
-Dpackage=com.das.test \
-DarchetypeGroupId=com.das\
-DarchetypeArtifactId=demo-archetype \
-DarchetypeVersion=1.0.0-SNAPSHOT \
-DinteractiveMode=false \
-DarchetypeRepository=http://nexus3.rd.chn-das.com/repository/maven-snapshots -X

2. 注意事项

  • 以上所有命令都不要通过 powershell 执行
  • target/generated-sources/archetype/pom.xml 需要添加远程仓库信息
<distributionManagement>
<!--maven上传配置-->
<snapshotRepository>
  <id>nexus-snapshots</id>
  <name>Nexus SnapShots Repository</name>
  <url>${maven.url}/repository/maven-snapshots/</url>
</snapshotRepository>
<repository>
  <id>nexus-release</id>
  <name>Nexus Release Repository</name>
  <url>${maven.url}/repository/maven-releases/</url>
</repository>
</distributionManagement>
<properties>
 <maven.url>你的远程仓库地址</maven.url>
</properties>
  • 模版项目中pom.xml需要添加如下解决路径带 - 问题:
    <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-archetype-plugin</artifactId>
                    <version>3.2.1</version>
                    <configuration>
                        <archetypeFilteredExtensions>java,xml,txt,groovy,cs,mdo,aj,jsp,gsp,vm,html,xhtml,properties,launch</archetypeFilteredExtensions>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    

示例 archetype-metadata.xml

<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor xsi:schemaLocation="https://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.1.0 http://maven.apache.org/xsd/archetype-descriptor-1.1.0.xsd" name="templet"
    xmlns="https://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modules>
    <module id="${rootArtifactId}-api" dir="__rootArtifactId__-api" name="${rootArtifactId}-api">
      <fileSets>
        <fileSet filtered="true" packaged="true" encoding="UTF-8">
          <directory>src/main/java</directory>
          <includes>
            <include>**/*.java</include>
          </includes>
        </fileSet>
        <fileSet filtered="true" encoding="UTF-8">
          <directory>src/main/resources</directory>
          <includes>
            <include>**/*.xml</include>
            <include>**/*.html</include>
          </includes>
        </fileSet>
        <fileSet encoding="UTF-8">
          <directory>src/main/resources</directory>
          <includes>
            <include>**/*.css</include>
            <include>**/*.json</include>
            <include>**/*.js</include>
          </includes>
        </fileSet>
      </fileSets>
    </module>
    <module id="${rootArtifactId}-biz" dir="__rootArtifactId__-biz" name="${rootArtifactId}-biz">
      <fileSets>
        <fileSet filtered="true" packaged="true" encoding="UTF-8">
          <directory>src/main/java</directory>
          <includes>
            <include>**/*.java</include>
          </includes>
        </fileSet>
        <fileSet encoding="UTF-8">
          <directory>src/main/resources</directory>
          <includes>
            <include>**/*.yml</include>
          </includes>
        </fileSet>
        <fileSet filtered="true" encoding="UTF-8">
          <directory>__rootArtifactId__-client</directory>
          <includes>
            <include>**/*.xml</include>
          </includes>
        </fileSet>
        <fileSet filtered="true" packaged="true" encoding="UTF-8">
          <directory>__rootArtifactId__-client/src</directory>
          <includes>
            <include>**/*.java</include>
          </includes>
        </fileSet>
        <fileSet encoding="UTF-8">
          <directory>__rootArtifactId__-client/target</directory>
          <includes>
            <include>**/*.class</include>
          </includes>
        </fileSet>
      </fileSets>
    </module>
  </modules>
</archetype-descriptor>

评论