我想要做的是将所有 spring-boot-starters 提取到一个单独的应用程序中,将它们全部打包到一个 JAR 中(使用 gradle shadow 插件),然后将其部署到本地 nexus 工件存储库,这样我就可以导入它及其对我的 Spring Boot 应用程序的所有依赖项。
这是包含 spring-boot-starters 的 app 的 build.gradle:
buildscript {
ext {
springBootVersion = '2.1.0.RELEASE'
}
repositories {
mavenCentral()
jcenter()
maven {
url "http://localhost:8081/repository/testowe/"
credentials {
username 'admin'
password 'admin123'
}
}
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:4.0.4'
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
subprojects {
apply plugin: 'java-library'
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'maven-publish'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'org.springframework.boot'
version = '1.0'
sourceCompatibility = 1.11
jar { enabled = true }
shadowJar { classifier(null) }
repositories {
jcenter()
mavenCentral()
maven {
url "http://localhost:8081/repository/testowe/"
credentials {
username 'admin'
password 'admin123'
}
}
}
publishing {
publications {
shadow(MavenPublication) { publication ->
project.shadow.component(publication)
}
}
repositories {
maven {
url "http://localhost:8081/repository/testowe/"
credentials {
username 'admin'
password 'admin123'
}
}
}
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web') {
transitive = true
}
compile('org.springframework.boot:spring-boot-starter-actuator') {
transitive = true
}
compile('org.springframework.boot:spring-boot-starter-data-jpa') {
transitive = true
}
compile('org.springframework.boot:spring-boot-starter-security') {
transitive = true
}
compile('org.springframework.boot:spring-boot-starter-test') {
transitive = true
}
}
}
这是我想要运行的 spring-boot 应用程序的 build.gradle,所有 spring-boot-starter 依赖项都来自构建的 JAR:
buildscript {
ext {
springBootVersion = '2.1.0.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = 11
repositories {
mavenCentral()
maven {
url "http://localhost:8081/repository/testowe/"
credentials {
username 'admin'
password 'admin123'
}
}
}
dependencies {
// When I'm running app with starters placed here everything works fine
// implementation('org.springframework.boot:spring-boot-starter-data-jpa')
// implementation('org.springframework.boot:spring-boot-starter-security')
// implementation('org.springframework.boot:spring-boot-starter-test')
// implementation('org.springframework.boot:spring-boot-starter-web')
// When I'm trying to run it with my JAR it doesn't work
implementation("pl.mplan:web-common:1.0")
compile group: 'org.javassist', name: 'javassist', version: '3.23.2-GA'
compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'
runtimeOnly('org.postgresql:postgresql')
compileOnly('org.projectlombok:lombok')
testImplementation('org.springframework.boot:spring-boot-starter-test')
compile("com.querydsl:querydsl-core:4.2.1")
compile("com.querydsl:querydsl-jpa:4.2.1")
compile("com.querydsl:querydsl-apt:4.2.1:jpa")
}
compileJava {
options.annotationProcessorGeneratedSourcesDirectory = file("$projectDir/generated/java")
}
idea {
module {
sourceDirs += file("$projectDir/generated/java")
}
}
当我尝试使用 gradle bootRun 任务运行应用程序时,这就是我得到的:
17:55:32.169 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Retrieved dependent beans for bean 'objectPostProcessor': [org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration]
17:55:32.169 [main] DEBUG org.springframework.beans.factory.support.DisposableBeanAdapter - Invoking destroy() on bean with name 'objectPostProcessor'
17:55:32.175 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230)
at pl.mplan.brew.it.BrewItBackendApplication.main(BrewItBackendApplication.java:12)
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:204)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:178)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152)
... 8 common frames omitted
(我正在使用 gradle 4.8 和 shadow 插件 4.0.4)
任何想法如何解决它?提前致谢!