欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Gradle构建SpringBoot项目

程序员文章站 2024-01-11 20:07:16
...

1、新建项目

Gradle构建SpringBoot项目
因为默认是勾选Java选项的,为了构建一个适合多模块的项目,去掉勾选,如上图。
Gradle构建SpringBoot项目
接下来右键单击项目名创建一个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、基本结构图片
Gradle构建SpringBoot项目