LoginSignup
87
82

More than 5 years have passed since last update.

gradleをつかって依存ライブラリを含む単一で実行可能なjarを生成する

Posted at

バッチ処理をおこなう実行可能jarを生成する場合にはコマンドラインでいちいちクラスパスを設定するのは面倒ですし依存関係がかわるとバッチ処理コマンドも変更する必要が出てくるので管理が煩雑です。

gradleを利用してjarを生成する際に依存jar内のクラスも含めて一つのjarに入れればデプロイが格段に楽になります。

やり方は簡単。
build.gradleファイルのjarの設定に
from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
を加えるだけでできます。

build.gradle
apply plugin: "java"
apply plugin: "maven"

def defaultEncoding = 'UTF-8'
[compileJava, compileTestJava]*.options*.encoding = defaultEncoding

sourceCompatibility = 1.7 
targetCompatibility = 1.7 
group = 'com.mychaelstyle.libs'
archivesBaseName = 'awstools' 
version = '0.1.0'

repositories {
  maven {
    url System.getenv("HOME") + "/.m2/repository"
  }
  mavenCentral()
}

dependencies {
  testCompile "junit:junit:4.11"
  compile 'javax.mail:mail:1.4'
  compile 'com.amazonaws:aws-java-sdk:1.6.4'
}

jar {
  manifest {
    attributes 'Implementation-Title': 'Mychael Style Tools', 'Implementation-Version': 1.0 
    attributes "Main-Class" : "com.mychaelstyle.tools.Main"
  }
  from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}

87
82
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
87
82