简体   繁体   English

使用Gradle和Spring-boot构建可执行jar文件时出现问题

[英]Trouble building executable jar file with Gradle and Spring-boot

I am having trouble executing the built jar file: 我在执行构建的jar文件时遇到问题:

Running the following command java -jar /build/libs/***.jar gives me a bunch of NoClassDefFoundError errors. 运行以下命令java -jar /build/libs/***.jar给了我一堆NoClassDefFoundError错误。

But when I run gradle bootrun it works fine... 但是当我运行gradle bootrun它工作得很好......

I have followed instructions here: http://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-gradle-plugin.html 我按照这里的说明操作: http//docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-gradle-plugin.html

My build.gradle looks like this: 我的build.gradle看起来像这样:

apply plugin: 'spring-boot'

sourceCompatibility = 1.5
version = '1.0'

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.6.RELEASE")
    }
}

springBoot {
    mainClass = "org.gradle.App"
}

dependencies {
    compile files('lib/selenium-server-standalone-2.47.1.jar')
}

Thank you. 谢谢。

It might be a bit difficult to answer without some element missing; 如果没有遗漏某些元素,可能会有点难以回答; if you have some fancy stuff, please highlight or better share your application but basically I was able to make it work with the following : 如果你有一些花哨的东西,请突出或更好地分享你的应用程序,但基本上我能够使它与以下工作:

the build.gradle file was pretty much like yours build.gradle文件非常像你的

apply plugin: 'spring-boot'

sourceCompatibility = 1.5
version = '1.0'

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.6.RELEASE")
    }
}

springBoot {
    mainClass = "com.readinglist.ReadingListApplication"
}

repositories {
    mavenCentral()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile files('lib/selenium-server-standalone-2.47.1.jar')
    testCompile('org.springframework.boot:spring-boot-starter-test') 
}

I do not know if they differ a lot but the doc for your 1.2.6 version is http://docs.spring.io/spring-boot/docs/1.2.6.RELEASE/reference/html/build-tool-plugins-gradle-plugin.html#build-tool-plugins-gradle-dependencies-without-versions and they do mention to add the dependencies. 我不知道它们是否有很大不同,但1.2.6版本的文档是http://docs.spring.io/spring-boot/docs/1.2.6.RELEASE/reference/html/build-tool-plugins -gradle-plugin.html #build-tool-plugins-gradle-dependencies-without-versions他们确实提到添加依赖项。

A note on your script, isn't selenium only necessary for your testing ? 关于你的脚本的注释,只有你的测试才需要硒吗? you could change compile files ..selenium to testCompile files ..selenium (or even testRuntime ) your jar file will thank you for that. 你可以将compile files ..selenium更改为testCompile files ..selenium (甚至testRuntime )你的jar文件会感谢你。

For info I build a pretty basic Spring-Boot app for this testing (based on the example from the Spring Boot In Action) 有关信息,我为此测试构建了一个非常基本的Spring-Boot应用程序(基于Spring Boot In Action中的示例)

package com.readinglist;

import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.openqa.selenium.server.SeleniumServer;

@SpringBootApplication
public class ReadingListApplication {

    private static Logger log = Logger.getLogger(ReadingListApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(ReadingListApplication.class, args);
        log.info("SeleniumServer ClassName is " + SeleniumServer.class.getName());
    }
}

The important piece is to make sure you have a valid main method 重要的是确保您拥有有效的main方法

For reference, my gradle execution is 作为参考,我的gradle执行是

fhenri@machine:~/project/examples/spring-boot/inactionbook/readinglist$ gradle build
:compileJava
warning: [options] bootstrap class path not set in conjunction with -source 1.5
warning: [options] source value 1.5 is obsolete and will be removed in a future release
warning: [options] target value 1.5 is obsolete and will be removed in a future release
warning: [options] To suppress warnings about obsolete options, use -Xlint:-options.
4 warnings
:processResources
:classes
:jar
:findMainClass
:startScripts
:distTar
:distZip
:bootRepackage
:assemble
:compileTestJava
warning: [options] bootstrap class path not set in conjunction with -source 1.5
warning: [options] source value 1.5 is obsolete and will be removed in a future release
warning: [options] target value 1.5 is obsolete and will be removed in a future release
warning: [options] To suppress warnings about obsolete options, use -Xlint:-options.
4 warnings
:processTestResources UP-TO-DATE
:testClasses
:test
:check
:build

BUILD SUCCESSFUL

Total time: 5.411 secs

and finally running your command 最后运行你的命令

fhenri@machine:~/project/examples/spring-boot/inactionbook/readinglist$ java -jar build/libs/***.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.2.6.RELEASE)

2015-10-13 19:18:01.625  INFO 94280 --- [           main] com.readinglist.ReadingListApplication   : Starting ReadingListApplication on macbook-pro-de-frederic.home with PID 94280 (/Users/fhenri/project/examples/spring-boot/inactionbook/readinglist/build/libs/readinglist-1.0.jar started by fhenri in /Users/fhenri/project/examples/spring-boot/inactionbook/readinglist)
2015-10-13 19:18:01.679  INFO 94280 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@71c79795: startup date [Tue Oct 13 19:18:01 CEST 2015]; root of context hierarchy
2015-10-13 19:18:02.377  INFO 94280 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2015-10-13 19:18:03.141  INFO 94280 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2015-10-13 19:18:03.489  INFO 94280 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2015-10-13 19:18:03.491  INFO 94280 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.26
2015-10-13 19:18:04.727  INFO 94280 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2015-10-13 19:18:04.727  INFO 94280 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3053 ms
2015-10-13 19:18:05.379  INFO 94280 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2015-10-13 19:18:05.385  INFO 94280 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2015-10-13 19:18:05.386  INFO 94280 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2015-10-13 19:18:05.692  INFO 94280 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@71c79795: startup date [Tue Oct 13 19:18:01 CEST 2015]; root of context hierarchy
2015-10-13 19:18:05.757  INFO 94280 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2015-10-13 19:18:05.758  INFO 94280 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
2015-10-13 19:18:05.786  INFO 94280 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-10-13 19:18:05.787  INFO 94280 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-10-13 19:18:05.824  INFO 94280 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-10-13 19:18:05.940  INFO 94280 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2015-10-13 19:18:06.047  INFO 94280 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2015-10-13 19:18:06.050  INFO 94280 --- [           main] com.readinglist.ReadingListApplication   : Started ReadingListApplication in 4.751 seconds (JVM running for 5.476)
2015-10-13 19:18:06.052  INFO 94280 --- [           main] com.readinglist.ReadingListApplication   : SeleniumServer ClassName is org.openqa.selenium.server.SeleniumServer

If you have something wrong, make sure to run gradle clean after you made some changes. 如果您有问题,请确保gradle clean一些更改后运行gradle clean

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM