idea自定义archetype及错误处理

介绍:公司内部会制定自己的规范及包结构。当创建新项目的时候就需要选择骨架,即可生成包结构。本文章简单介绍如何创建,解决遇到的各种问题。

一、创建项目。

idea点击file--->new--->project--->点击左侧的Spring initializr-

图2

创建自己的各种包,及配置。包括pom.xml;yml;

二、命令生成骨架

根目录执行mvn archetype:create-from-project

如图所示:

执行正常结果图

再进入targetgenerated-sourcesarchetype 这个目录执行:mvn clean install;就把骨架安装到仓库中了。

其他人用这个骨架,就可以把仓库文件复制过去。使用

如果执行第一步失败;见问题处理。

三、选择骨架,创建新项目

idea点击file--->new--->project--->点击左侧的Maven----->选中复选框create from archetype----->选择自己的骨架。填写包路径,项目名称就可以了

如果不展示自己的骨架,见问题处理

四、遇到的问题

错误1.执行mvn archetype:create-from-project命令失败;如图

错误日志

PS D:pro819democodedemo> mvn archetype:create-from-project
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.example:demo >--------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.2.0:create-from-project (default-cli) > generate-sources @ demo >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.2.0:create-from-project (default-cli) < generate-sources @ demo <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.2.0:create-from-project (default-cli) @ demo ---
[INFO] Setting default groupId: com.example
[INFO] Setting default artifactId: demo
[INFO] Setting default version: 0.0.1-SNAPSHOT
[INFO] Setting default package: com.example.demo
[WARN] Maven will be executed in interactive mode, but no input stream has been configured for this MavenInvoker instance.
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< com.example:demo-archetype >---------------------
[INFO] Building demo-archetype 0.0.1-SNAPSHOT
[INFO] --------------------------[ maven-archetype ]---------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ demo-archetype ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Using null encoding to copy filtered properties files.
[INFO] Copying 104 resources
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ demo-archetype ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Using null encoding to copy filtered properties files.
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-archetype-plugin:3.2.0:jar (default-jar) @ demo-archetype ---
[INFO] Building archetype jar: D:pro819democodedemo	argetgenerated-sourcesarchetype	argetdemo-archetype-0.0.1-SNAPSHOT.jar
[INFO] Building jar: D:pro819democodedemo	argetgenerated-sourcesarchetype	argetdemo-archetype-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.085 s
[INFO] Finished at: 2021-12-06T14:28:54+08:00
[INFO] ------------------------------------------------------------------------
[INFO] Archetype project created in D:pro819democodedemo	argetgenerated-sourcesarchetype
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.979 s
[INFO] Finished at: 2021-12-06T14:28:54+08:00
[INFO] ------------------------------------------------------------------------
PS D:pro819democodedemo> mvn archetype:create-from-project
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.example:demo >--------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.2.0:create-from-project (default-cli) > generate-sources @ demo >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.2.0:create-from-project (default-cli) < generate-sources @ demo <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.2.0:create-from-project (default-cli) @ demo ---
[INFO] Setting default groupId: com.example
[INFO] Setting default artifactId: demo
[INFO] Setting default version: 0.0.1-SNAPSHOT
[INFO] Setting default package: com.example
[WARN] Maven will be executed in interactive mode, but no input stream has been configured for this MavenInvoker instance.
[ERROR] Error executing Maven.
[ERROR] The specified user settings file does not exist: C:Usersadmin.m2settings.xml
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.640 s
[INFO] Finished at: 2021-12-06T14:37:32+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:3.2.0:create-from-project (default-cli) on project demo: Invoker process ended with result different than 0! -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

这个是说找不到setting.xml,我的maven配置已经是我自己的了,不知道为什么这里又走别处了,没有找到解决办法,只能复制一份setting.xml放在这个目录C:Usersadmin.m2下;如图:

在运行就解决了。

错误2. 骨架生成了,仓库也有但是自己创建的时候不显示

解决办法:在C:UsersadminAppDataLocalJetBrainsIntelliJIdea2021.2MavenIndices这个目录下面自己新建一个UserArchetypes.xml;填写内容:

<archetypes>
    <archetype groupId="com.example" artifactId="demo-archetype" version="0.0.1-SNAPSHOT" />
</archetypes>

重启idea就可以看见了。

错误3:自己新建项目,出现wrapper

错误图:

这是因为选择spring Initializr 创建项目有多余的文件生成,需要删除。再重新生成骨架;删除图中文件

五、参考文章

六、代码地址

经验分享 程序员 微信小程序 职场和发展