" />

欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中如何快速構(gòu)建項(xiàng)目腳手架的實(shí)現(xiàn)

 更新時(shí)間:2022年05月31日 09:45:01   作者:緈諨の約錠  
這篇文章主要介紹了Java中如何快速構(gòu)建項(xiàng)目腳手架,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

1 前言

項(xiàng)目中,目前主流的當(dāng)然是微服務(wù)項(xiàng)目。為了應(yīng)對(duì)高并發(fā),以及保證自己的服務(wù)比較穩(wěn)定,通常會(huì)把服務(wù)按照模塊,或者具體的業(yè)務(wù)劃分為多個(gè)獨(dú)立的微服務(wù),那么如果一個(gè)一個(gè)去創(chuàng)建每一個(gè)微服務(wù)項(xiàng)目,感覺在做重復(fù)的事情,而且容易出錯(cuò),所以筆者就自己搞了一個(gè)通用的腳手架,在此基礎(chǔ)上修修補(bǔ)補(bǔ),大大提高了工作項(xiàng)目。

2 微服務(wù)項(xiàng)目準(zhǔn)備

在進(jìn)行腳手架構(gòu)建之前,肯定需要有一套自己的或者公司統(tǒng)一的微服務(wù)項(xiàng)目規(guī)范,比如說依賴管理、項(xiàng)目的包結(jié)構(gòu)等等,然后基于這些規(guī)范寫一個(gè)空的微服務(wù)項(xiàng)目出來,基于這個(gè)來生成項(xiàng)目腳手架模板。每個(gè)公司的項(xiàng)目名稱、包名稱,都有自己公司的標(biāo)準(zhǔn),所以結(jié)構(gòu)都不相同,這里項(xiàng)目源碼就忽略了,可以給大家分享一下目前大致的目錄結(jié)構(gòu),大致如下:

在這里插入圖片描述

每個(gè)模塊大致功能介紹:

模塊名描述
smilehappiness-api用來存放自己服務(wù)內(nèi)的feign服務(wù),可以用來給另一個(gè)項(xiàng)目強(qiáng)引用
smilehappiness-api-entity該模塊,是為了兼容強(qiáng)依賴,而做的妥協(xié),不建議使用強(qiáng)依賴
smilehappiness-common用來做一些通用的配置、攔截或者過濾器等等
smilehappiness-dao持久層處理的代碼,可以放到這個(gè)目錄下
smilehappiness-integrate用來存放外部項(xiàng)目的feign調(diào)用client,也可以做一些核心的插件集成
smilehappiness-job該模塊用來實(shí)現(xiàn)xxl-job或者其他任務(wù)調(diào)度功能
smilehappiness-model用來存放核心的model類,一般是與數(shù)據(jù)庫對(duì)應(yīng)的實(shí)體類
smilehappiness-msg用來處理消息功能,可以使短信、郵件或者是mq消息
smilehappiness-service核心業(yè)務(wù)處理
smilehappiness-test測試用例模塊
smilehappiness-web對(duì)外提供action層的restful接口,可以讓外部直接通過http方式訪問
smilehappiness-start項(xiàng)目啟動(dòng)模塊

3 腳手架構(gòu)建

在第二步創(chuàng)建一個(gè)spring-boot的maven項(xiàng)目后,就可以開始構(gòu)建腳手架了

3.1 項(xiàng)目正常啟動(dòng) && 測試用例正常

spring-boot的maven項(xiàng)目創(chuàng)建好之后,要保證項(xiàng)目能夠正常運(yùn)行起來,并且能夠通過http訪問,以及正常進(jìn)行單元測試,確保項(xiàng)目基本沒有問題后,將項(xiàng)目中的.class、.setting.project無關(guān)的文件刪除

3.2 在項(xiàng)目的根pom中加入以下maven插件配置

  <!-- 插件 -->
  <build>
       <finalName>${project.artifactId}</finalName>
       <plugins>
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-archetype-plugin</artifactId>
               <version>3.1.2</version>
           </plugin>
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-compiler-plugin</artifactId>
               <version>3.8.1</version>
           </plugin>
       </plugins>
   </build>

3.3 執(zhí)行archetype插件

在idea中,打開自己創(chuàng)建的微服務(wù)模板項(xiàng)目,右側(cè)maven中,plugins展開,然后雙擊執(zhí)行archetype:create-from-project命令,如下圖所示:

在這里插入圖片描述

在控制臺(tái)中顯示BUILD SUCCESS說明構(gòu)建成功,同時(shí)需要把文件存放的路徑記錄下來,筆者生成的路徑如下:
Archetype project created in D:\smilehappiness-project-template\target\generated-sources\archetype

在這里插入圖片描述

3.4 執(zhí)行安裝命令

3.3步驟生成的Archetype project created in D:\smilehappiness-project-template\target\generated-sources\archetype目錄中,依次執(zhí)行以下兩個(gè)命令:

命令一 mvn clean install

在這里插入圖片描述

在這里插入圖片描述

出現(xiàn)BUILD SUCCESS之后,就可以在本地maven倉庫的根目錄中看到一個(gè)archetype-catalog.xml文件(注:如果重復(fù)操作可能需要把改文件刪除,否則會(huì)出現(xiàn)delete fail錯(cuò)誤),打開該文件,可以看到如下內(nèi)容:

在這里插入圖片描述

<?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.smilehappiness</groupId>
      <artifactId>smilehappiness-project-template-archetype</artifactId>
      <version>1.0.0-RELEASE</version>
      <description>project-template</description>
    </archetype>
  </archetypes>
</archetype-catalog>

命令二 mvn archetype:crawl
關(guān)于該命令,簡單介紹如下:

Full name:
org.apache.maven.plugins:maven-archetype-plugin:3.2.1:crawl

Description:
Crawl a Maven repository (filesystem, not HTTP) and creates a catalog file.

mvn archetype:crawl執(zhí)行過程如下:

在這里插入圖片描述

在這里插入圖片描述

最后生成的archetype-resource內(nèi)容如下:

在這里插入圖片描述

3.5 將smilehappiness-project-template deploy到私服

登錄到nexus私服,將template deploy上傳到私服:

在這里插入圖片描述

4 基于腳手架生成新的項(xiàng)目

以下演示基于IDEA操作,具體如下

4.1 添加腳手架

在idea中引入腳手架,點(diǎn)擊菜單File-> new -> Project,選擇maven,勾選Create from archetype,點(diǎn)擊 Add Archetype按鈕,填寫groupIdartifactId以及version信息(就是archetype-catalog.xml文件中的信息),點(diǎn)擊OK,示例如下:

在這里插入圖片描述

注:有可能會(huì)遇到一些問題,腳手架生成了,私服倉庫也有,但是自己創(chuàng)建的時(shí)候就是不顯示,解決辦法:

C:\Users\26310\AppData\Local\JetBrains\IntelliJIdea2021.2\Maven\Indices這個(gè)目錄(根據(jù)自己實(shí)際目錄進(jìn)行調(diào)整)下面自己新建一個(gè)UserArchetypes.xml,填寫如下內(nèi)容:

在這里插入圖片描述

<archetypes>
    <archetype groupId="com.smilehappiness" artifactId="smilehappiness-project-template-archetype" version="1.0.0-RELEASE" />
</archetypes>

使用自定義的archetype:

在這里插入圖片描述

4.2 創(chuàng)建新項(xiàng)目

點(diǎn)擊next,進(jìn)入new project界面,填寫項(xiàng)目名稱、項(xiàng)目位置、groupId、artifactId、version,然后點(diǎn)擊確定,此時(shí)基于腳手架生成的項(xiàng)目,即創(chuàng)建完畢,示例如下:

在這里插入圖片描述

創(chuàng)建后的生成的項(xiàng)目結(jié)構(gòu)如下:  

在這里插入圖片描述

4.3 命令行基于腳手架生成新的項(xiàng)目

如果4.1步驟,操作失敗了,可以使用命令行來生成腳手架,具體操作步驟如下:

第一步,輸入命令:mvn archetype:generate

第二步,選擇指定的的archetype,輸入對(duì)應(yīng)的碼值即可

在這里插入圖片描述

第三步:輸入對(duì)應(yīng)的groupId、artifactId、version等,然后輸入y確認(rèn),即可生成

Define value for property 'groupId': com.smilehappiness
Define value for property 'artifactId': smile-user-test
Define value for property 'version' 1.0-SNAPSHOT: : 1.0.0-RELEASE
Define value for property 'package' com.smilehappiness: : com.smilehappiness.smile
Confirm properties configuration:
groupId: com.smilehappiness
artifactId: smile-user-test
version: 1.0.0-RELEASE
package: com.smilehappiness.smile
 Y: : y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: smilehappiness-project-template-archetype:1.0.0-RELEASE
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.smilehappiness
[INFO] Parameter: artifactId, Value: smile-user-test
[INFO] Parameter: version, Value: 1.0.0-RELEASE
[INFO] Parameter: package, Value: com.smilehappiness.smile
[INFO] Parameter: packageInPathFormat, Value: com/smilehappiness/smile
[INFO] Parameter: package, Value: com.smilehappiness.smile
[INFO] Parameter: version, Value: 1.0.0-RELEASE
[INFO] Parameter: groupId, Value: com.smilehappiness
[INFO] Parameter: artifactId, Value: smile-user-test
[WARNING] Don't override file D:\smile-user-test\.idea\inspectionProfiles\Project_Default.xml
[INFO] Parent element not overwritten in D:\smile-user-test\smilehappiness-api\pom.xml
[INFO] Parent element not overwritten in D:\smile-user-test\smilehappiness-api-entity\pom.xml
[INFO] Parent element not overwritten in D:\smile-user-test\smilehappiness-common\pom.xml
[INFO] Parent element not overwritten in D:\smile-user-test\smilehappiness-dao\pom.xml
[INFO] Parent element not overwritten in D:\smile-user-test\smilehappiness-integrate\pom.xml
[INFO] Parent element not overwritten in D:\smile-user-test\smilehappiness-job\pom.xml
[INFO] Parent element not overwritten in D:\smile-user-test\smilehappiness-model\pom.xml
[INFO] Parent element not overwritten in D:\smile-user-test\smilehappiness-msg\pom.xml
[INFO] Parent element not overwritten in D:\smile-user-test\smilehappiness-service\pom.xml
[INFO] Parent element not overwritten in D:\smile-user-test\smilehappiness-web\pom.xml
[INFO] Parent element not overwritten in D:\smile-user-test\smilehappiness-start\pom.xml
[INFO] Project created from Archetype in dir: D:\smile-user-test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:20 min
[INFO] Finished at: 2022-05-29T20:39:06+08:00
[INFO] ------------------------------------------------------------------------

注: 模板腳手架中,只有包下面有文件的才會(huì)生成,而空包不會(huì)生成

參考資料:https://www.bbsmax.com/A/8Bz8N1Zo5x/

到此這篇關(guān)于Java中如何快速構(gòu)建項(xiàng)目腳手架的文章就介紹到這了,更多相關(guān)Java中如何快速構(gòu)建項(xiàng)目腳手架內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論