1. 程式人生 > >Spring boot + Gradle + Eclipse打war包發布總結

Spring boot + Gradle + Eclipse打war包發布總結

重寫 並且 .html jdb log4j2 jce title file apach

首先感謝兩位博主的分享

http://lib.csdn.net/article/git/55444?knId=767

https://my.oschina.net/alexnine/blog/540651


buildscript {
    ext {
        springBootVersion = ‘1.5.2.RELEASE‘
    }
    repositories {
        maven { url ‘http://maven.aliyun.com/nexus/content/groups/public/‘ }
        jcenter()
    }
    dependencies {
        classpath(
"org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } group ‘com.gzkit‘ version ‘1.0.1-SNAPSHOT‘ apply plugin: ‘java‘ apply plugin: ‘eclipse‘
apply plugin: ‘eclipse-wtp‘ apply plugin: ‘org.springframework.boot‘ apply plugin: ‘war‘ compileJava { options.encoding = ‘UTF-8‘ options.compilerArgs
<< "-Xlint:unchecked" << "-Xlint:deprecation" } sourceCompatibility = 1.7 targetCompatibility = 1.7 [javadoc, compileTestJava]*.options*.encoding = ‘UTF-8‘ repositories { mavenLocal() maven { url ‘http://maven.aliyun.com/nexus/content/groups/public/‘ } jcenter() } configurations { provided
//compile.exclude module: "spring-boot-starter-tomcat" //compile.exclude group: ‘org.apache.tomcat‘ //compile.exclude group: ‘org.apache.tomcat.embed‘ all*.exclude module: ‘spring-boot-starter-logging‘ } ext { shiroVersion = ‘1.3.2‘ } dependencies { provided(‘org.springframework.boot:spring-boot-starter-web‘){ exclude module: "spring-boot-starter-tomcat" } compile(‘org.springframework.boot:spring-boot-starter-freemarker‘) compile(‘org.springframework.boot:spring-boot-starter-undertow‘) compile(‘org.springframework.boot:spring-boot-starter-log4j2‘) compile(‘org.springframework.boot:spring-boot-starter-mail‘) //compile(‘org.springframework.boot:spring-boot-starter-data-redis‘) // spring session //compile(‘org.springframework.session:spring-session‘) // apache shiro compile("org.apache.shiro:shiro-core:$shiroVersion") compile("org.apache.shiro:shiro-web:$shiroVersion") compile("org.apache.shiro:shiro-spring:$shiroVersion") compile("org.apache.shiro:shiro-ehcache:$shiroVersion") // mybatis support compile(‘org.mybatis.spring.boot:mybatis-spring-boot-starter:1.1.1‘) compile(‘tk.mybatis:mapper-spring-boot-starter:1.1.1‘) // apache commons compile(‘org.apache.commons:commons-lang3:3.4‘) compile(‘org.apache.commons:commons-collections4:4.1‘) // apache poi (excel/word) compile(‘org.apache.poi:poi:3.14‘) compile(‘org.apache.poi:poi-ooxml:3.14‘) // alibaba datasource runtime(‘com.alibaba:druid:1.0.27‘) // mysql jdbc driver runtime(‘mysql:mysql-connector-java‘) // log4j2 needs disruptor to enable async logger runtime(‘com.lmax:disruptor:3.3.5‘) //compile files(‘libs/common-uams-2.2.4.jar‘) testCompile(‘org.springframework.boot:spring-boot-starter-test‘) }

兩個地方要註意

第一是apply plugin: ‘eclipse-wtp‘,用來生成Eclipseweb項目的插件(web-tool-platform)

  如果不加,在server欄裏面的Tomcat上右鍵,選擇Add and Remove…中沒有可發布的項目,加上運行後去掉也可以,不知道為啥,應該是已經生成Eclipseweb項目的插件(web-tool-platform)

第二是

  技術分享

  技術分享

主要是添加war包的支持,其次的話就是在spring-boot-starter-tomcat處改為provided。provided和compile的區別在與前者是在調試使用時會加載對應的包,但是 在打包時不會講對應的包加入到war包的lib中而後者則是兩種情況都要調用對應的包。

最後是要修改啟動Spring的類

  

public class MonolithicPlatformApplication extends SpringBootServletInitializer{

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // TODO Auto-generated method stub
        return builder.sources(MonolithicPlatformApplication.class);
    }

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

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                // ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
                ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/static/404.html");
                ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/static/500.html");
                container.addErrorPages(error404Page, error500Page);
            }
        };
    }
}

這裏繼承SpringBootServletInitializer 並重寫其中的configure方法目的是使用Spring框架的Servlet3.0支持。並且允許我們可以配置項目從serclet容器中啟動。

修改完成之後就可以啟動了

另外配上application.yml的配置

server:
    undertow:
        accesslog:
            enabled: true
            dir: target/log
            pattern: combined
    compression:
        enabled: true
        min-response-size: 1
    port: 8080
    session:
        timeout: 1800 # in seconds

spring:
    datasource:
        url: jdbc:mysql://121.201.97.113:3306/km_lis_new?characterEncoding=UTF-8&useSSL=false
        username: root
        password: Zqit3503
        type: com.alibaba.druid.pool.DruidDataSource # 使用druid數據源
        driver-class-name: com.mysql.jdbc.Driver
        maxActive: 2
        initialSize: 1
    redis:
        host: 192.168.1.104
        #password: redispassword
        port: 6379
        pool:
          max-idle: 100
          min-idle: 1
          max-active: 1000
          max-wait: -1
#        database: 0 # database index used by this connection
#        port: 6379
#        host: 192.168.1.104
#        pool:
#            max-active: 30
#            min-idle: 0
#            max-wait: 1500 # milliseconds
#            max-idle: 20
#        timeout: 2000 # connection timeout in milliseconds
    freemarker:
        check-template-location: true
        content-type: text/html
        expose-request-attributes: true
        expose-session-attributes: true
        request-context-attribute: request
        template-loader-path=classpath: /templates/
        settings:
            locale: zh_CN
            template_update_delay: 0
            tag_syntax: auto_detect
            default_encoding: UTF-8
            output_encoding: UTF-8
            url_escaping_charset: UTF-8
            date_format: yyyy-MM-dd
            time_format: HH:mm:ss
            datetime_format: yyyy-MM-dd HH:mm:ss
            number_format: \#.##
            classic_compatible: true
            template_exception_handler: rethrow # ignore, debug, html_debug, rethrow
            whitespace_stripping: true
        expose-spring-macro-helpers: true
        suffix: .ftl
        charset: UTF-8
        cache: false
    mvc:
        static-path-pattern: /static/**
    messages:
        basename: i18n/ui_messages
    mail:
        host: smtp.qq.com
        username: [email protected]
        password: mjevcothmfqybbib #使用QQ郵箱SMTP服務需要生成授權碼,而非QQ密碼
        sendTo: [email protected]
        disasterTitle: 手動切換容災模式
        disasterContent: 會話共享項目-發送郵件功能-手動切換容災模式~~~~
        normalTitle: 手動切換正常模式
        normalContent: 會話共享項目-發送郵件功能-手動切換正常模式~~~~
        autoTitle: 自動切換運行模式
        autoContent: 會話共享項目-發送郵件功能-自動切換運行模式~~~~
        properties:
            mail:
                smtp:
                    auth: true
                    starttls:
                        enable: true
                        required: true

mybatis:
    config-location: classpath:mybatis-config.xml
    mapper-locations: classpath:mapper/**/*Mapper.xml
    type-aliases-package: com.kmlis.entity

Spring boot + Gradle + Eclipse打war包發布總結