在IDEA中使用gradle配置打可执行jar包[可执行jar与其所依赖的jar分离]

时间:2022-08-16 09:53:32

下面是我的项目的build.gradle文件的所有内容,注意高亮部分是关键:

group 'com.xbs'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'idea'

sourceCompatibility = 1.8

ext {
    profile = System.getProperty("env") ?: "dev"
    println "[current profile]:" + profile
}

repositories { mavenCentral() }
 dependencies { compile group: 'org.postgresql', name: 'postgresql', version: '9.4-1201-jdbc41'
    mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.5'
    mybatisGenerator 'mysql:mysql-connector-java:5.1.32'
    mybatisGenerator 'tk.mybatis:mapper:3.3.9'
    compile group: 'joda-time', name: 'joda-time', version:'2.5'
    compile group: 'org.apache.commons', name: 'commons-lang3', version:'3.3.2'
    compile group: 'org.apache.commons', name: 'commons-io', version:'1.3.2'
    compile group: 'commons-net', name: 'commons-net', version:'3.3'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version:'2.4.2'
    compile group: 'org.apache.httpcomponents', name: 'httpclient', version:'4.3.5'
    compile group: 'org.slf4j', name: 'slf4j-log4j12', version:'1.6.4'
    compile group: 'org.mybatis', name: 'mybatis', version:'3.2.8'
    compile group: 'org.mybatis', name: 'mybatis-spring', version:'1.2.2'
    compile group: 'com.github.miemiedev', name: 'mybatis-paginator', version:'1.2.15'
    compile group: 'com.github.pagehelper', name: 'pagehelper', version:'3.4.2'
    compile group: 'mysql', name: 'mysql-connector-java', version:'5.1.32'
    compile group: 'com.alibaba', name: 'druid', version:'1.0.9'
    compile group: 'org.springframework', name: 'spring-context', version:'4.1.3.RELEASE'
    compile group: 'org.springframework', name: 'spring-beans', version:'4.1.3.RELEASE'
    compile group: 'org.springframework', name: 'spring-webmvc', version:'4.1.3.RELEASE'
    compile group: 'org.springframework', name: 'spring-jdbc', version:'4.1.3.RELEASE'
    compile group: 'org.springframework', name: 'spring-aspects', version:'4.1.3.RELEASE'
    compile group: 'jstl', name: 'jstl', version:'1.2'
    compile group: 'commons-fileupload', name: 'commons-fileupload', version:'1.3.1'
    compile group: 'redis.clients', name: 'jedis', version:'2.7.2'
    compile group: 'org.apache.solr', name: 'solr-solrj', version:'4.10.3'
    compile group: 'com.alibaba', name: 'fastjson', version:'1.2.4'
    compile group: 'org.springframework.integration', name: 'spring-integration-kafka', version:'1.3.0.RELEASE'
    compile group: 'org.springframework.kafka', name: 'spring-kafka', version:'1.1.3.RELEASE'
    compile group: 'org.apache.kafka', name: 'kafka_2.10', version:'0.10.0.0'
    compile 'com.mashape.unirest:unirest-java:1.4.9'
    testCompile group: 'junit', name: 'junit', version:'4.12' }

sourceSets {
    main {
        resources {
            srcDirs = ["src/main/resources", "env/$profile"]
        }
    }
}

jar { String someString = ''
    configurations.runtime.each {someString = someString + " lib//"+it.name}
    manifest {
        attributes 'Main-Class': 'com.xbs.AppMain'
        attributes 'Class-Path': someString
    }
}
//清除上次的编译过的文件
task clearPj(type:Delete){
    delete 'build','target' } task copyJar(type:Copy){ from configurations.runtime into ('build/libs/lib')
}
//把JAR复制到目标目录
task release(type: Copy,dependsOn: [build,copyJar]) {
// from 'conf'
// into ('build/libs/eachend/conf') // 目标位置
}

展开右侧的Gradle侧边栏,找到在other下可以看到clearPj,copyJar以及release,双击release即可打jar包(它默认会执行copyJar,就是把所有的第三方依赖包放到lib目录下):

在IDEA中使用gradle配置打可执行jar包[可执行jar与其所依赖的jar分离]

如果看到控制台输出"BUILD SUCCESSFUL",说明打包成功:

在IDEA中使用gradle配置打可执行jar包[可执行jar与其所依赖的jar分离]

 

打好的jar默认放在项目中的build目录中:

在IDEA中使用gradle配置打可执行jar包[可执行jar与其所依赖的jar分离]

 打开jar包所在目录,可以使用反编译工具查看jar包中的目录结构和文件内容:

在IDEA中使用gradle配置打可执行jar包[可执行jar与其所依赖的jar分离]

 打开命令行窗口,使用java -jar命令来执行jar包:

 在IDEA中使用gradle配置打可执行jar包[可执行jar与其所依赖的jar分离]

 

如果觉得本文对您有帮助,不妨扫描下方微信二维码打赏点,您的鼓励是我前进最大的动力:

在IDEA中使用gradle配置打可执行jar包[可执行jar与其所依赖的jar分离]