idea創建springboot項目時,一般使用默認的start.spring.io來初始化項目,為了能直接生成帶有自己版本信息的項目,就想搭一個本地的spring-initializr。
參考
訪問start.spring.io
可以獲取到源碼地址spring-io/start.spring.io: https://start.spring.io。
本文使用的spring-initializr源碼版本是spring-io/start.spring.io at 8ddc6a9ab0c1f62c524571f1ab8dc0ee8b45a63f
IDE直接啟動
通過idea直接啟動后台,無頁面,但是能用,這個是比較簡單的。
打包后啟動
打包后啟動,這個比較適合部署在服務器上,並且是有前端頁面的。
步驟
- 配置jdk
- 配置maven
- 配置git
- 拉取源碼,安裝
- 遇到問題解決問題。根據報錯提示,找解決辦法,一般解決問題的根源就在於找控制台首次出現
Error
的地方
安裝maven並配置環境變量后,刷新環境變量
wget https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
tar zxvf apache-maven-3.6.3-bin.tar.gz -C /opt/module
mv /opt/module/apache-maven-3.6.3/ /opt/module/maven/
#添加環境變量
vi /etc/profile.d/maven-env.sh
# 里面輸入內容
export PATH=$PATH:/opt/module/maven/bin
# ----
# 刷新環境變量
source /etc/profile
配置阿里鏡像
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
檢驗是否成功
[root@iZp62wy25rtcetZ ~]# mvn -v
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /opt/module/maven
Java version: 1.8.0_301, vendor: Oracle Corporation, runtime: /opt/module/jdk1.8.0_301/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.18.0-193.28.1.el8_2.x86_64", arch: "amd64", family: "unix"
安裝git
yum -y install git
[root@iZp62wy25rtcetZ ~]# git --version
git version 2.27.0
拉取源碼
wget https://github.com/spring-io/start.spring.io/archive/8ddc6a9ab0c1f62c524571f1ab8dc0ee8b45a63f.zip
unzip 8ddc6a9ab0c1f62c524571f1ab8dc0ee8b45a63f.zip
cd start.spring.io-8ddc6a9ab0c1f62c524571f1ab8dc0ee8b45a63f/
mvn clean package -Dmaven.test.skip=true
在安裝過程中,可能會比較慢,比如install node and yarn的時候,耐心一點即可。
錯誤一
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for start.spring.io 0.0.1-SNAPSHOT:
[INFO]
[INFO] start.spring.io .................................... SUCCESS [ 17.975 s]
[INFO] start.spring.io client ............................. SUCCESS [05:40 min]
[INFO] start.spring.io website ............................ FAILURE [04:43 min]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11:30 min
[INFO] Finished at: 2021-10-12T13:54:27+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal pl.project13.maven:git-commit-id-plugin:4.0.5:revision (default) on project start-site: .git directory is not found! Please specify a valid [dotGitDirectory] in your pom.xml -> [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/MojoExecutionException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR] mvn <args> -rf :start-site
解決方法:找到你想項目-> github源碼根目錄\start-site\pom.xml -->編輯此文件 -->搜索“git-commit-id-plugin” -->新增configuration
部分代碼
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<configuration>
<!--".git"文件夾未找到時,構建是否失敗;若設置true,則構建失敗;若設置false,則跳過執行該目標;默認值:true;-->
<failOnNoGitDirectory>false</failOnNoGitDirectory>
</configuration>
</plugin>
重新進行安裝
mvn clean package -Dmaven.test.skip=true
安裝成功
啟動項目
cd start-site/target/
nohup java -jar start-site-exec.jar
訪問8080端口
錯誤
錯誤一:nodejs命令找不到
# 首先要添加 Nodejs 14.x 存儲庫(root 用戶執行)
curl -sL https://rpm.nodesource.com/setup_14.x | bash -
# 開始安裝 Nodejs
yum -y install nodejs
# 安裝 gcc-c++ 以便從 npm 構建原生插件
yum install gcc-c++ make
# 檢查 node、npm 安裝是否成功
node --version // v14.15.4
npm --version // 6.14.10
錯誤二:報錯node-gyp
sudo npm install -g node-gyp
yum install gcc-c++
yum groupinstall 'Development Tools'
錯誤三:找不到python
yum -y install python3
# 測試安裝成功
[root@localhost ~]# python
Python 2.7.5 (default, Oct 14 2020, 14:45:30)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>>
配置自定義信息
修改start-site
下面的application.yml
initializr:
version:
value: 1.0.0
group-id:
value: top.meethigher
description: chenchuancheng's demo
description:
value: chenchuancheng's demo
package-name:
value: top.meethigher
types:
- name: Maven Project
id: maven-project
description: Generate a Maven based project archive.
tags:
build: maven
format: project
default: true
action: /starter.zip
- name: Maven POM
id: maven-build
description: Generate a Maven pom.xml.
tags:
build: maven
format: build
default: false
action: /pom.xml
- name: Gradle Project
id: gradle-project
description: Generate a Gradle based project archive.
tags:
build: gradle
format: project
default: false
action: /starter.zip
- name: Gradle Config
id: gradle-build
description: Generate a Gradle build file.
tags:
build: gradle
format: build
default: false
action: /build.gradle
packagings:
- name: Jar
id: jar
default: true
- name: War
id: war
default: false
javaVersions:
- id: 17
default: false
- id: 11
default: false
- id: 1.8
name: 8
default: true
languages:
- name: Java
id: java
default: true
- name: Kotlin
id: kotlin
default: false
- name: Groovy
id: groovy
default: false
最后的效果,看圖。