sva*_*605 16 java gradle spring-boot
我正在尝试在Spring Boot + Gradle项目中构建一个可执行jar,但现在没有任何作用.这是最简单的结构.可能,Gradle配置中缺少某些东西.
摇篮:
buildscript {
ext {
springBootVersion = '1.5.8.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
jar {
manifest {
attributes 'Main-Class': 'com.example.demo.DemoApplication'
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
}
Run Code Online (Sandbox Code Playgroud)
主配置文件:
@RestController
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping(value = "/")
public String index() {
return "index";
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行像java -jar 1.jar这样的jar文件时,我遇到了这个异常:
[main] ERROR org.springframework.boot.SpringApplication - Applicati
on startup failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to proces
s import candidates for configuration class [com.example.demo.DemoApplication];
nested exception is java.lang.IllegalArgumentException: No auto configuration cl
asses found in META-INF/spring.factories. If you are using a custom packaging, m
ake sure that file is correct.
at org.springframework.context.annotation.ConfigurationClassParser.proce
ssDeferredImportSelectors(ConfigurationClassParser.java:556)
at org.springframework.context.annotation.ConfigurationClassParser.parse
(ConfigurationClassParser.java:185)
at org.springframework.context.annotation.ConfigurationClassPostProcesso
r.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:308)
at org.springframework.context.annotation.ConfigurationClassPostProcesso
r.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:228)
at org.springframework.context.support.PostProcessorRegistrationDelegate
.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.ja
va:272)
at org.springframework.context.support.PostProcessorRegistrationDelegate
.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:92)
at org.springframework.context.support.AbstractApplicationContext.invoke
BeanFactoryPostProcessors(AbstractApplicationContext.java:687)
at org.springframework.context.support.AbstractApplicationContext.refres
h(AbstractApplicationContext.java:525)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationConte
xt.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.
java:693)
at org.springframework.boot.SpringApplication.refreshContext(SpringAppli
cation.java:360)
at org.springframework.boot.SpringApplication.run(SpringApplication.java
:303)
at org.springframework.boot.SpringApplication.run(SpringApplication.java
:1118)
at org.springframework.boot.SpringApplication.run(SpringApplication.java
:1107)
at com.example.demo.DemoApplication.main(DemoApplication.java:13)
Caused by: java.lang.IllegalArgumentException: No auto configuration classes fou
nd in META-INF/spring.factories. If you are using a custom packaging, make sure
that file is correct.
at org.springframework.util.Assert.notEmpty(Assert.java:277)
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelecto
r.getCandidateConfigurations(AutoConfigurationImportSelector.java:153)
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelecto
r.selectImports(AutoConfigurationImportSelector.java:95)
at org.springframework.context.annotation.ConfigurationClassParser.proce
ssDeferredImportSelectors(ConfigurationClassParser.java:547)
... 14 common frames omitted
Run Code Online (Sandbox Code Playgroud)
可能有什么问题?
小智 13
我创建了一个包含您提供的所有资源的项目.从终端运行"gradle build",切换到/ build/libs然后运行"java -jar artifactname"就可以了.
你试过清理和重新编译吗?您使用的是哪个版本的Gradle?
Raj*_*esh 12
在Boot 2.x中,bootJar和bootWar任务负责打包应用程序。
bootJar任务负责创建可执行jar文件。一旦应用了Java插件,它将自动创建。
如果未生成可执行jar / war文件,请手动运行以下gradle任务。
$./gradlew bootJar
Run Code Online (Sandbox Code Playgroud)
同样,bootWar会生成可执行的war文件,并在应用war插件后立即创建它。
我们可以使用以下命令执行bootWar任务:
$./gradlew bootWar
Run Code Online (Sandbox Code Playgroud)
请注意,对于Spring Boot 2.x,我们需要使用Gradle 4.0或更高版本。
我最近刚刚尝试使用 2.1.4.Release 和 Gradle 构建的 Spring boot 应用程序。
我从 Windows CMD 中的目录运行以下命令。
gradlew clean build
Run Code Online (Sandbox Code Playgroud)
(系统中需要安装JDK8),我能够看到生成的JAR,
<project-directory>/build/libs/<project-name-version.jar>
Run Code Online (Sandbox Code Playgroud)
希望这对较旧的问题有所帮助。
参考:
小智 5
在春季启动中,您可以通过以下方式直接创建可执行的jar文件:
springBoot {
executable = true
}
Run Code Online (Sandbox Code Playgroud)
请试试
jar{
baseName = 'myapp'
version = 'version'
}
Run Code Online (Sandbox Code Playgroud)
它将从命令行创建名为myapp-version.jar Do ./myapp-version.jar的jar。它将执行
请参阅以下链接以获取更多信息。https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html
| 归档时间: |
|
| 查看次数: |
28767 次 |
| 最近记录: |