由Gradle bootRepackage创建的可执行Jar缺少jar

时间:2018-02-13 16:35:49

标签: eclipse spring-boot gradle

我想做什么:

使用gradle创建可执行Jar,并将eclipse作为IDE创建。 Jar应该包含运行所需的所有库,并且可以在docker容器中使用。

到目前为止

问题:

org.springframework.boot未包含在Jar中。

代码:

的src /主/爪哇/ SampleController.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

    @Controller    
    @EnableAutoConfiguration
    public class SampleController {

    @RequestMapping("/")
    @ResponseBody
        String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleController.class, args);
    }
}

的build.gradle

buildscript {
    repositories {
        jcenter()
        mavenCentral()  
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.10.RELEASE")
    }
}

apply plugin: 'java-library'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
    mavenCentral()
}

dependencies {
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation 'com.google.guava:guava:21.0'

    api 'org.springframework.boot:spring-boot-starter-web'
    api 'org.springframework.boot:spring-boot-starter'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'
}

springBoot {
  mainClass = "SampleController"
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

使用的命令:

在workspace / project / build / libs

java -jar project.jar

错误:

java : Exception in thread "main" java.lang.reflect.InvocationTargetException
At line:1 char:1
+ java -jar RSSFeedAggregator.jar
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Exception in th...TargetException:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

    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 SampleController.main(SampleController.java:18)
    ... 8 more
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    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)

猜猜我找到了解决办法。

根据我的理解,当直接在eclipse中运行时,gradle api / implementation会在运行时自动添加jar。 但是当使用:bootRepackage创建fat jar时,需要显式添加运行时库。 (该程序直接在eclipse上运行良好)

runtime 'org.springframework.boot:spring-boot-starter'
runtime 'org.springframework.boot:spring-boot-starter-web'

这解决了我的问题。 如果有人在这个问题上有什么要补充的话,我不会关闭这个话题。