jar {
manifest {
attributes "Implementation-Version": "1.0"
}
}
Gradle 7.2 で確認
task createfatJar(type: Jar) {
baseName = 'Fuga'
duplicatesStrategy 'exclude'
manifest {
attributes 'Main-Class': 'com.example.Hoge'
}
archiveClassifier = "all"
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
with jar
}
./gradlew createfatJar
jar {
duplicatesStrategy 'exclude'
manifest {
attributes 'Main-Class': 'com.example.Hoge'
}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
plugins {
id 'java'
// git からコミットのハッシュ値、タグを取得してくれるプラグイン
id 'com.palantir.git-version' version '0.12.3'
}
group 'com.examples'
version 'v1.2'
repositories {
mavenCentral()
}
tasks.withType(JavaCompile).configureEach {
// コンパイル時の警告を表示する設定
options.deprecation = true
options.compilerArgs << '-Xlint:unchecked'
}
// git からバージョン情報取得
def gitInfo = versionDetails()
version = gitInfo.lastTag
def implementationVersion = version + '(' + gitInfo.gitHash[0..7] + ')'
jar {
// hoge.jar がビルドされる
archiveBaseName = 'hoge'
// これを指定しなかった場合、hoge-0.0.1.jar といった感じの JAR ファイルがビルドされる。
archiveVersion = ''
// MANIFEST.MF に設定する内容
manifest {
attributes(
// 起動するクラス
'Main-Class': 'com.examples.Hoge',
// gitかから取得したバージョン
'Implementation-Version' : implementationVersion,
'Implementation-Title' : 'Hoge'
)
}
// Fat JAR をビルド
duplicatesStrategy 'exclude'
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
dependencies {
implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.0.12'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
test {
useJUnitPlatform()
}
./gradlew jar
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.9.22'
id 'application'
}
group = 'org.example'
version = '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
jar {
archiveBaseName = 'hoge'
archiveVersion = ''
duplicatesStrategy 'exclude'
manifest {
attributes(
'Main-Class': 'MainKt',
'Implementation-Title' : 'Hoge'
)
}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
dependencies {
implementation("org.slf4j:slf4j-api:2.0.12")
implementation("ch.qos.logback:logback-classic:1.4.14")
}
test {
useJUnitPlatform()
}
kotlin {
jvmToolchain(21)
}
application {
mainClass.set('MainKt')
}
JDK 21 を使う場合は gradle-wrapper.properties に Gradle 8.x を使うように指定しておくとよさそう。
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
mkdir bin
#!/bin/sh
JAVA_OPTS='-Xmx32M'
java $JAVA_OPTS -jar "$0" "$@"
exit $?
#!/bin/bash
cat bin/exec.sh build/libs/hoge.jar > build/libs/hoge
chmod 755 build/libs/hoge
task executable(type: Exec) {
dependsOn "build"
commandLine '/bin/bash', 'bin/makeExecutable.sh'
}
./gradlew executable
(参考)
gradle dependencies
gradle dependencies --configuration runtime
gradle assembleDist
gradle compileKotlin
gradle compileJava
gradle compileTestKotlin
gradle compileTestJava
gradle build
gradle build -x test
./gradlew clean build --refresh-dependencies