利用gradle shadowjar构建包含依赖的JAR包

正常情况下,gradle构建的jar包是不包含依赖的,只包含项目源码。

可是有些情况下我们希望JAR包中也包含相关依赖,譬如说向SPARK集群提交作业的时候,运行JAVA命令的时候。

这篇文件介绍一种方便的构建assemble jar的方法

apply plugin :'java'
apply plugin :'scala'
apply plugin: 'com.github.johnrengelman.shadow'

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.4'
    }
}

repositories {
    mavenCentral()
}

shadowJar {
   baseName = 'spark-job'
   classifier = null
   version = null
   dependencies {
      include(dependency('org.apache.flume:flume-ng-core:1.7.0'))
  	  include(dependency('org.apache.spark:spark-streaming-kafka-0-10_2.11:2.0.2'))
  	  include(dependency('org.apache.flume:flume-ng-sdk:1.7.0'))
  	  include(dependency('org.apache.kafka:kafka-clients:0.10.1.0'))
   }
}

dependencies {
	compile group: 'org.apache.spark', name: 'spark-mllib_2.11', version: '2.0.2'
	compile group: 'org.apache.spark', name: 'spark-streaming_2.11', version: '2.0.2'
    compile group: 'org.apache.spark', name: 'spark-sql_2.11', version: '2.0.2'
    compile group: 'org.apache.spark', name: 'spark-hive_2.11', version: '2.0.2'
    compile 'org.apache.spark:spark-sql-kafka-0-10_2.11:2.0.2'
    compile group: 'org.apache.flume', name: 'flume-ng-core', version: '1.7.0'
	compile group: 'org.apache.flume', name: 'flume-ng-sdk', version: '1.7.0'
    compile 'org.apache.spark:spark-streaming-kafka-0-10_2.11:2.0.2'
    compile group: 'org.apache.hadoop', name: 'hadoop-common', version: '2.7.3'
    compile group: 'org.apache.hadoop', name: 'hadoop-hdfs', version: '2.7.3'
    compile group: 'org.apache.kafka', name: 'kafka-clients', version: '0.10.1.0'
}


这是某个项目的相关build.gradle

核心是名为shadow的插件,打包命令为clean shadowJar

你会发现build好的JAR包中,不仅包含项目源码,还包括flume以及kafka中的类。

其他配置请参考官方文档:

shadow官方文档




 

你可能感兴趣的:(scala,gradle,spark,Spark)