0%

编译运行halo博客的一些问题汇总

前言

halo博客是什么这里不多说,简单介绍一下:前后端分离开发,RESTful风格接口,功能短小精悍,支持APP客户端管理博客内容。

这样就不得不让人想去欣赏一下它的源文件,本文讲解自行编译运行中遇到的一些问题。

注意:本文用的是idea+jdk8开发环境。

halo传送门

以下是我转换为maven之后的代码,代码没有区别,只是改为了maven构建。

halo-maven传送门

问题1:Gradle构建问题

你很可能会遇到如下错误

1
2
3
4
5
6
7
* What went wrong:
Plugin [id: 'org.springframework.boot', version: '2.2.6.RELEASE'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'org.springframework.boot:org.springframework.boot.gradle.plugin:2.2.6.RELEASE')
Searched in the following repositories:
Gradle Central Plugin Repository

以上问题在我尝试切换了几个不同gradle版本后还是无法构建项目,百度google基本搜不到这个问题,但是 org.springframework.boot这个插件是肯定存在的,问题应该还是出在gradleidea的版本上,但是我不打算去折腾了,加上一直用的是mavengradle不熟,直接切换为maven构建。

问题1:Gradle构建问题-解决方法

这里我新建了一个maven项目,将gradle依赖转换为maven的依赖形式即可,我写了个转换代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package run.halo.app;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
* Create by yang 2020-04-28 10:19
*/
public class Test {
public static void main(String[] args) throws Exception {
Map<String,String[]> qqqMap = new HashMap<String,String[]>();
Map<String,String> aaaMap = new HashMap<String,String>();

BufferedReader qqq = new BufferedReader(new InputStreamReader(Test.class.getResourceAsStream("qqq.txt")));
BufferedReader aaa = new BufferedReader(new InputStreamReader(Test.class.getResourceAsStream("aaa.txt")));
String line = null;
while((line=qqq.readLine())!=null){
String s = line.split(" ")[1].replaceAll("\"", "");
String[] info = s.split(":");
qqqMap.put(s,info);
}

while((line=aaa.readLine())!=null){
String[] info = line.replaceAll("[\"\\s]", "").split("=");
aaaMap.put(info[0],info[1]);
}

StringBuilder sb = new StringBuilder();
for (String s : qqqMap.keySet()) {
sb.append("<dependency>\n");
sb.append("\t<groupId>"+qqqMap.get(s)[0]+"</groupId>\n");
sb.append("\t<artifactId>"+qqqMap.get(s)[1]+"</artifactId>\n");
if(qqqMap.get(s).length==3){
String s1 = aaaMap.get(qqqMap.get(s)[2].substring(1));
if(s1!=null){
sb.append("\t<version>${"+qqqMap.get(s)[2].substring(1)+"}</version>\n");
}
}
sb.append("</dependency>\n");
}

System.out.println(sb);
}
}

其中qqq.txt文件内容如下,这里只是显示一部分,实际还有很多:

1
2
3
4
5
6
implementation "org.springframework.boot:spring-boot-starter-actuator"
implementation "org.springframework.boot:spring-boot-starter-data-jpa"
implementation "org.springframework.boot:spring-boot-starter-web"
implementation "org.springframework.boot:spring-boot-starter-undertow"
implementation "org.springframework.boot:spring-boot-starter-freemarker"
.....篇幅原因省略......

aaa.txt文件内容如下,这里只是显示一部分,实际还有很多:

1
2
3
4
hutoolVersion =  "5.3.2"
upyunSdkVersion = "4.2.0"
qiniuSdkVersion = "7.2.28"
.....篇幅原因省略......

跑完这个代码,将得到转换后的maven依赖形式,将其复制到pom.xml的dependencies节点下即可,可以看到,maven已经成功导入依赖。

问题2:buildProperties 问题

当我庆幸终于TM能跑了,控制台又出现了这个,我TM能怎么办。

1
2
3
Description:

Field buildProperties in run.halo.app.config.HaloConfiguration required a bean of type 'org.springframework.boot.info.BuildProperties' that could not be found.

问题2:buildProperties 问题-解决方法

这个问题容易搜到,贴一下博文地址,感谢!地址

具体就是在run之前跑一下产生版本信息,halo有代码会访问这个信息

1
springboot:build-info

那么如何在run之前自动执行build-info? 这里给不熟悉idea或者maven得人说下,本文唯一一张图奉上,点击红框得+号,再点击Run Maven Goal添加spring-boot:build-info。

JoBlvT.png

问题3:缺少主题文件-解决方法

去官网下载一个主题解压缩放在templates/themes下,地址halo主题仓库

1
2
3
4
java.lang.RuntimeException: Initialize internal theme to user path error
at run.halo.app.listener.StartedListener.initThemes(StartedListener.java:152) ~[classes/:na]
at run.halo.app.listener.StartedListener.onApplicationEvent(StartedListener.java:70) ~[classes/:na]
at run.halo.app.listener.StartedListener.onApplicationEvent(StartedListener.java:40) ~[classes/:na]

问题4:checkstyle问题-解决方法

以上运行起来是没有问题了,但是执行mvn package会出现

1
Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:3.1.1:check (checkstyle) on project app: Failed during checkstyle execution

解决就是两种

1.直接禁用checkstyle检查

2.复制halo(原作者项目)config目录到halo-maven(自己新建的)根目录,添加maven-checkstyle-plugin插件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<!-- 此处为自定义规范文件地址,confing目录与pom.xml平行 -->
<configLocation>config/checkstyle.xml</configLocation>
</configuration>
<executions>
<execution>
<id>checkstyle</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<failOnViolation>true</failOnViolation>
</configuration>
</execution>
</executions>
</plugin>

结语

至此,问题完美解决,我们可以学习或者参考下作者的代码风格,甚至自行修改向halo社区提供代码。

再次感谢halo作者以及社区!

坚持原创技术分享,您的支持将鼓励我继续创作!
YANG 微信支付

微信支付

YANG 支付宝

支付宝