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 '**/*.*'
}
}
}

 

 

 

如果觉得本文对您有所帮助,欢迎您扫码下图所示的支付宝和微信支付二维码对本文进行随意打赏。您的支持将鼓励我继续创作

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值