2017-04-12 98 views
1

使用Intellij啓動我的應用程序時,一切正常。但是,當我做了fatJar(與gradle這個插件:eu.appsatori.fatjar)和執行:Gradle使用fatJar插件和SpringBoot應用程序構建「應用程序啓動失敗」

java -jar myapp.jar 

我得到這樣的:

11:41:01.224 [main] ERROR org.springframework.boot.SpringApplication - Application startup failed 
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [my.testing.MyAppMain]; nested exception is java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct. 
     at org.springframework.context.annotation.ConfigurationClassParser.processDeferredImportSelectors(ConfigurationClassParser.java:482) 
     at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:184) 
... 

看起來它並沒有發現自動配置類中META-INF/spring.factories

如何添加此文件?它的內容應該是什麼?

我有以下構建腳本:

apply plugin: "java"; 
apply plugin: "idea"; 
apply plugin: 'application' 
apply plugin: 'eu.appsatori.fatjar' 
apply plugin: 'org.springframework.boot' 

repositories { 
    mavenCentral() 
} 

buildscript { 
    ext { 
     springBootVersion = '1.4.3.RELEASE' 
    } 

    repositories { 
     mavenCentral() 
     jcenter() 
    } 

    dependencies { 
     classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}" 
     classpath "eu.appsatori:gradle-fatjar-plugin:0.3" 
    } 
} 

sourceSets { 
    main { 
     java { 
      srcDir 'src/main/java' 
     } 

     resources { 
      srcDir 'src/main/resources' 
     } 
    } 

    test { 
     java { 
      srcDir 'src/test/java' 
     } 
    } 
} 

fatJar { 
    manifest { 
     attributes("Main-Class": 'my.testing.MyAppMain') 
    } 

    exclude 'META-INF/*.DSA' 
    exclude 'META-INF/*.SF' 
    exclude 'META-INF/*.RSA' 
} 

dependencies { 
    compile 'org.springframework.boot:spring-boot-starter-jdbc' 
    runtime 'mysql:mysql-connector-java' 

    testCompile 'org.springframework.boot:spring-boot-starter-test' 

} 

我的示例代碼:

package my.testing; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.context.ConfigurableApplicationContext; 
import org.springframework.jdbc.core.JdbcTemplate; 
import org.springframework.stereotype.Component; 

@SpringBootApplication 
public class MyAppMain { 
    private ConfigurableApplicationContext springContext; 

    @Autowired 
    private SimpleDao dao; 

    public static void main(String[] args) throws Exception { 
     MyAppMain test = new MyAppMain(); 
     try { 
      test.init(); 
      test.doWhatYouGotToDo(); 
     } finally { 
      test.stop(); 
     } 
    } 

    private void doWhatYouGotToDo() { 
     System.out.println("Auto-wired dao: " + dao.hashCode()); 
     System.out.println("Auto-wired jdbcTemplate: " + dao.jdbcTemplate.hashCode()); 
    } 

    private void init() throws Exception { 
     springContext = SpringApplication.run(MyAppMain.class); 
     springContext.getAutowireCapableBeanFactory().autowireBean(this); 
    } 

    private void stop() throws Exception { 
     springContext.close(); 
    } 
} 

@Component 
class SimpleDao { 

    @Autowired 
    JdbcTemplate jdbcTemplate; 
} 

application.properties文件:

spring.datasource.driver-class-name = com.mysql.jdbc.Driver 
spring.datasource.url = jdbc:mysql://localhost:3306/some_db?useSSL=false&serverTimezone=UTC 
spring.datasource.username = some_user 
spring.datasource.password = some_pass 

注:這個問題是基於在SpringBoot - making jar files - No auto configuration classes found in META-INF/spring.factories 哪裏答案都指Maven。請在這裏只填寫與Gradle相關的答案。

+1

不要使用'fatjar'插件......春天啓動已經產生一個胖的罐子...你現在有兩個競爭的機制...... –

+0

那麼我怎樣才能建立一個沒有這個插件的胖罐子?即如何在IntelliJ之外成功運行以上應用程序? – alwi

+2

好的。 Gradle'bootRepackage'任務執行此操作。謝謝!你可以形成一個答案,所以我可以接受它... – alwi

回答

2
gradle clean build 
gradle bootRepackage 

結果:

enter image description here

這裏是我的build.gradle文件:

enter image description here

相關問題