Gradle構建SpringBoot專案

JS-無言以對發表於2020-05-26

1、新建專案

在這裡插入圖片描述
因為預設是勾選Java選項的,為了構建一個適合多模組的專案,去掉勾選,如上圖。
在這裡插入圖片描述
接下來右鍵單擊專案名建立一個Model,此時一個純淨的gradle專案結構便已經完成,如果是多模組專案,那麼便可以建立多個Model,接下來便是配置yml檔案。

1.1、首先配置最外層的build.gradle檔案。個人整合了一部分常用的jar包。

buildscript {
    // 定義變數
    ext {
        springBootVersion = '2.2.2.RELEASE'
        ALI_REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public'
    }
    // 倉庫地址
    repositories {
        // 先從阿里雲maven倉庫中去下載,沒有再去maven中央倉庫
        maven {
            url ALI_REPOSITORY_URL
        }
        mavenCentral()

    }
    // 構建時所需要的外掛或者是其他的依賴
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }

}
// 所有模組的總倉庫地址
allprojects {
    // 在build.gradle檔案中直接宣告的依賴項、倉庫地址等資訊是專案自身需要的資源。
    repositories {
        maven {
            url ALI_REPOSITORY_URL
        }
    }
}
subprojects {
    apply plugin: 'java'
    apply plugin: 'war';
    apply plugin: 'idea'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'org.springframework.boot'
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    // 指定編碼格式
    [compileJava,compileTestJava,javadoc]*.options*.encoding = 'UTF-8'
    version = "1.0.0-SNAPSHOT"  // 開發版本: -SNAPSHOT 正式版本:.RELEASE
//    group = "com.js"
    description = "spring boot in action"
    dependencies {
        //mysql驅動
        compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.11'
        //阿里資料庫連線池
        compile group: 'com.alibaba', name: 'druid', version: '1.1.0'
        //SpringBootWeb模組
        compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.2.2.RELEASE'
        //springboot的aop切面驅動
        compile group: 'org.springframework.boot', name: 'spring-boot-starter-aop', version: '2.2.2.RELEASE'
        //郵箱服務
        compile group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: '2.2.2.RELEASE'
        // https://mvnrepository.com/artifact/org.springframework.amqp/spring-rabbit
        compile group: 'org.springframework.amqp', name: 'spring-rabbit', version: '2.2.2.RELEASE'
        //redis驅動
        compile 'org.springframework.boot:spring-boot-starter-data-redis:2.2.2.RELEASE'
        //mybatis需要的
        compile group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter', version: '1.3.2'
        
        //lombok的jar包
        compileOnly 'org.projectlombok:lombok:1.18.10'
        annotationProcessor 'org.projectlombok:lombok:1.18.10'
        testCompileOnly 'org.projectlombok:lombok:1.18.10'
        testAnnotationProcessor 'org.projectlombok:lombok:1.18.10'

        compile 'org.hibernate:hibernate-validator:5.3.6.Final'
        compile 'com.fasterxml.jackson.core:jackson-databind:2.9.7'
        compile group: 'org.apache.commons', name: 'commons-pool2', version: '2.4.1'

        //swagger驅動
        compile 'io.springfox:springfox-swagger-ui:2.9.2'
        compile 'io.springfox:springfox-swagger2:2.9.2'
        compile 'io.github.swagger2markup:swagger2markup:1.3.1'
        compile 'joda-time:joda-time:2.9.4'
        //Mybatis分頁外掛
        compile group: 'com.github.pagehelper', name: 'pagehelper-spring-boot-starter', version: '1.2.12'

        // https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient
        compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.12'

        // https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
        compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1'
        // https://mvnrepository.com/artifact/commons-io/commons-io
        compile group: 'commons-io', name: 'commons-io', version: '2.4'
        // https://mvnrepository.com/artifact/commons-net/commons-net
        compile group: 'commons-net', name: 'commons-net', version: '3.6'

        compile 'com.github.pagehelper:pagehelper:5.1.4'
        //token外掛
//        compile 'com.auth0:java-jwt:3.2.0'
        compile group: 'com.auth0', name: 'java-jwt', version: '3.0.0'
        compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'
    }
}

1.2、日誌列印相關採用的lombok元件,配置日誌的相關檔案logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration  scan="true" scanPeriod="10 seconds">
    <contextName>logback</contextName>
    <!-- 格式化輸出:%date表示日期,%thread表示執行緒名,%-5level:級別從左顯示5個字元寬度 %msg:日誌訊息,%n是換行符-->
    <property name="LOG_PATTERN" value="%date{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" />
    <!-- 定義日誌儲存的路徑,不要配置相對路徑 E:\file\log %i宣告每天產生的日誌檔案字尾-->
    <property name="FILE_PATH" value="D:/logs/%d{yyyy.MM.dd}/%d{yyyy.MM.dd}.%i.log" />

    <!-- 定義日誌儲存的路徑,不要配置相對路徑 E:\file\log %i宣告每天產生的日誌檔案字尾-->
    <property name="FILE_ERROR_PATH" value="D:/logs/%d{yyyy.MM.dd}/error/%d{yyyy.MM.dd}.%i.error.log" />
    <!-- 控制檯輸出日誌 -->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <!-- 日誌級別過濾INFO以下 -->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
        <encoder>
            <!-- 按照上面配置的LOG_PATTERN來列印日誌 -->
            <pattern>${LOG_PATTERN}</pattern>
        </encoder>
    </appender>

    <!--只儲存error日誌-->
    <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${FILE_ERROR_PATH}</fileNamePattern>
             <!--keep 15 days' worth of history-->
            <maxHistory>30</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <!-- 日誌檔案的最大大小 -->
                <maxFileSize>100MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <!-- 超出刪除老檔案 -->
            <totalSizeCap>1GB</totalSizeCap>
        </rollingPolicy>

        <encoder>
            <pattern>${LOG_PATTERN}</pattern>
        </encoder>
    </appender>


    <!--每天生成日誌檔案,儲存30天的日誌檔案。rollingFile用來切分檔案的 -->
    <appender name="rollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${FILE_PATH}</fileNamePattern>
            <!-- keep 15 days' worth of history -->
            <maxHistory>30</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <!-- 日誌檔案的最大大小 -->
                <maxFileSize>100MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <!-- 超出刪除老檔案 -->
            <totalSizeCap>1GB</totalSizeCap>
        </rollingPolicy>

        <encoder>
            <pattern>${LOG_PATTERN}</pattern>
        </encoder>
    </appender>


    <!-- project default level -->
    <logger name="net.sh.rgface.serive" level="ERROR" />

    <!-- 日誌輸出級別 -->
    <root level="INFO">
        <appender-ref ref="console" />
        <appender-ref ref="rollingFile" />
        <appender-ref ref="fileErrorLog"/>
    </root>
</configuration>

1.3、配置yml檔案

server:
  port: 8080
  tomcat:
    # 該執行緒池可以容納的最大執行緒數。預設值:200;
    maxThreads: 300
    # 最大連線執行緒數,即:併發處理的最大請求數,預設值為 75
    maxConnections: 3000
    # 允許的最大連線數,應大於等於 maxProcessors ,預設值為 100
    acceptCount: 1000
    # 編碼方式
    uri-encoding: UTF-8

spring:
  jackson:
    time-zone: GMT+8
    date-format: yyyy-MM-dd HH:mm:ss
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
#    這裡採用的QQ郵箱測試
  mail:
    host: smtp.qq.com
    username: 郵箱
    password: grqpjixakusedicc
    default-encoding: utf-8
application:
  base-package: 基礎包名我通常是啟動類所在的包

mybatis:
  typeAliasesPackage: ${application.base-package}.pojo, ${application.base-package}.vo, ${application.base-package}.dto
  mapper-locations: classpath:mapper/*/*.xml,classpath:mapper/*.xml
  configuration:
    # 一定要設定,避免個別SQL執行時間過長,把系統搞跨
    default-statement-timeout: 5
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    cache-enabled: false

#pagehelper分頁外掛配置
pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: count=countSq

logging:
  config: classpath:logback-spring.xml

#基本資訊
info:
  description: 平臺
  version: v1.0
  author: 作者

1.4、基本結構圖片
在這裡插入圖片描述

相關文章