LoginSignup
0
0

More than 1 year has passed since last update.

Gradleコマンド・build.gradle記述方法まとめ

Posted at

初めに

本記事はJava使用時のGradleの使用を想定しています。ご注意ください。
Gradleコマンド
build.gradle記述方法

Gradleコマンド

Gradleコマンドについてです。

プロジェクトの作成

プロジェクトの作成方法

gradle init 

クリーン

クリーンの方法

# クリーン
gradle build 

ビルド

ビルドの方法

# ビルド
gradle build 

プログラムの実行

# プログラムの実行
gradle run --args="任意の引数1 任意の引数2..."

以下例です。

# 例
gradle run --args="1 1"

build.gradle記述方法

build.gradleの記述方法についてです。

Mainクラスを指定

# Mainクラスを指定
application {
    mainClass = 'メインクラスを指定(パッケージ込み)'
}

以下例です。

# 例
application {
    mainClass = 'app.App'
}

実行可能なjarファイルを作成する

実行可能なjarファイルを作成したい

#
jar {
    manifest {
        attributes 'Main-Class': 'メインクラスを指定(パッケージ込み)'
    }
} 

以下例です。

# 例
jar {
    manifest {
        attributes 'Main-Class': 'app.App'
    }
}

外部jarの追加

外部jarの追加したい

# 外部jarの追加
dependencies {
    implementation '任意のライブラリ'
}

以下例です。

# 例(OpenCSVの追加)
dependencies {
    implementation 'com.opencsv:opencsv:5.6'
}

おまけ

外部jarをまとめて1つのライブラリにしたい。

Shadowと呼ばれるGradleのプラグインを使用します。

手順1 Shadowプラグインの追加

# 手順1 Shadowプラグインの追加
plugins {
    id 'application'
    // Shadowプラグインの追加
    id 'com.github.johnrengelman.shadow' version '7.1.2'
}

repositories {
    mavenCentral()
}

dependencies {
    // 追加 opencsv
    implementation 'com.opencsv:opencsv:5.6'
}

application {
    mainClass = 'app.App'
}

jar {
    manifest {
        attributes 'Main-Class': 'app.App'
    }
}

外部ライブラリをまとめてjar化するコマンド。

外部ライブラリをまとめてjar化するコマンドです。

外部ライブラリをまとめてjar化するコマンド
gradle shadowJar

参考

0
0
0

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
0
0