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

网友投稿 1174 2022-11-14

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

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

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

group 'com.xbs'version '1.0-SNAPSHOT'apply plugin: 'java'apply plugin: 'idea'sourceCompatibility = 1.8ext { profile = System.getProperty("env") ?: "dev" println "[current 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.name: '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'}= ["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目录下):

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

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

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

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:JMeter测试工具中的参数化使用[函数助手]
下一篇:一次DB2数据库连接失败(SQLSTATE=08001)的解决方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~