springboot 通过 gradle 打成可运用的 Jar包

1. 首先的话需要配置build.gradle

buildscript {
    ext {
        springBootVersion = '2.2.4.RELEASE'
    }

    tasks.withType(JavaCompile) {
        options.encoding = "UTF-8"
    }

    repositories {
        maven {
            url 'http://maven.aliyun.com/nexus/content/groups/public'
        }
    }
    dependencies {
        // 引入 gradle 插件
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

allprojects {
    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: 'idea'
    apply plugin: 'jacoco'
    //注意这里使用org.springframework.boot的bootJar插件因此要引入这个插件
    apply plugin: 'org.springframework.boot'

    sourceCompatibility = '1.8'
    compileJava.options.encoding = 'UTF-8'
    compileTestJava.options.encoding = 'UFT8'
    repositories {
        maven {
            url 'http://maven.aliyun.com/nexus/content/groups/public'
        }
    }
    dependencies {
        implementation "org.springframework.boot:spring-boot-starter-web:${springBootVersion}"
        testImplementation "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"
        testCompile group: 'junit', name: 'junit', version: '4.12'
        
        implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.18'

        implementation group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter', version: '2.1.2'

//        implementation files('lib/demo.main.jar')

    }
}
// Main-Class的目录 必须有的
jar {
    String someString = ''
    configurations.runtime.each {someString = someString + " lib//"+it.name}
    manifest {
        attributes 'Main-Class': 'com.xu.Application'
        attributes 'Class-Path': someString
    }
}

2. 使用gradle 的build 的命令进行打包

3. jar包会生成在项目根目录的 \build\libs 下

通过命令行执行 java -jar ×××.jar  

 

执行功能,可以正常访问,执行java -jar 的时候需要你当前的环境有java环境 

猜你喜欢

转载自blog.csdn.net/qq_42673067/article/details/130058845