【工程】-一文带你使用Gradle构建SpringBoot微服务项目

简介: 【工程】-一文带你使用Gradle构建SpringBoot微服务项目

微信截图_20220612174009.png

Gradle官方介绍是Build Anything, Automate Everything,Deliver Faster,即构建任何项目,自动化,构建快。


可以明显看到Gradle速度快,显示内容也比较清爽

项目实例

IDEA创建初始项目

so easy..

copy 两个项目,目录结构是这样,这里用1-5标识Gradle配置文件

配置文件详解

1. settings.gradle

/**
 * rootProject.name 项目名称
 * include 模块名称
 */
rootProject.name = 'micro-service-framework'
include 'framework-base'
include 'framework-web'
include 'framework-redis'

2. build.gradle

这个相当于maven中的父maven配置

buildscript {
    ext {
        //spring boot 版本
        bootVersion = '2.0.6.RELEASE'
    }
    //私服地址,这个地址适用于gradle自身,比如删除,下面的springboot插件就会找不到
    repositories {
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
    }
    //springboot gradle插件配置
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${bootVersion}")
    }
}
allprojects {
    //导入使用的插件
    apply plugin: 'java'
    apply plugin: 'maven'
    //如果导入该插件,你需要指定main class,否则不能打包
    //apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'
    //这个插件用于发布jar包到私服
    apply plugin: 'maven-publish'
    //jdk编译版本
    sourceCompatibility = 1.8
    //jar包的group ,version配置
    group 'net.178le.micro'
    version '0.0.1-SNAPSHOT'
    //私服地址,
    repositories {
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
    }
    /**
     * 导入了springboot,spring cloud的pom文件,能够免去自己管理版本
     * PS: 在Spring官网指导上面有另外一种配置,那种配置需要配置main class,一会说明
     */
    dependencyManagement {
        imports {
            mavenBom "org.springframework.boot:spring-boot-starter-parent:${bootVersion}"
            mavenBom "org.springframework.cloud:spring-cloud-dependencies:Finchley.SR2"
        }
    }
    //私服发布配置
    publishing {
        publications {
            maven(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
                //配置上传源码
                artifact sourceJar {
                    classifier "src"
                }
            }
        }
        repositories {
            maven {
                def releasesUrl = "http://你的私服ip:8081/repository/maven-releases/"
                def snapshotsUrl = "http://你的私服ip:8081/repository/maven-snapshots/"
                url = version.endsWith('SNAPSHOT') ? snapshotsUrl : releasesUrl
                credentials {
                    username = 'admin'
                    password = 'admin123'
                }
            }
        }
    }
}
//这里的配置对子项目生效
subprojects {
    dependencies {
        testCompile("org.springframework.boot:spring-boot-starter-test")
        compile("com.google.guava:guava:28.0-jre")
    }
}
//打包源码
task sourceJar(type: Jar) {
    from sourceSets.main.allJava
}

maven publish使用

在task -> publishing 中有如下几个命令

我认为使用这两个命令就足够了

publishMavenPublicationToMavenLocal 发布项目到本地仓库

publishMavenPublicationToMavenRepository 发布项目到私服

PS:使用apply plugin: 'org.springframework.boot' build必须要指定main class

23:26:17: Executing task 'build'...
> Task :framework-base:compileJava NO-SOURCE
> Task :framework-base:processResources NO-SOURCE
> Task :framework-base:classes UP-TO-DATE
> Task :framework-base:jar SKIPPED
> Task :framework-redis:compileJava UP-TO-DATE
> Task :framework-redis:processResources NO-SOURCE
> Task :framework-redis:classes UP-TO-DATE
> Task :framework-redis:jar SKIPPED
> Task :framework-web:compileJava UP-TO-DATE
> Task :framework-web:processResources NO-SOURCE
> Task :framework-web:classes UP-TO-DATE
> Task :framework-web:bootJar FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':framework-web:bootJar'.
> Main class name has not been configured and it could not be resolved
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.10.3/userguide/command_line_interface.html#sec:command_line_warnings
BUILD FAILED in 1s
3 actionable tasks: 1 executed, 2 up-to-date
Main class name has not been configured and it could not be resolved
23:26:18: Task execution finished 'build'.

framework-web 项目 3. build.gradle

dependencies {
    //依赖framework-redis项目
    compile project(':framework-redis')
    //不需要写版本
    compile('org.springframework.boot:spring-boot-starter-web')
    //不需要写版本
    compile('org.springframework.cloud:spring-cloud-starter-openfeign')
}

framework-redis 项目 4. build.gradle

dependencies {
    //依赖framework-base
    compile project(':framework-base')
    compile('org.springframework.boot:spring-boot-starter-data-redis')
}

framework-base 5. build.gradle

//做为演示没有引入任何jar包
dependencies {
}


相关文章
|
安全 Java Apache
微服务——SpringBoot使用归纳——Spring Boot中集成 Shiro——Shiro 身份和权限认证
本文介绍了 Apache Shiro 的身份认证与权限认证机制。在身份认证部分,分析了 Shiro 的认证流程,包括应用程序调用 `Subject.login(token)` 方法、SecurityManager 接管认证以及通过 Realm 进行具体的安全验证。权限认证部分阐述了权限(permission)、角色(role)和用户(user)三者的关系,其中用户可拥有多个角色,角色则对应不同的权限组合,例如普通用户仅能查看或添加信息,而管理员可执行所有操作。
672 0
|
安全 Java 数据安全/隐私保护
微服务——SpringBoot使用归纳——Spring Boot中集成 Shiro——Shiro 三大核心组件
本课程介绍如何在Spring Boot中集成Shiro框架,主要讲解Shiro的认证与授权功能。Shiro是一个简单易用的Java安全框架,用于认证、授权、加密和会话管理等。其核心组件包括Subject(认证主体)、SecurityManager(安全管理员)和Realm(域)。Subject负责身份认证,包含Principals(身份)和Credentials(凭证);SecurityManager是架构核心,协调内部组件运作;Realm则是连接Shiro与应用数据的桥梁,用于访问用户账户及权限信息。通过学习,您将掌握Shiro的基本原理及其在项目中的应用。
503 0
|
9月前
|
存储 API Android开发
【02】完整的安卓二次商业实战-配置gradle-构建打包原生安卓项目-调试本地运行模拟器-优雅草伊凡
【02】完整的安卓二次商业实战-配置gradle-构建打包原生安卓项目-调试本地运行模拟器-优雅草伊凡
898 4
【02】完整的安卓二次商业实战-配置gradle-构建打包原生安卓项目-调试本地运行模拟器-优雅草伊凡
|
8月前
|
负载均衡 Java API
《深入理解Spring》Spring Cloud 构建分布式系统的微服务全家桶
Spring Cloud为微服务架构提供一站式解决方案,涵盖服务注册、配置管理、负载均衡、熔断限流等核心功能,助力开发者构建高可用、易扩展的分布式系统,并持续向云原生演进。
|
前端开发 安全 Java
Spring Boot 便利店销售系统项目分包设计解析
本文深入解析了基于Spring Boot的便利店销售系统分包设计,通过清晰的分层架构(表现层、业务逻辑层、数据访问层等)和模块化设计,提升了代码的可维护性、复用性和扩展性。具体分包结构包括`controller`、`service`、`repository`、`entity`、`dto`、`config`和`util`等模块,职责分明,便于团队协作与功能迭代。该设计为复杂企业级应用开发提供了实践参考。
535 0
|
11月前
|
JSON 分布式计算 大数据
springboot项目集成大数据第三方dolphinscheduler调度器
springboot项目集成大数据第三方dolphinscheduler调度器
730 3
|
11月前
|
Java 关系型数据库 数据库连接
Spring Boot项目集成MyBatis Plus操作PostgreSQL全解析
集成 Spring Boot、PostgreSQL 和 MyBatis Plus 的步骤与 MyBatis 类似,只不过在 MyBatis Plus 中提供了更多的便利功能,如自动生成 SQL、分页查询、Wrapper 查询等。
1035 2
|
11月前
|
Java 关系型数据库 MySQL
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
1037 2
|
11月前
|
分布式计算 Java 大数据
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
557 2
|
11月前
|
Java 测试技术 Spring
简单学Spring Boot | 博客项目的测试
本内容介绍了基于Spring Boot的博客项目测试实践,重点在于通过测试驱动开发(TDD)优化服务层代码,提升代码质量和功能可靠性。案例详细展示了如何为PostService类编写测试用例、运行测试并根据反馈优化功能代码,包括两次优化过程。通过TDD流程,确保每项功能经过严格验证,增强代码可维护性与系统稳定性。
397 0

推荐镜像

更多