惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
Spread Privacy
Spread Privacy
I
InfoQ
V
V2EX
S
Schneier on Security
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Attack and Defense Labs
Attack and Defense Labs
云风的 BLOG
云风的 BLOG
The Hacker News
The Hacker News
S
SegmentFault 最新的问题
C
Cybersecurity and Infrastructure Security Agency CISA
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
Latest news
Latest news
S
Secure Thoughts
Project Zero
Project Zero
MongoDB | Blog
MongoDB | Blog
I
Intezer
Security Latest
Security Latest
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
N
Netflix TechBlog - Medium
V2EX - 技术
V2EX - 技术
量子位
T
Threatpost
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
T
Tor Project blog
A
Arctic Wolf
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Check Point Blog
博客园 - Franky
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
L
LINUX DO - 热门话题

Deed's博客

解决群晖百度套件提示name "baiduapp" dupliacted - Deed's博客 2023年3月16日 Maven项目引入第三方本地jar包 - Deed's博客 2022年8月23日nginx反向代理配置去除前缀 - Deed's博客 2022年8月1日 ts-node 运行报错`Cannot find name 'console'.` 2022年6月23日 Windows 杀死进程 - Deed's博客 2022年6月16日 查看服务器异常IP访问以及处理 - Deed's博客 2022年6月15日 Linux删除用户 - Deed's博客 2022年6月8日 AntDesignPro文档Demo示例[后置拦截器]报错 - Deed's博客 2022年6月8日 React使用umi.js报错Invalid hook call - Deed's博客
2022年7月13日 Gradle项目打包相关配置 - Deed's博客
Mahalalel · 2022-07-13 · via Deed's博客
«

Mahalalel 发布于 阅读:6419 Gradle


近期,小编在用Gradle构建工具来开发项目,期间遇到了打包的问题。
众所周知,SpringBoot项目可以通过配置Maven的打包插件,就可以很轻松构建依赖jar与可执行jar。

特此记录下Gradle项目的打包配置

一、SpringBoot可执行jar打包

1.1、配置打包插件

首先需要在项目根目录下的build.gradle文件中配置插件

plugins {
    id "org.springframework.boot" version "2.4.2"
}

1.2、指定使用插件 并配置main方法入口类

配置指定使用的插件,尤其是在多模块工程下

// 配置所有项目公共的内容
allprojects {
    // springboot项目打jar包
    apply plugin: "org.springframework.boot"

    // 指定main方法所在的class
    springBoot {
        mainClass = "com.xxx.xxx.xxx.XxxXxxApplication"
    }
}

1.3、子模块配置依赖jar

启动部署模块,需要依赖其他模块,故此,在部署模块的依赖module的build.gradle中配置

jar {
    enabled = true
}

1.4、打包

打开IDEA开发工具中找到 gradle 窗口
在启动类所在模块中找到 Tasks ==>> build ==>> bootJar

Gradle打Jar包

点击运行,即可在模块下 build ==>> libs ==>> xxx.jar

Gradle打包后目录

二、SpringBoot项目war类型打包

2.1、指定使用插件

配置使用的插件

allprojects {

    apply plugin: 'war'
    // 指定main方法所在的class
    springBoot {
        mainClass = "com.xxx.xxx.xxx.XxxXxxApplication"
    }
}

2.2、新建Initializer类并继承SpringBootServletInitializer,重写configure

注:Initializer类应该与项目的启动类在同一级目录下

package com.xxxx.demo;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
                                    //Application的类名
        return application.sources(XxxApplication.class);
    }
}

2.3、同1.3

2.4、打war包

打开IDEA开发工具中找到 gradle 窗口
在启动类所在模块中找到 Tasks ==>> build ==>> bootWar
Gradle打包命令
点击运行,即可在模块下 build ==>> libs ==>> xxx.war

三、SpringBoot依赖jar包打包

在build.gradle中加入以下配置

jar {
    enabled = true
    // 分类器(打包成功后的会以 `项目名-dependency` 为包名)
    classifier = "dependency"
}

执行jar任务,即可将项目打成依赖包
SpringBoot生成依赖jar包

Gradle jar包 war包