公司每次做新項目都要去搭建一套新的架子,形成新的服務。其實大部分的架子都差不多,但是每次都要重新建包,引入pom依賴,配置數據源,修改日志文件,配置文件修改,類目錄結構修改等等。每次步驟都差不多,主要是耗時,有時候甚至出個問題都要解決半天。那么有沒有一種方式可以從這里面解放出來? 有,就是maven的腳手架。
以我自己搭建的一套服務為准,現在把這套搭建好的架子使用腳手架的方式重新生成。先看下當前的目錄

這個是當前大概的目錄結構,我以這個為基准,在搭建一套新的服務。就是變下名稱,以下是步驟:
1)在當前目錄下 使用maven命令:
mvn clean archetype:create-from-project -Darchetype.properties
主要是使用archetype生成源碼。注意一定要是在指定的target目錄下,注意一定要是在指定的target目錄下,
注意一定要是在指定的target目錄下,重要事情說3遍。以我的為例:
/Users/fan/workspace/summary/target/generated-sources/archetype
3) 第二步執行成功后, 使用命令.
mvn clean install
主要是使用成archetype生成jar包。注意 當前操作仍然是在target目錄下。
注意 當前操作仍然是在target目錄下。
注意 當前操作仍然是在target目錄下。 以我的為例
/Users/fan/workspace/summary/target/generated-sources/archetype
以上三步都成功后,就可以使用命令行生成項目了
4)以這個項目為例,打開一個新的文件,使用命令行:
mvn archetype:generate -DarchetypeGroupId=com.sq.summary
-DarchetypeArtifactId=summary-archetype
-DarchetypeVersion=0.0.1-SNAPSHOT
-Ddatabase=olympic-winter
-DgroupId=com.sq.summary
-DartifactId=summary-archetype
-Dversion=0.0.1-SNAPSHOT
-DapplicationName=summaryTest
-Dpackage=com.sq.summary
----------------------------------------------------------------
需要配置的參數說明
-DarchetypeGroupId=com.sq (可以看target/generated-sources/archetype/的pom文件的groupId)
-DarchetypeArtifactId=summary-archetype (可以看target/generated-sources/archetype/的pom文件的artifactId)
-DarchetypeVersion=1.0-SNAPSHOT (可以看target/generated-sources/archetype/的pom文件的version)
-DartifactId=summary-archetype(新的artifactId)
-Dversion=0.0.1-SNAPSHOT(新的version)
-DapplicationName=summaryTest(新的服務名稱)
-Dpackage=com.sq.summary(新的包名)

生成完成打開查看和現在的目錄一模一樣,只是包名、version等改為了我們這次新命名的了。


Invoker process ended with result different than 0!
Execution repackage of goal org.springframework.boot:spring-boot-maven-plugi
只有子模塊需要生成可執行JAR包,其他幾個模塊是不需要可執行的,spring boot 默認幫我加入了如下build策略。不需要的pom.xml里面去掉這個就行了。我的只有對外提供服務的api有這個,其他的都不需要。

參考:https://blog.csdn.net/qq_41264674/article/details/106539584
3)執行 第三步 mvn clean install 時候報錯:
[ERROR] Errors:
[ERROR] SqSummaryServiceApplicationTests » IllegalState Unable to find a @SpringBootCo...

原因:單元測試類的問題,我是刪除了不需要的單元測試類。參考:
https://blog.csdn.net/csdn_am/article/details/79757097
4)jar包生成成功后,執行命令
mvn archetype:generate -DarchetypeGroupId=com.sq.summary -DarchetypeArtifactId=summary -DarchetypeVersion=0.0.1-SNAPSHOT -Ddatabase=olympic-winter -DgroupId=com.sq.summary -DartifactId=summary -Dversion=0.0.1-SNAPSHOT -DapplicationName=summaryTest -Dpackage=com.sq.summary
報錯: The desired archetype does not exist
原因:使用 mvn的archetype生成的jar包的默認artifactId帶有后綴 -archetype,但是命令行里面沒有帶 需要帶上才行:
mvn archetype:generate -DarchetypeGroupId=com.sq.summary -DarchetypeArtifactId=summary-archetype -DarchetypeVersion=0.0.1-SNAPSHOT -Ddatabase=olympic-winter -DgroupId=com.sq.summary -DartifactId=summary-archetype -Dversion=0.0.1-SNAPSHOT -DapplicationName=summaryTest -Dpackage=com.sq.summary
遇到的問題大概有這些。記錄下。
