gradle - spring boot项目

gradle - spring boot 项目

基于gradle 7.6

项目依赖

└─demo
    │  settings.gradle
    │  build.gradle
    │  version.gradle  //版本控制
    │  publish.gradle  //发布
    ├─demo-svc       //子项目
    │  │  .gitignore
    │  │  build.gradle
    ├─demo-web      //子项目
    │  │  .gitignore
    │  │  build.gradle
    └─gradle
        └─wrapper

项目目录

└─demo
    │  .gitignore
    │  build.gradle
    │  gradlew
    │  gradlew.bat
    │  HELP.md
    │  settings.gradle
    │  version.gradle
    ├─demo-svc
    │  │  .gitignore
    │  │  build.gradle
    │  │
    │  └─src
    │      ├─main
    │      │  ├─java
    │      │  │  └─com
    │      │  │      └─example
    │      │  │          └─demosvc
    │      │  │              ├─dal
    │      │  │              │  ├─domain
    │      │  │              │  │      AccountModel.java
    │      │  │              │  ├─mapper
    │      │  │              │  │      AccountModelMapper.java
    │      │  │              │  └─service
    │      │  │              │      │  AccountModelService.java
    │      │  │              │      └─impl
    │      │  │              │              AccountModelServiceImpl.java
    │      │  │              └─manager
    │      │  └─resources
    │      │      └─mapper
    │      │              AccountModelMapper.xml
    │      └─test
    │          └─java
    │              └─com
    │                  └─example
    │                      └─demosvc
    │                              DemoSvcApplicationTests.java
    │
    ├─demo-web
    │  │  .gitignore
    │  │  build.gradle
    │  │
    │  └─src
    │      ├─main
    │      │  ├─java
    │      │  │  └─com
    │      │  │      └─example
    │      │  │          └─web
    │      │  │              │  DemoWebApplication.java
    │      │  │              └─controller
    │      │  │                      AccountController.java
    │      │  ├─profiles
    │      │  │  ├─dev
    │      │  │  │      cfg.properties
    │      │  │  ├─test
    │      │  │  │      cfg.properties
    │      │  │  └─uat
    │      │  │          cfg.properties
    │      │  └─resources
    │      │      │  application.properties
    │      │      ├─static
    │      │      └─templates
    │      └─test
    │          └─java
    │              └─com
    │                  └─example
    │                      └─web
    │                              DemoWebApplicationTests.java
    └─gradle
        └─wrapper
                gradle-wrapper.jar
                gradle-wrapper.properties

demo 目录

setting.gradle

rootProject.name = 'demo'

include 'demo-web'
include 'demo-svc'

version.gradle

版本管理, 用双引号

// 依赖版本管理
ext {
    version = [
            "mysqlConnectorJVersion"          : "8.0.31",
            "mybatisPlusBootStarterVersion"   : "3.4.3",
            "mybatisSpringBootStarterVersion": "3.0.0"
    ]

    dependencies = [
            "mysql"                   : "com.mysql:mysql-connector-j:${version.mysqlConnectorJVersion}",
            "mybatisPlusBootStarter"  : "com.baomidou:mybatis-plus-boot-starter:${version.mybatisPlusBootStarterVersion}",
            "mybatisSpringBootStarter": "org.mybatis.spring.boot:mybatis-spring-boot-starter:${version.mybatisSpringBootStarterVersion}"
    ]
}

build.gradle

buildscript {
    //定义扩展属性(可选)
    ext {
        springBootVersion = "2.4.5"
    }
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        //管理boot-版本
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}


//引入版本
apply from: 'version.gradle'


allprojects {
    group = 'com.example'
    version = '0.0.1-SNAPSHOT'
    apply plugin: 'idea'
}

subprojects {
    apply plugin: 'java'
    apply plugin: 'java-library'  // 使用 api 方式引用工程-间接传递
    apply plugin: 'maven-publish' // install jar to 本地库
    apply plugin: 'signing'
    apply plugin: 'org.springframework.boot'//使用springboot插件
    apply plugin: 'io.spring.dependency-management'//版本管理插件
//    println  "Root project:   ${project.rootProject}";
//    println  "  rootDir:      ${project.rootDir}"
//    println  "  projectDir:   ${project.projectDir}";
//    println  "  project dir:  ${System.getProperty("user.dir")}";
    //jdk版本
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    // Java 源码和文档的编码
    [compileJava, compileTestJava, javadoc]*.options*.encoding = 'UTF-8'

    tasks.withType(JavaCompile) {
        options.encoding = 'UTF-8'
    }
/*
    //指定 wrapper, 约定相同 gradle版本,在 gradle下 gradle-wrapper.properties 约定
    task wrapper(type: Wrapper) {
        gradleVersion = '7.6'
    }
*/


    repositories {
        //maven 本地仓库,必须配置M2_HOME=maven安装路径
        //user_home->M2_HOME/conf/setting.xml 下指定仓库
        mavenLocal()
        mavenCentral()
        maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
    }

    //=========================依赖管理=======================//
    dependencyManagement {
        dependencies {
            for (depJar in rootProject.ext.dependencies) {
                dependency depJar.value
            }
        }
    }

    dependencies {
        compileOnly 'org.projectlombok:lombok'
        annotationProcessor 'org.projectlombok:lombok'
    }

    //===================================单元测试=====================
    test {
        useJUnitPlatform()
    }

    //指定 gradle 脚本
    tasks.register("prepareKotlinBuildScriptModel") {}

    //==================================打包方式=====================
    bootJar {
        enabled = false
    }
    jar {
        enabled = true
    }
}

//子项目统一依赖管理
project(':demo-web') {

    group = 'com.example'
    version = '0.0.1-SNAPSHOT'
    // publish.gradle 要在 版本之后

    apply from: "${project.rootDir}/publish.gradle"

    //使用 api 引入项目
    dependencies {
        compileOnly 'org.projectlombok:lombok'
        annotationProcessor 'org.projectlombok:lombok'
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
        //junit5
        testImplementation 'org.junit.jupiter:junit-jupiter-api'
        testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
        api project(':demo-svc')
    }
}

project(':demo-svc') {

    group = 'com.example'
    version = '0.0.1'
    // gradle :demo-svc:publishMavenJavaPublicationToMavenRepository
    apply from: "${project.rootDir}/publish.gradle"
    //安装到本地库插件
    dependencies {
        api 'org.springframework.boot:spring-boot-starter-web'
        api 'com.baomidou:mybatis-plus-boot-starter:'
        api 'org.mybatis.spring.boot:mybatis-spring-boot-starter'
        compileOnly 'org.projectlombok:lombok'
        api 'com.mysql:mysql-connector-j'
        annotationProcessor 'org.projectlombok:lombok'
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
    }

}

demo-svc

service 实现,dao…

build.gradle

jar {
    enabled = true
    baseName('demo-svc')
}

demo-web

build.gradle, 运行时传入 -Denv=dev 可以根据 环境运行

def env = project.hasProperty('env') ? project.env : 'dev'
//将其他目录资源copy到 src/main/resources 目录下
sourceSets{
    main{
        resources{
            srcDirs = ["src/main/resources","src/main/profiles/${env}"]
        }
    }
}

bootJar {
    enabled = true
    baseName('demo-web')
}

publish.gradle 发布


//=============================打包发布=====================
//打包
task sourceJar(type: Jar) {
    from sourceSets.main.allJava
}

publish.dependsOn(build)
/**
 * 1.publishToMavenLocal: 发布到 user_home/.m2/setting.xml 指定的本地仓库
 *
 * 2.publishMavenPublicationToMavenLocal : 发布到远程指定仓库
 *
 */
publishing {
    publications {
        mavenJava(MavenPublication) {

            //指定group/artifact/version信息,可以不填。默认使用项目group/name/version作为groupId/artifactId/version
            groupId project.group
            artifactId project.name
            version project.version

            //如果是war包填写components.web,如果是jar包填写components.java
            from components.java
            versionMapping {
                usage('java-api') {
                    fromResolutionOf('runtimeClasspath')
                }
                usage('java-runtime') {
                    fromResolutionResult()
                }
            }
            //配置上传源码
            artifact sourceJar {
                classifier "sources"
            }
        }
    }

    //私服
    repositories {
        //publishMavenPublicationToMavenLocal
        maven {
            //允许非 https
            allowInsecureProtocol = true
            def releasesRepoUrl = "http://192.xxx.x:8081/repository/maven-releases/"
            def snapshotsRepoUrl = "http://192.xxx.x:8081/repository/maven-snapshots/"
            //指定要上传的maven私服仓库
            url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
            println "${project.name}-${version} deploy to -${url}"
            //认证用户和密码
            credentials {
                username 'admin'
                password 'admin123'
            }
        }
    }
}

你可能感兴趣的:(spring,boot,spring,boot,gradle)