【问题标题】:Executable Jar created by Gradle bootRepackage missing jarsGradle bootRepackage 创建的可执行 Jar 缺少 jar
【发布时间】:2018-07-24 02:21:04
【问题描述】:

我想做的事:

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

目前的问题:

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

代码:

src/main/java/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

使用的命令:

在工作区/项目/构建/库中

java -jar 项目.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

在这方面我是个新手。有人对如何解决我的问题有任何想法吗?

【问题讨论】:

    标签: eclipse spring-boot gradle


    【解决方案1】:

    我想我找到了解决方案。

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

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

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

    【讨论】:

      猜你喜欢
      • 2017-01-20
      • 2014-04-18
      • 1970-01-01
      • 2014-10-31
      • 1970-01-01
      • 2019-10-06
      • 2021-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多