「Eclipseなんか使いたくない」「さくっとJava環境を整えたい」というときに、ひとつのDockerfileに全部コミコミでまとめました。(事前にDockerが使える環境は必要です。)
Dockerfile 構成
Dockerfileに含まれている物たちはこちら
- Ubuntu 20.04
- adoptOpenJava-11
- Gradle (latest)
- ubuntu でよく使う必須Tools
- sudo
- wget
- vim
- curl
- tree
- csv
Dockerfileは以下になります。コピーして(または編集して)お使いください。
# ubuntu ver 20.04 image
FROM ubuntu:20.04
# set timezome befor installing software-properties-common
ENV TZ=Asia/Tokyo
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# install common linux command
RUN apt-get update && apt-get install -y \
sudo \
wget \
vim \
curl \
tree \
cvs
# install adoptOpenJava-11 and Gradle
RUN sudo apt-get install -y software-properties-common && \
sudo add-apt-repository --yes ppa:rpardini/adoptium-installers && \
sudo add-apt-repository --yes ppa:cwchien/gradle && \
sudo apt-get update && \
sudo apt-get install -y adoptium-11-installer && \
sudo apt-get install -y gradle
CMD ["/bin/bash"]
DockerfileからJava環境を立ち上げる
imageを作る
上記で示したDockerfileを任意のディレクトリにコピーしてください。まずDockerfileからDocker imageを作成します。
-t オプションで任意のリポジトリ名とタグ名を指定できます。今回は、リポジトリ名=「myjava」, タグ名=「openJDK11」としました。リポジトリ名は小文字にする必要がありますのでご注意ください。
// imageの生成
$ cd {Dockerfileが格納されているディレクトリ}
$ docker build -t myjava:openJDK11 . //リポジトリ名は小文字にする必要がある
// 生成されたimageの確認
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myjava openJDK11 f5392ad45fbb 41 minutes ago 966MB
コンテナ生成/起動
生成したimageからコンテナを生成して起動します。
dockerコマンドの基本形はこちら
docker run -it -v {マウントしたいローカルディレクトリ}:{マウントしたいコンテナ内ディレクトリ} {イメージID} bash
下の例では、ローカルカレントディレクトリをコンテナ上の/home/helloWorldディレクトリにマウントしています。
$ docker run -it -v $PWD:/home/helloWorld f5392ad45fbb bash
root@6a9016de57f9:/# // rootでubuntuに入りました
// /home/helloWorldディレクトリが作成されているので移動する
root@640bbfda613d:/# cd /home/helloWorld/
これで、Java環境が整いました。各種バージョンを確認してみましょう。
Gradleを使わない場合は、この時点でjavacでコンパイルすることができます。
// Java バージョン確認
root@640bbfda613d:/home/helloWorld# java -version
openjdk version "11.0.12" 2021-07-20
OpenJDK Runtime Environment Temurin-11.0.12+7 (build 11.0.12+7)
OpenJDK 64-Bit Server VM Temurin-11.0.12+7 (build 11.0.12+7, mixed mode)
// コンパイラJavacバージョン確認
root@640bbfda613d:/home/helloWorld# javac -version
javac 11.0.12
// Gradleバージョン確認
root@640bbfda613d:/home/helloWorld# gradle -version
Welcome to Gradle 7.1.1!
Here are the highlights of this release:
- Faster incremental Java compilation
- Easier source set configuration in the Kotlin DSL
For more details see https://docs.gradle.org/7.1.1/release-notes.html
------------------------------------------------------------
Gradle 7.1.1
------------------------------------------------------------
Build time: 2021-07-02 12:16:43 UTC
Revision: 774525a055494e0ece39f522ac7ad17498ce032c
Kotlin: 1.4.31
Groovy: 3.0.7
Ant: Apache Ant(TM) version 1.10.9 compiled on September 27 2020
JVM: 11.0.12 (Eclipse Foundation 11.0.12+7)
OS: Linux 5.10.47-linuxkit amd64
GradleでJavaプロジェクトを作成する
Gradle init
「Gradle init」でプロジェクトを管理する「build.gradle」テンプレートの作成と、必要なディレクトリ構成が自動生成されます。
テンプレートが自動生成されますが、内容は後でいくらでも編集できます。必要に応じで編集していきましょう。
root@640bbfda613d:/home/helloWorld# gradle init
Select type of project to generate:
1: basic
2: application
3: library
4: Gradle plugin
Enter selection (default: basic) [1..4] 2 //applicationを選択
Select implementation language:
1: C++
2: Groovy
3: Java
4: Kotlin
5: Scala
6: Swift
Enter selection (default: Java) [1..6] 3 //Javaを選択
Split functionality across multiple subprojects?:
1: no - only one application project
2: yes - application and library projects
Enter selection (default: no - only one application project) [1..2] 1
Select build script DSL:
1: Groovy
2: Kotlin
Enter selection (default: Groovy) [1..2] 1 // Groovyを選択
Select test framework:
1: JUnit 4
2: TestNG
3: Spock
4: JUnit Jupiter
Enter selection (default: JUnit Jupiter) [1..4] 1 // JUnitを選択
Project name (default: helloWorld): //(enter) デフォルトのまま
Source package (default: helloWorld): //(enter) デフォルトのまま
生成されたディレクトリとファイル構成を見てみます。
Ubuntuで使える「tree」コマンドをインストール済みですので、「tree」コマンドでディレクトリ構成を確認します。
root@640bbfda613d:/home/helloWorld# tree
.
|-- app
| |-- build.gradle // (1)
| `-- src
| |-- main
| | |-- java
| | | `-- App.java // (2)
| | `-- resources
| `-- test
| |-- java
| | `-- AppTest.java // (3)
| `-- resources
|-- gradle
| `-- wrapper
| |-- gradle-wrapper.jar // (4)
| `-- gradle-wrapper.properties // (5)
|-- gradlew // (6)
|-- gradlew.bat // (7)
`-- settings.gradle // (8)
(1) プロジェクトを構成するためのGradleビルドスクリプト
依存関係やクラスパスなどを記述する
(2) ソースコード
ソースコードは app/source/main/java/ディレクトリ配下に格納する
(3) テスト ソースコード
テストコードは app/source/test/java/ディレクトリ配下に格納する
(4) Gradle Wrapper実行可能JAR
(5) Gradleラッパー構成プロパティ
(6) Unixベースのシステム用のGradleラッパースクリプト
./gradlew {Gradleコマンド} でGradleコマンドを実行する
(7) Windows用Gradleラッパースクリプト
(8) Gradleビルドを構成するためのGradle設定スクリプ
ビルドするプロジェクト名を記述する(複数のプロジェクト名も記述可能)
build.gradleを編集する
自動生成されたテンプレートだけでも、「./gradlew run」で実行できますが、ここでは少し設定を追加します。
(1)にプロジェクトに必要なプラグインを記載していきます。標準的なプラグインはこちらを参照ください。
// (1)プラグイン定義
plugins {
id 'application'
id 'java'
}
// (2)リポジトリ定義&優先順位(google--> mavenCentral--> jcenter)
repositories {
google()
mavenCentral()
jcenter()
}
// (3)依存性定義
dependencies {
testImplementation 'junit:junit:4.13.2'
implementation 'com.google.guava:guava:30.1-jre'
}
// (4)applicationプラグインで使用するメインクラス名を指定
application {
mainClass = 'helloWorld.App'
}
// (5)jarファイルを生成するときのメインクラス名を指定
jar {
manifest {
attributes(
'Main-Class': 'helloWorld.App'
)
}
}
ビルド&実行
gradlew run (アプリケーション実行)
‘application’をプラグインしていますので。「run」コマンドでhelloWorldアプリケーションが実行できます。
root@640bbfda613d:/home/helloWorld# ./gradlew run
> Task :app:run
Hello World!
「Hello World!」が表示されました。
このとき、クラスファイルも作成されています。javaコマンドで生成されたクラスファイルが実行できるか試してみましょう。
// クラスファイルの確認
root@640bbfda613d:/home/helloWorld# tree
|-- app
| |-- build
| | |-- classes
| | | `-- java
| | | `-- main
| | | `-- helloWorld
| | | `-- App.class //クラスファイル
(省略)
//クラスパスを指定して、クラスファイルを実行
root@640bbfda613d:/home/helloWorld# java --class-path app/build/classes/java/main/ helloWorld.App
Hello World!
gradlew build (jarファイル生成)
こんどは、「gradle build」コマンドを使ってみます。「gradle run」で作られたディレクトリやファイル類を削除しておきましょう。
//初期状態にもどす
root@640bbfda613d:/home/helloWorld# ./gradlew clean
// build
root@640bbfda613d:/home/helloWorld# ./gradlew build
//クラスファイル、jarファイルの確認
root@640bbfda613d:/home/helloWorld# tree
.
|-- app
| |-- build
| | |-- classes
| | | `-- java
| | | |-- main
| | | | `-- helloWorld
| | | | `-- App.class //クラスファイル
(中略)
| | |-- libs
| | | `-- app.jar //jarファイル
(省略)
//クラスパスを指定して、クラスファイルを実行
root@640bbfda613d:/home/helloWorld# java --class-path app/build/classes/java/main/ helloWorld.App
Hello World!
// jarファイルの実行
root@640bbfda613d:/home/helloWorld# java -jar app/build/libs/app.jar
Hello World!
クラスファイルもjarファイルの実行もできました。お疲れ様でした。
gradlew taskでコマンドを確認する
今回使った「run」「build」「clean」以外のコマンドもチェックしておきましょう。「gradlew task」でコマンドリストが表示されます。
root@640bbfda613d:/home/helloWorld# ./gradlew task
> Task :tasks
------------------------------------------------------------
Tasks runnable from root project 'helloWorld'
------------------------------------------------------------
Application tasks
-----------------
run - Runs this project as a JVM application
Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.
Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.
Distribution tasks
------------------
assembleDist - Assembles the main distributions
distTar - Bundles the project as a distribution.
distZip - Bundles the project as a distribution.
installDist - Installs the project as a distribution as-is.
Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.
Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'helloWorld'.
dependencies - Displays all dependencies declared in root project 'helloWorld'.
dependencyInsight - Displays the insight into a specific dependency in root project 'helloWorld'.
help - Displays a help message.
javaToolchains - Displays the detected java toolchains.
outgoingVariants - Displays the outgoing variants of root project 'helloWorld'.
projects - Displays the sub-projects of root project 'helloWorld'.
properties - Displays the properties of root project 'helloWorld'.
tasks - Displays the tasks runnable from root project 'helloWorld' (some of the displayed tasks may belong to subprojects).
Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.
コメント