Spring Cloud版本更改后,无法构建标准的Jar

时间:2019-03-27 07:51:19

标签: java spring build spring-cloud

我的./build.gradle

plugins {
//  id 'org.springframework.boot' version '2.1.4.BUILD-SNAPSHOT'
    id 'org.springframework.boot' version '1.5.9.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.yd.spring'
version = '1.0.0'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
    maven { url 'https://repo.spring.io/snapshot' }
    maven { url 'https://repo.spring.io/milestone' }
}

ext {
//  set('springCloudVersion', 'Greenwich.BUILD-SNAPSHOT')
    set('springCloudVersion', 'Edgware.SR2')
}

dependencies {
    implementation 'com.google.code.gson:gson'
    implementation 'redis.clients:jedis'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-web'

    runtimeOnly 'mysql:mysql-connector-java'
    implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.0'

    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-zuul'
    implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

注释代码是依赖项的版本,然后是版本更改。

更改后,我的想法可以成功运行项目

但是当我构建一个Jar使用命令时

./gradlew clean build

将被打印出来

15:43:33: Executing task 'build'...


> Task :compileJava
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

> Task :processResources
> Task :classes
> Task :findMainClass
> Task :jar
> Task :bootRepackage
> Task :assemble
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses

> Task :test
2019-03-27 15:43:43.837  INFO 31765 --- [       Thread-9] o.s.w.c.s.GenericWebApplicationContext   : Closing org.springframework.web.context.support.GenericWebApplicationContext@7c93dc34: startup date [Wed Mar 27 15:43:37 CST 2019]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@7bb38644
2019-03-27 15:43:43.838  INFO 31765 --- [       Thread-9] o.s.c.n.e.s.EurekaServiceRegistry        : Unregistering application gateway-oauth2-service with eureka with status DOWN
2019-03-27 15:43:43.838  WARN 31765 --- [       Thread-9] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1553672623838, current=DOWN, previous=STARTING]
2019-03-27 15:43:43.839  INFO 31765 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_GATEWAY-OAUTH2-SERVICE/localhost:gateway-oauth2-service:-1: registering service...
2019-03-27 15:43:43.841  INFO 31765 --- [       Thread-9] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 2147483647
2019-03-27 15:43:43.842  INFO 31765 --- [       Thread-9] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 0
2019-03-27 15:43:43.843  INFO 31765 --- [       Thread-9] o.s.c.n.zuul.ZuulFilterInitializer       : Stopping filter initializer
2019-03-27 15:43:43.846  INFO 31765 --- [       Thread-9] com.netflix.discovery.DiscoveryClient    : Shutting down DiscoveryClient ...
2019-03-27 15:43:43.847  INFO 31765 --- [       Thread-9] com.netflix.discovery.DiscoveryClient    : Unregistering ...
2019-03-27 15:43:43.851  INFO 31765 --- [       Thread-9] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_GATEWAY-OAUTH2-SERVICE/localhost:gateway-oauth2-service:-1 - deregister  status: 200
2019-03-27 15:43:43.855  INFO 31765 --- [       Thread-9] com.netflix.discovery.DiscoveryClient    : Completed shut down of DiscoveryClient
2019-03-27 15:43:43.864  WARN 31765 --- [       Thread-9] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2019-03-27 15:43:43.864  INFO 31765 --- [       Thread-9] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.

> Task :check
> Task :build

BUILD SUCCESSFUL in 10s
7 actionable tasks: 7 executed
15:43:44: Task execution finished 'build'.

输出文件uuGatewayOAuth2-1.0.0.jar的大小仅为125KB。

并使用命令运行:

java -jar uuGatewayOAuth2-1.0.0.jar

然后在项目启动时抛出错误:

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
    at com.yd.spring.gatewayoauth2.Application.main(Application.java:17)
    ... 8 more
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:94)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 9 more

为什么?我该如何解决?

1 个答案:

答案 0 :(得分:0)

您正在混合版本。对于格林威治发布系列,您需要使用Boot2.1.x。您使用的implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.0'依赖关系很可能是使用Boot 2.0。因此,一般来说,您需要照顾的版本不匹配。

您可以在网站https://spring.io/projects/spring-cloud#overview的“发行火车”部分下查看发行火车和启动版本兼容性的表。