目录
Springboot默认jar的方式打包,会把所有依赖都打包,有的jar比较大,变动很小,这样每次部署都整个发布,网速不好花的时间就会比较多,我们可以把依赖的jar包独立出来。
一、获取依赖的jar包
在Idea终端使用命令:
mvn dependency:copy-dependencies -DoutputDirectory=E:\jar -DincludeScope=runtime
二、修改pom文件
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.seaxiang.blog.BlogAdminApplication</mainClass>
<!-- 瘦身的jar layout 必须是 ZIP, one jar 的可以是 JAR -->
<layout>ZIP</layout>
<!--构建完整可执行程序,可以直接运行-->
<executable>true</executable>
<!-- 编译出不带 lib 文件夹的Jar包 -->
<includes>
<include>
<groupId>nothing</groupId>
<artifactId>nothing</artifactId>
</include>
</includes>
</configuration>
</plugin>
三、运行命令
java -jar -Dloader.path=E:\jar demo.jar >> demo.log
四、gradle版本
plugins {
id 'org.springframework.boot' version '2.5.6'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'cn.demo.backend'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
maven { url 'https://maven.aliyun.com/repository/public' }
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
google()
mavenCentral()
}
ext {
elasticsearchVersion = '7.8.0'
}
dependencies {
implementation fileTree(dir: 'lib', include: ['*.jar'])
//swagger
implementation "io.springfox:springfox-swagger-ui:2.9.2"
implementation group: 'com.github.xiaoymin', name: 'knife4j-spring-boot-starter', version: '2.0.4'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
implementation 'org.apache.commons:commons-collections4:4.4'
implementation 'org.apache.commons:commons-lang3:3.12.0'
implementation 'org.modelmapper:modelmapper:2.3.7'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.querydsl:querydsl-jpa:4.4.0'
annotationProcessor("com.querydsl:querydsl-apt:4.4.0:jpa", "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final"
, "javax.annotation:javax.annotation-api:1.3.2")
runtimeOnly 'mysql:mysql-connector-java'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'com.h2database:h2'
testImplementation 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
implementation 'com.alibaba:fastjson:1.2.83'
implementation group: 'cn.hutool', name: 'hutool-all', version: '5.7.20'
implementation "org.elasticsearch.client:transport:$elasticsearchVersion"
implementation "org.elasticsearch:elasticsearch:$elasticsearchVersion"
implementation "org.elasticsearch.plugin:transport-netty4-client:$elasticsearchVersion"
implementation "org.elasticsearch.client:elasticsearch-rest-high-level-client:$elasticsearchVersion"
}
tasks.named('test') {
useJUnitPlatform()
}
// 清除lib
task myClearLib(type: Delete) {
delete "$buildDir/libs/libs"
}
// 拷贝lib
task myCopyLib(type: Copy) {
into "$buildDir/libs/libs"
from configurations.runtimeClasspath
}
bootJar {
excludes = ["*.jar"]
// lib目录的清除和复制任务
dependsOn myClearLib
dependsOn myCopyLib
// 指定依赖包的路径,运行时不再需要指定 java.ext.dir 或 loader.path 参数。
manifest {
attributes "Manifest-Version": 1.0,
'Class-Path': configurations.runtimeClasspath.files.collect { "libs/$it.name" }.join(' ')
}
}