Skip to content

Commit f4fe9ea

Browse files
authored
Merge pull request #854 from simple-robot/pref-resources-load
简单优化spring中加载resources时资源不存在的错误提示
2 parents 47cc1de + 44d7393 commit f4fe9ea

File tree

15 files changed

+212
-12
lines changed

15 files changed

+212
-12
lines changed

.github/workflows/kdoc.yml.bk

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
uses: actions/setup-java@v4
2323
with:
2424
distribution: 'zulu'
25-
java-version: 11
25+
java-version: 21
2626
cache: 'gradle'
2727

2828
# setup Gradle

.github/workflows/publish-release.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
uses: actions/setup-java@v4
3131
with:
3232
distribution: 'zulu'
33-
java-version: 11
33+
java-version: 21
3434
#cache: 'gradle'
3535

3636
- name: Run All Tests
@@ -74,7 +74,7 @@ jobs:
7474
uses: actions/setup-java@v4
7575
with:
7676
distribution: 'zulu'
77-
java-version: 11
77+
java-version: 21
7878
cache: 'gradle'
7979

8080
# setup Gradle
@@ -113,7 +113,7 @@ jobs:
113113
uses: actions/setup-java@v4
114114
with:
115115
distribution: 'zulu'
116-
java-version: 11
116+
java-version: 21
117117
cache: 'gradle'
118118

119119
# setup Gradle

.github/workflows/publish-snapshot.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
uses: actions/setup-java@v4
4242
with:
4343
distribution: 'zulu'
44-
java-version: 11
44+
java-version: 21
4545
cache: 'gradle'
4646

4747
- name: Run All Tests
@@ -82,7 +82,7 @@ jobs:
8282
uses: actions/setup-java@v4
8383
with:
8484
distribution: 'zulu'
85-
java-version: 11
85+
java-version: 21
8686
cache: 'gradle'
8787

8888
# setup Gradle

.github/workflows/test-branch.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- uses: actions/setup-java@v4
2121
with:
2222
distribution: 'zulu'
23-
java-version: 11
23+
java-version: 21
2424
cache: 'gradle'
2525

2626
- name: Run All Tests
@@ -50,7 +50,7 @@ jobs:
5050
- uses: actions/setup-java@v4
5151
with:
5252
distribution: 'zulu'
53-
java-version: 11
53+
java-version: 21
5454
cache: 'gradle'
5555
- name: Run Detekt
5656
uses: gradle/gradle-build-action@v3

.github/workflows/test-v4-branch.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ jobs:
5454
- uses: actions/setup-java@v4
5555
with:
5656
distribution: 'zulu'
57-
java-version: 11
57+
java-version: 21
5858
cache: 'gradle'
5959
- name: Run Detekt
6060
uses: gradle/gradle-build-action@v3

build.gradle.kts

+4
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ detekt {
102102
tasks.withType<Detekt>().configureEach {
103103
// internal 处理器不管
104104
exclude("internal-processors/**")
105+
// tests 不管
106+
exclude("tests/**")
105107

106108
include("**/src/*Main/kotlin/**/*.kt")
107109
include("**/src/*Main/kotlin/**/*.java")
@@ -142,6 +144,8 @@ apiValidation {
142144
listOf(
143145
"interface-uml-processor",
144146
"simbot-test",
147+
"tests",
148+
"spring-boot-starter-test",
145149
)
146150
)
147151

qodana.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ profile:
1818
# paths:
1919
# - <path/where/not/run/inspection>
2020

21-
projectJDK: azul-11 #(Applied in CI/CD pipeline)
21+
projectJDK: azul-21 #(Applied in CI/CD pipeline)
2222

2323
#Execute shell command before Qodana execution (Applied in CI/CD pipeline)
2424
#bootstrap: sh ./prepare-qodana.sh

settings.gradle.kts

+3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ include(":simbot-cores:simbot-core-spring-boot-starter-v2")
6767
// extensions
6868
include(":simbot-extensions:simbot-extension-continuous-session")
6969

70+
// local tests
71+
include(":tests:spring-boot-starter-test")
72+
7073
// local
7174
// if (!(System.getProperty("IS_CI") ?: System.getenv("IS_CI")).toBoolean()) {
7275
// include(":tests:native-impl-test")

simbot-cores/simbot-core-spring-boot-starter-common/api/simbot-core-spring-boot-starter-common.api

+2
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,14 @@ public final class love/forte/simbot/spring/common/application/SpringApplication
109109
public final fun getAutoStartBots ()Z
110110
public final fun getAutoStartMode ()Llove/forte/simbot/spring/common/application/BotAutoStartMode;
111111
public final fun getConfigurationJsonResources ()Ljava/util/Set;
112+
public final fun getIgnoreIOExceptionForResourcesLoad ()Z
112113
public final fun setAutoRegistrationFailurePolicy (Llove/forte/simbot/spring/common/application/BotRegistrationFailurePolicy;)V
113114
public final fun setAutoRegistrationMismatchConfigurableBotManagerPolicy (Llove/forte/simbot/spring/common/application/MismatchConfigurableBotManagerPolicy;)V
114115
public final fun setAutoRegistrationResourceLoadFailurePolicy (Llove/forte/simbot/spring/common/application/BotConfigResourceLoadFailurePolicy;)V
115116
public final fun setAutoStartBots (Z)V
116117
public final fun setAutoStartMode (Llove/forte/simbot/spring/common/application/BotAutoStartMode;)V
117118
public final fun setConfigurationJsonResources (Ljava/util/Set;)V
119+
public final fun setIgnoreIOExceptionForResourcesLoad (Z)V
118120
}
119121

120122
public final class love/forte/simbot/spring/common/application/SpringApplicationConfigurationProperties$BotProperties$Companion {

simbot-cores/simbot-core-spring-boot-starter-common/src/main/kotlin/love/forte/simbot/spring/common/application/SpringApplicationConfigurationProperties.kt

+7
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ public class SpringApplicationConfigurationProperties {
4848
internal const val DEFAULT_JSON_RESOURCE_PATTERN: String = "classpath:simbot-bots/*.bot.json"
4949
}
5050

51+
/**
52+
* 是否忽略读取 [configurationJsonResources] 过程中产生的 `IOException`。
53+
*
54+
* @since 4.0.1
55+
*/
56+
public var ignoreIOExceptionForResourcesLoad: Boolean = true
57+
5158
/**
5259
* 需要加载的所有组件下它们对应的所有 JSON 格式 的 bot 配置文件。
5360
* 默认为 [`classpath:simbot-bots/\*.bot.json`][DEFAULT_JSON_RESOURCE_PATTERN]

simbot-cores/simbot-core-spring-boot-starter-v2/src/main/kotlin/love/forte/simbot/spring2/configuration/application/DefaultSimbotApplicationProcessor.kt

+21
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import org.springframework.context.annotation.Configuration
4747
import org.springframework.core.io.Resource
4848
import org.springframework.core.io.support.PathMatchingResourcePatternResolver
4949
import java.io.FileNotFoundException
50+
import java.io.IOException
5051

5152
/**
5253
* [SimbotApplicationProcessor] 的默认实现
@@ -150,6 +151,7 @@ private class BotAutoLoader(
150151

151152
fun load() {
152153
val resolver = PathMatchingResourcePatternResolver()
154+
val ignoreIO = properties.ignoreIOExceptionForResourcesLoad
153155

154156
val botList =
155157
properties.configurationJsonResources
@@ -172,6 +174,25 @@ private class BotAutoLoader(
172174
fne
173175
)
174176

177+
emptySequence()
178+
} catch (ie: IOException) {
179+
if (!ignoreIO) {
180+
throw ie
181+
}
182+
logger.warn(
183+
"Bot configuration resource path [{}] could not be resolved " +
184+
"because of IOException(message={}), will be skipped.",
185+
it,
186+
ie.localizedMessage,
187+
)
188+
logger.debug(
189+
"Bot configuration resource path [{}] could not be resolved " +
190+
"because of IOException(message={}), will be skipped.",
191+
it,
192+
ie.localizedMessage,
193+
ie
194+
)
195+
175196
emptySequence()
176197
}
177198
}

simbot-cores/simbot-core-spring-boot-starter/src/main/kotlin/love/forte/simbot/spring/configuration/application/DefaultSimbotApplicationProcessor.kt

+23-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import org.springframework.context.annotation.Configuration
4747
import org.springframework.core.io.Resource
4848
import org.springframework.core.io.support.PathMatchingResourcePatternResolver
4949
import java.io.FileNotFoundException
50+
import java.io.IOException
5051

5152
/**
5253
* [SimbotApplicationProcessor] 的默认实现
@@ -150,6 +151,7 @@ private class BotAutoLoader(
150151

151152
fun load() {
152153
val resolver = PathMatchingResourcePatternResolver()
154+
val ignoreIO = properties.ignoreIOExceptionForResourcesLoad
153155

154156
val botList =
155157
properties.configurationJsonResources
@@ -160,18 +162,37 @@ private class BotAutoLoader(
160162
} catch (fne: FileNotFoundException) {
161163
logger.warn(
162164
"Bot configuration resource path [{}] could not be resolved " +
163-
"because of FileNotFoundException(message={}), will be skip.",
165+
"because of FileNotFoundException(message={}), will be skipped.",
164166
it,
165167
fne.localizedMessage,
166168
)
167169
logger.debug(
168170
"Bot configuration resource path [{}] could not be resolved " +
169-
"because of FileNotFoundException(message={}), will be skip.",
171+
"because of FileNotFoundException(message={}), will be skipped.",
170172
it,
171173
fne.localizedMessage,
172174
fne
173175
)
174176

177+
emptySequence()
178+
} catch (ie: IOException) {
179+
if (!ignoreIO) {
180+
throw ie
181+
}
182+
logger.warn(
183+
"Bot configuration resource path [{}] could not be resolved " +
184+
"because of IOException(message={}), will be skipped.",
185+
it,
186+
ie.localizedMessage,
187+
)
188+
logger.debug(
189+
"Bot configuration resource path [{}] could not be resolved " +
190+
"because of IOException(message={}), will be skipped.",
191+
it,
192+
ie.localizedMessage,
193+
ie
194+
)
195+
175196
emptySequence()
176197
}
177198
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2024. ForteScarlet.
3+
*
4+
* Project https://github.com/simple-robot/simpler-robot
5+
6+
*
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* Lesser GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the Lesser GNU General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
plugins {
25+
kotlin("jvm")
26+
id("org.springframework.boot") version "3.3.0"
27+
id("io.spring.dependency-management") version "1.1.5"
28+
kotlin("plugin.spring") version "2.0.0"
29+
}
30+
31+
java {
32+
toolchain {
33+
languageVersion = JavaLanguageVersion.of(17)
34+
}
35+
}
36+
37+
repositories {
38+
mavenCentral()
39+
}
40+
41+
dependencies {
42+
implementation(project(":simbot-cores:simbot-core-spring-boot-starter"))
43+
implementation("org.springframework.boot:spring-boot-starter")
44+
implementation("org.jetbrains.kotlin:kotlin-reflect")
45+
testImplementation("org.springframework.boot:spring-boot-starter-test")
46+
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
47+
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
48+
}
49+
50+
kotlin {
51+
compilerOptions {
52+
freeCompilerArgs.addAll("-Xjsr305=strict")
53+
}
54+
}
55+
56+
tasks.withType<Test> {
57+
useJUnitPlatform()
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2024. ForteScarlet.
3+
*
4+
* Project https://github.com/simple-robot/simpler-robot
5+
6+
*
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* Lesser GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the Lesser GNU General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
package love.forte.spring.test
25+
26+
import org.springframework.boot.autoconfigure.SpringBootApplication
27+
import org.springframework.boot.runApplication
28+
29+
/**
30+
*
31+
* @author ForteScarlet
32+
*/
33+
@SpringBootApplication
34+
class Main
35+
36+
fun main(args: Array<String>) {
37+
runApplication<Main>(*args)
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2024. ForteScarlet.
3+
*
4+
* Project https://github.com/simple-robot/simpler-robot
5+
6+
*
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* Lesser GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the Lesser GNU General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
package love.forte.spring.test
25+
26+
import kotlinx.coroutines.runBlocking
27+
import love.forte.simbot.application.Application
28+
import love.forte.simbot.spring.EnableSimbot
29+
import org.springframework.beans.factory.getBean
30+
import org.springframework.boot.autoconfigure.SpringBootApplication
31+
import org.springframework.boot.runApplication
32+
33+
34+
/**
35+
*
36+
* @author ForteScarlet
37+
*/
38+
@SpringBootApplication
39+
@EnableSimbot
40+
class Main
41+
42+
fun main(args: Array<String>) {
43+
val context = runApplication<Main>(*args)
44+
val app = context.getBean<Application>()
45+
runBlocking { app.join() }
46+
}

0 commit comments

Comments
 (0)