下面是個人項目的build.gradle文件的全部內容,注意高亮部分是關鍵:java
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目錄下):mysql
若是看到控制檯輸出"BUILD SUCCESSFUL",說明打包成功:git
打好的jar默認放在項目中的build目錄中:github
打開jar包所在目錄,可使用反編譯工具查看jar包中的目錄結構和文件內容:web
打開命令行窗口,使用java -jar命令來執行jar包:redis
若是以爲本文對您有幫助,不妨掃描下方微信二維碼打賞點,您的鼓勵是我前進最大的動力:spring