【Gradle项目构建】纯内网或无网络构建配置 您所在的位置:网站首页 gradle项目配置 【Gradle项目构建】纯内网或无网络构建配置

【Gradle项目构建】纯内网或无网络构建配置

2023-08-01 16:44| 来源: 网络整理| 查看: 265

Gradle管理的Java Web项目离线构建 前言一、build.gradle在线使用配置二、build.gradle离线使用配置三、内网下使用私有仓库配置总结

前言

最近Gradle发展快速,使用方便功能强大,且支持将第三方jar包依赖置于同一个目录下即可实现离线的构建与使用,而不需要像Maven一样,需要拷贝整个Maven的本地Repository仓库且不能改变目录结构。

离线构建主要基于build.gradle的dependencies配置,同时,对于SpringBoot项目,如果希望像在线项目一样打包成一个Jar包,里面的目录结构与在线build的Jar包目录结构一致,则需要引入Gradle的SpringBoot插件,因此项目创建时,外部依赖jar包可分为两部分放置:

plugins:放置Gradle的SpringBoot插件需要使用的jar包,用于实现打包runtime:放置项目开发、运行需要使用的jar包,如lombok-*.jar、mybatis-*.jar、spring-boot-*.jar等

目录结构如下图: 离线开发项目目录结构

打包后Jar包内部结构如下: 离线打包与在线打包结果 直接运行:

没有配置数据库连接报错,无关大局

运行

一、build.gradle在线使用配置

当前配置用于下载依赖的插件以及依赖包。 提示:插件和依赖交替注释,分别运行对应的copy任务,以免混淆 对项目的build.gradle进行配置,配置如下:

plugins { id 'org.springframework.boot' version '2.5.3' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' } group = 'com.example' version = '0.0.1' sourceCompatibility = '1.8' repositories { // mavenCentral() maven {url 'https://maven.aliyun.com/repository/public'} } dependencies { // 下载插件 implementation group: 'org.springframework.boot', name: 'spring-boot-gradle-plugin', version: '2.5.3' // 下载依赖 implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'cn.hutool:hutool-all:5.7.19' implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch' implementation 'org.springframework.boot:spring-boot-starter-data-neo4j' implementation 'org.apache.kafka:kafka-streams' implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.2' implementation 'org.springframework.kafka:spring-kafka' runtimeOnly 'mysql:mysql-connector-java' } // 拷贝插件 task CopyPlugins(type: Copy) { from configurations.runtimeClasspath into "$rootDir/libs/plugins" } // 拷贝依赖 task CopyRuntime(type: Copy) { from configurations.compile into "$rootDir/libs/runtime" } 二、build.gradle离线使用配置

当前配置用于离线开发场景,支持打包成在线打包的结果形式 build.gradle离线配置如下:

buildscript { dependencies { // 指定引用插件 classpath fileTree(dir: 'libs/plugins', includes: ['*.jar']) } } apply plugin: 'java' apply plugin: 'idea' apply plugin: 'org.springframework.boot' group 'com.example' version '1.0' sourceCompatibility = 1.8 tasks.withType(JavaCompile) { options.encoding = "UTF-8" } repositories { // mavenCentral() maven {url 'https://maven.aliyun.com/repository/public'} } dependencies { // 指定引用 libs/runtime目录下的依赖包 annotationProcessor fileTree(dir: 'libs/runtime', includes: ['lombok-*.jar']) compile fileTree(dir: 'libs/runtime', includes: ['*.jar']) } 三、内网下使用私有仓库配置 两种插件使用方式混用加上在线下载的方式管理 plugins { id 'org.springframework.boot' version '2.5.3' id 'java' } // 还不清楚为什么这个需要使用如下方式引入插件才能使用 apply plugin: "io.spring.dependency-management" group 'com.example' version '1.0' sourceCompatibility = 1.8 tasks.withType(JavaCompile) { options.encoding = "UTF-8" } dependencies { // 下载插件 implementation group: 'org.springframework.boot', name: 'spring-boot-gradle-plugin', version: '2.5.3' // 下载依赖 implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'cn.hutool:hutool-all:5.7.19' implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch' implementation 'org.springframework.boot:spring-boot-starter-data-neo4j' implementation 'org.apache.kafka:kafka-streams' implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.2' implementation 'org.springframework.kafka:spring-kafka' runtimeOnly 'mysql:mysql-connector-java' } 使用buildscript buildscript { repositories { maven {url 'http://ip:port/public'} } dependencies { classpath "io.spring.gradle:dependency-management-plugin:1.0.12.RELEASE" } } plugins { id 'java' id 'org.springframework.boot' version '2.5.3' } apply plugin: 'io.spring.dependency-management' apply plugin: 'java' group = 'com.test.example' version = '0.0.1' sourceCompatibility = "1.8" configurations { compileOnly { extendsFrom annotaionProcessor } } dependencies { // 下载插件 implementation group: 'org.springframework.boot', name: 'spring-boot-gradle-plugin', version: '2.5.3' // 下载依赖 implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'cn.hutool:hutool-all:5.7.19' implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch' implementation 'org.springframework.boot:spring-boot-starter-data-neo4j' implementation 'org.apache.kafka:kafka-streams' implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.2' implementation 'org.springframework.kafka:spring-kafka' runtimeOnly 'mysql:mysql-connector-java' } 待探索

如果需要打包成仅包含自身业务代码的Jar,将依赖Jar放置于外部的方式,需要自定义打包方式

总结

以上即为针对于在纯内网或者无网络环境下,使用Gradle进行项目开发场景的配置,同时支持针对于包含所有依赖jar和自身业务代码编译后文件打成一个Jar包运行且目录结构与在线SpringBoot打包结果相同的需求。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有