原创

gradle普通项目构建外部依赖jar的终极方法gradle瘦身

示例build.gradle如下

加载的包可以随意主要是下面的部分

plugins {
id 'maven-publish'
}


dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile('org.springframework:spring-context:5.0.9.RELEASE'){
exclude group:'commons-logging',module:'commons-logging'
}
compile 'org.springframework:spring-web:5.0.9.RELEASE'
compile 'org.springframework:spring-webmvc:5.0.9.RELEASE'
compile 'org.codehaus.janino:janino:3.0.10'
compile 'commons-lang:commons-lang:2.6'
compile 'commons-codec:commons-codec:1.11'
compile 'com.alibaba:fastjson:1.2.49'
compile 'io.netty:netty-all:4.1.29.Final'
compile ('com.alibaba:dubbo:2.6.2'){
exclude group: 'org.springframework', module: 'spring'
exclude group: 'org.jboss.netty', module: 'netty'
}
compile 'org.apache.zookeeper:zookeeper:3.4.9'
compile 'org.apache.curator:curator-framework:2.12.0'
}

publishing {
publications {
maven(MavenPublication) {
from(components.java)
}
}
}
//设置资源编译的编码
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
//资源目录的设置
sourceSets {
main {
java {
srcDir 'src/main/java'
}
resources {
srcDir 'src/main/java/resources'
}
}
test {
java {
srcDir 'test/java'
}
resources {
srcDir 'test/resources'
}
}
}
//设置主程序入口
def mainClassName = "com.cyjz.server.Main"

//在某些场合,我们不需要依赖和src打在一个jar包,我们希望有个lib,然后我们的jar运行时,自动去找依赖jar。这样,就可以不用插件了
task copyDependencies(type: Copy) {
from configurations.runtime
into 'build/libs/lib'
}
jar.dependsOn(copyDependencies)

jar {
manifest {
attributes "Main-Class": "$mainClassName"
attributes "Implementation-Title": project.name
}
//设置打包依赖的jar
// from {
// configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
// }

if (!configurations.runtime.isEmpty()) {
manifest.attributes('Class-Path': '. lib/' + configurations.runtime.collect { it.name }.join(' lib/'))
}
//将静态资源打包出来
processResources {
from('src/main/java/resources'){
include '**/*.*'
}
}
}


正文到此结束
本文目录