springboot2的gradle分离lib依赖jar瘦身打包

plugins {
    
    
    id 'org.springframework.boot' version '2.2.1.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}

group 'com.ddddd'
version '1.0'
sourceCompatibility = 1.8

repositories {
    
    
    mavenLocal()
    maven {
    
     url 'http://maven.aliyun.com/nexus/content/repositories/central/' }
    mavenCentral()
}

dependencies {
    
    
    implementation 'org.springframework.boot:spring-boot-starter-web'
//    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
    
    
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    compileOnly 'org.springframework.boot:spring-boot-devtools'
//    加载外部jar
//    compile files('lib/ojdbc6.jar')
//     去除内置tomcat
//    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
}

test {
    
    
    useJUnitPlatform()
}

// 清除lib
task myClearLib(type: Delete) {
    
    
    delete "$buildDir/libs/lib"
}

// 拷贝lib
task myCopyLib(type: Copy) {
    
    
    from configurations.compileClasspath
    into "$buildDir\\libs\\lib"
}

bootJar {
    
    
    excludes = ["*.jar"]

    // lib目录的清除和复制任务
    dependsOn myClearLib
    dependsOn myCopyLib

    // 指定依赖包的路径,运行时不再需要指定 java.ext.dir 或 loader.path 参数。
    manifest {
    
    
        attributes "Manifest-Version": 1.0,
                'Class-Path': configurations.compileClasspath.files.collect {
    
     "lib/$it.name" }.join(' ')
    }
}

使用bootjar打包,在build/libs下是项目jar与依赖的lib文件夹
cmd切换到build/libs目录执行运行

java -jar springtest.jar

因为springtest.jar/META-INF/MANIFEST.MF里的Class-Path已经表面依赖的所有jar路径就是当前lib目录下

猜你喜欢

转载自blog.csdn.net/c5113620/article/details/104670347