依存関係のライブラリ(JAR)を含めた実行可能JAR を Gradle で作成する

Gradle の jar タスクは以下のようにする。

jar {
    excluse 'MET-INF/*.SF', 'MET-INF/*.DSA', 'MET-INF/*.RSA', 'MET-INF/*.MF', 
    manifest {
        attributes 'Main-Class': 'org.yips.HellowMain'
        'Class-Path': configurations.runtime.files.collect { "lib/%it.name" }.join(' ')
    }
    from configurations compile.collect { it.isDirectory() ? it : zipTree(it) }
}

Class-Path の書き方を上のようにする。

ついでに、作成した jarファイルを別のディレクトリにコピーするタスクは、以下のとおり。
(InteliJ の場合)

task distribute(type Copy){
   from 'build/libs'
   into 'dist'
   include '*.jar'
}

InteliJ でこの distribute タスクは Gradle タブで other の下に作られる。
f:id:posturan:20200206215737j:plain