? ? ? ? 介绍:公司内部会制定自己的规范及包结构。当创建新项目的时候就需要选择骨架,即可生成包结构。本文章简单介绍如何创建,解决遇到的各种问题。
一、创建项目。
?idea点击file--->new--->project--->点击左侧的Spring initializr-
图2??
创建自己的各种包,及配置。包括pom.xml;yml;?
二、命令生成骨架
根目录执行mvn archetype:create-from-project
如图所示:
执行正常结果图
?
?
再进入target\generated-sources\archetype?这个目录执行:mvn clean install;就把骨架安装到仓库中了。
?
其他人用这个骨架,就可以把仓库文件复制过去。使用?
如果执行第一步失败;见问题处理。
三、选择骨架,创建新项目
?idea点击file--->new--->project--->点击左侧的Maven----->选中复选框create from archetype----->选择自己的骨架。填写包路径,项目名称就可以了
?
如果不展示自己的骨架,见问题处理
四、遇到的问题
错误1.执行mvn archetype:create-from-project命令失败;如图
?
错误日志
PS D:\pro819\democode\demo> 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:\pro819\democode\demo\target\generated-sources\archetype\target\demo-archetype-0.0.1-SNAPSHOT.jar
[INFO] Building jar: D:\pro819\democode\demo\target\generated-sources\archetype\target\demo-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:\pro819\democode\demo\target\generated-sources\archetype
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.979 s
[INFO] Finished at: 2021-12-06T14:28:54+08:00
[INFO] ------------------------------------------------------------------------
PS D:\pro819\democode\demo> 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:\Users\admin\.m2\settings.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:\Users\admin\.m2下;如图:
在运行就解决了。
?错误2. 骨架生成了,仓库也有但是自己创建的时候不显示
解决办法:在C:\Users\admin\AppData\Local\JetBrains\IntelliJIdea2021.2\Maven\Indices这个目录下面自己新建一个UserArchetypes.xml;填写内容:
<archetypes>
<archetype groupId="com.example" artifactId="demo-archetype" version="0.0.1-SNAPSHOT" />
</archetypes>
重启idea就可以看见了。
错误3:自己新建项目,出现wrapper
错误图:
?
这是因为选择spring Initializr?创建项目有多余的文件生成,需要删除。再重新生成骨架;删除图中文件
?
五、参考文章
Idea自定义Maven骨架(archetype)_Quick_的博客-CSDN博客选择骨架时的问题:? ? ? ? 在创建Maven的项目时我们发现了一个很不方便的问题,就是每次创建Maven的工程的时候,都需要选择一个骨架,但是每个骨架都需要删除一部分东西才行,在平时开发中,如果是分布式项目,有可能会频繁创建子项目,每次都要删除不要的文件然后拷贝我们要的文件,例如弹簧配置,web.xml中配置等。这样做不仅会浪费大量时间,拷贝和删除文件也容易导致错误的发生,比如漏拷某文件...https://blog.csdn.net/Quick_/article/details/81842936如何在 IDEA 中添加 Maven 项目的 Archetype(解决添加不起作用的问题)_小艾的未解之谜-CSDN博客前言在 IDEA 中点击新建 Maven 模块,会发现他已经为我们罗列出来了许多的 archetype,但有些时候满足不了我们创建脚手架并打包打开终端,cd到这个模块的根目录,比如这里是D:/Java_Study/idea_projects/springmvc-course/ssm-quickstart,然后运行mvn archetype:create-from-project,等待构建完成。接着cd到生成的target/generated-sources/archetype目...https://blog.csdn.net/qq_52827181/article/details/121479427
六、代码地址
myarchetype: 自定义骨架https://gitee.com/itwangfl/myarchetype.git?
|