Boss said the structure of the new project is just as good as XXX project, I (quickly create projects using Maven archetype)

Liu Zhihang 2020-11-06 01:16:07
boss said structure new project


Preface

It's time to develop a new project , Or create a new project , What do I do ? The boss said according to xxx Project structure create a new project .

official account :liuzhihangs, Record the skills in work study 、 Development and source notes ; From time to time to share some of the life experience . You are welcome to guide !

There are often new projects to be created at work , There are three common ways

CC Dafa New projects , Then find the previous various tool classes , Copy and paste in , You may not be able to run at this time , And then do all kinds of debugging .

archetype-cc-45VBU1

CD Dafa Copy the old project , Then change module name , Rely on names , Delete old code , Of course, it doesn't have to be able to run , At this point, all kinds of debugging will be carried out .

archetype-cc1-SPx1ct

Of course , It's certainly not the use of these two methods , Let's introduce a more concise way , Use maven archetype Generate project template , One click project creation .

Action!!!

What is? Archetype ?

In short ,Archetype yes Maven Project template toolbox .

An archetype is defined as an original pattern or model from which all other things of the same kind are made.

Prototypes are defined as original styles or models , You can make all the other projects of the same kind .

Official explanation , Simple and clear , It's using existing projects , Generate a template . In the future, you can use this template to quickly generate projects with the same structure . Useful in team development .

It's the official explanation , Put up the address :http://maven.apache.org/arche...

Use IDEA As a demonstration tool , Step by step introduction .

8e83e6d78b16698fb19762806acadd1c-ZXYM5I

Prepare template project

Here is still a template project , Such as this :

archetype1-lZkrqX

This is a lot of module project , A simple demo

  1. Used nacos As a registry ;
  2. fegin As a calling tool ;
  3. There is a universal check token Tool class ;
  4. Let's assume that there are also some public configurations of companies (MQ, Link monitoring , Unified log, etc ).

Of course, this project can run . The newly created project is also the template .

Get into the subject

Use command

archetype:generate

This is based on the current project , You can also use the official website to specify parameters separately

  1. mvn archetype:generate Then, step by step, follow the prompts and enter ;
  2. One time input , among DarchetypeGroupId 、DarchetypeArtifactId 、DarchetypeVersion For the generated Archetype Organizational version of the project .
mvn archetype:generate \
-DarchetypeGroupId=<archetype-groupId> \
-DarchetypeArtifactId=<archetype-artifactId> \
-DarchetypeVersion=<archetype-version> \
-DgroupId=<my.groupid> \
-DartifactId=<my-artifactId>

Advanced usage mvn clean archetype:create-from-project

The execution diagram is as follows :

archetype-iterm-GONbvt

Go back to IDEA Check out the project , stay target/generated-sources In the catalog archetype The generated project template .

archetype-afer-term-vP3Tz3

The structure is as shown in the figure :

  1. main/resources/archetype-resources: Project template , Generate a new project , It is based on this code to generate .

    1. .idea It's useless , Delete the .
    2. __rootArtifactId__xxx Each part of the project module
  2. main/resources/META-INF/maven/archetype-metadata.xml: Template project metadata configuration .

You can put archetype Torture out , This is a separate engineering template , After the torture , Use IDEA open .

So let's start with archetype What's in it .

archetype Template project introduction

Use IDEA Open and find , Or a Maven project .

archetype-resources

archetype-structure-xUdYE2

open pom file , You can see inside ${groupId}${artifactId}${version} Use the placeholder to specify the organization version , These are what you specify when you create a new project .

archetype-metadata.xml

archetype-metadata.xml It is configured for metadata .

  • fileSet: Used to generate files in some projects . If the file or directory name contains __property__ Pattern , Replace it with the corresponding attribute value .
attribute type describe
filtered boolean Filter the corpus , Copy the specified file directly without modification . The default value is :false.
packaged boolean Package files , The specified file will be in package Attribute is generated in the directory structure before the / Copy . They can be non packaged , This means that the selected file will be generated without the prefix / Copy . The default value is :false.
encoding String The encoding used when filtering content .

fileSet Contains the following elements :

Elements type describe
directory String The directory where the project files are generated
includes/include* List<String> Include files
excludes/exclude* List<String> Exclude files

Because you don't need to build a project .idea *.iml file , So delete :

archetype-metadata1-iVIvLq

  • module How many projects are to be generated module
attribute type describe
id String The module's artifactId.
dir String The module's directory.
name String The module's name.
Elements type describe
fileSets/fileSet* List<FileSet> file
modules/module* List<ModuleDescriptor> modular

archetype-metadata2-HCnJqd

You can see that it is your own project template .

__rootArtifactId__-controller At the time of generation , According to the incoming artifactId Generate the specified module name .

Use

  1. clean install

archetype-clean-install-zbua0z

  1. IDEA Add Archetype

archetype-maven-b3sZ6a

  1. Choose to use Archetype Generate a new project

archetype-maven2-HDnFc4

  1. Fill in the name of the newly generated project, etc

archetype-maven3-sbJlI1

  1. Generate a new project

archetype-generate-5GUrpu

Expand

Q: How to customize the package path ?

A: have access to requiredProperties Custom parameters . By passing in custom parameters , To generate custom package paths .

For example, it is found that the package paths of newly generated projects are com.liuzhihang.archetype, That's not gonna work , Each project has its own package path . Just make the following changes :

  1. take requiredProperties Add to the project , Then add a new variable middlePackage.
<requiredProperties>
<!-- Use archetype The input parameters must be required when -->
<requiredProperty key="groupId">
<!-- Default values can be set , Use archetype Default values will be used -->
<defaultValue>com.liuzhihang</defaultValue>
</requiredProperty>
<requiredProperty key="package">
<defaultValue>com.liuzhihang</defaultValue>
</requiredProperty>
<requiredProperty key="middlePackage">
<defaultValue>${rootArtifactId}</defaultValue>
</requiredProperty>
</requiredProperties>
  1. Modify the file name of the template

archetype-middle-mH0Gzf

If the file or directory name contains __property__ Pattern , Replace it with the corresponding attribute value . Not yet here , Because the generated package name has not been changed .

  1. Modify the package path of internal files . Include .java .xml .properties etc. .

archetype-change-middle-3C0Msb

  1. again clean install

notes : You may report an error at this time , Need to be in src/test/resources/projects/basic/archetype.properties Add below middlePackage=basic Try again .

Pay attention to the specification of middlePackage attribute .

archetype-middle-1-r8qOco

Q: I want to customize Application How to get your name ?

A: Also use requiredProperties Custom parameters .

<requiredProperty key="appName">
</requiredProperty>

archetype-app-name-Vt9Xj3

Of course, you can have a generic name .

Q: How to use the other partners ?

A: Certainly deploy It's private service , stay pom Add the following configuration , Specify your own company's private service .deploy , In this way, you can use it happily with your partner .

<!-- Remote warehouse -->
<distributionManagement>
<repository>
<id>releases</id>
<name>Nexus Release Repository</name>
<url>http://liuzhihang.com:xxxx/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://liuzhihang.com:xxxx/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>

Q: How can I get from IDEA Delete Archetype ​?

A: So easy to use, how willing to delete ​ Well ? Just find the following path

liuzhihang % > pwd
/Users/liuzhihang/Library/Caches/JetBrains/IntelliJIdea2020.1/Maven/Indices

There's a UserArchetypes.xml​, open , Delete the inside archetype Just go .

archetype-delete-ZyQULu

Related information

Code :https://github.com/liuzhihang...

版权声明
本文为[Liu Zhihang]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云