自定义archetype骨架
自定义一个maven项目,用来作archetype骨架
在pom.xml文件中添加
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-archetype-plugin</artifactId>
<version>3.2.1</version>
</plugin>
</plugins>
<extensions>
<extension>
<groupId>org.apache.maven.archetype</groupId>
<artifactId>archetype-packaging</artifactId>
<version>3.0.1</version>
</extension>
</extensions>
创建archetype骨架
在xml.pom文件夹同目录下开启cmd或powershell,执行下面命令:
mvn clean archetype:create-from-project
构建成功后项目会出现如下结构的target目录:
target目录下会有generated-sources目录,generated-sources/archetype/src/main.resource/META_INF.maven下会有一个archetype-metadata.xml文件,这里是可以配置那些资源会被包含在骨架中,那些不会包含在骨架中。
安装archetype到本地
进入target/generated-sources/archetype目录下,执行指令:
cd target/generated-sources/archetype/
mvn clean install
运行成功后可以在info指示的目录下找到骨架的jar包。
在本地仓库生成骨架坐标信息
执行指令:
mvn archetype:crawl
maven本地仓库下就会生成一个archetype-metadata.xml文件,默认是自己maven的mvn_resp文件夹下
打开该配置文件,里面有固件的坐标信息。
<?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.NovaTest</groupId>
<artifactId>SpringBoot_damo01-archetype</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>SpringBoot_damo01</description>
</archetype>
<archetype>
<groupId>org.apache.cocoon</groupId>
<artifactId>cocoon-22-archetype-webapp</artifactId>
<version>1.0.0</version>
<description>cocoon-22-archetype-webapp</description>
</archetype>
<archetype>
<groupId>com.NovaLover</groupId>
<artifactId>maven_archetype-archetype</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>Parent pom providing dependency and plugin management for applications built with Maven</description>
</archetype>
</archetypes>
</archetype-catalog>
根据骨架创建项目
直接通过maven指令创建项目
打开需要创建项目的文件夹,在该文件夹下打开cmd或powershell,执行下面指令:
mvn archetype:generate -DarchetypeGroupId=<groupId> -DarchetypeArtifactId=<artifactId> -DarchetypeVersion=<version> -DgroupId=<groupId> -DartifactId=<artifactId> -X
其中groupId、artifactId、version填写创建的固件的坐标信息和版本号,后面的groupId、artifactId填自己需要创建的项目的坐标号。
通过Idea创建项目
创建一个maven项目。
输入骨架坐标,以及对应的仓库信息(如果是远程仓库的话需要指定,本地仓库可以不指定):
选择下一步,再输入创建项目的groupId和artifactId之后项目就创建成功了。
创建的项目会有骨架项目的java代码、依赖配置、以及资源文件。
Maven 默认情况下不会复制空文件夹。如果这些空文件夹中没有任何文件,那么 Maven 在复制资源文件时会将它们忽略掉。您可以手动向这些空文件夹中添加一个空的 .gitkeep 文件(或者其他文件),这样 Maven 就会复制这些文件夹。
如果生成的项目中没有resources下的文件,可能是在使用 Maven Archetype 生成项目时,您可能没有选择包括资源文件夹。您可以在运行 Maven Archetype 生成项目命令时使用 -DincludeSrc=false
参数来排除资源文件夹。如果您不想排除资源文件夹,可以在运行命令时省略该参数或将其设置为 true。
上传骨架到nexus
在项目target/generated-sources/archetype/pom.xml 中加入以下配置,指定nexus地址。
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Micaicms Releases</name>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Micaicms Releases</name>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
然后在target/generated-sources/archetype运行:
mvn deploy
这样就能把骨架上传到nexus仓库中了
注意:上传nexus私服需要密码。
使用远程nexus仓库的archetype创建项目
使用远程nexus仓库的archetype创建项目的时候,必须在自己的maven conf 下settings.xml加入以下配置:
因为Maven 3改变了原型存储库的集成方式。该-DarchetypeRepository
参数不再存在。相反,需要将archteype存储库添加到settings.xml
<profile>
<!-- the profile id is arbitrary 这个id是任意填的-->
<id>my_archetype</id>
<repositories>
<repository>
<!-- the repository id has to be named: archetype 这repository Id必须是archetype -->
<id>archetype</id>
<name>my archetypes</name>
<url>http://127.0.0.1:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</repository>
</repositories>
</profile>
<activeProfiles>
<activeProfile>my_archetype</activeProfile> <!-- 这个id是上面的profile id -->
</activeProfiles>
注意:上面的配置是为了告诉maven archetype可以从哪里拿,如果没有上面的配置,使用远程nexus 的archetype的时候会报The desired archetype does not exist。