【Java】MacでJavaの開発環境を構築しよーよ!
こんにちは、かっつんです!
今日はJavaの開発環境の構築手順について紹介します!
Java8 + MySQL + SpringBoot + IntelliJで構築します!
ミドルウェアのインストール
まずはApple IDを取得(アカウントを持っていない場合のみ)
しましょう!
- https://appleid.apple.com/account#!&page=create から登録出来ます!
- ID/Passwordは忘れ無いように気をつけてくださいね!
Apple IDが取得出来たら、ここからは、ターミナルを開いてコマンドラインから実行しましょう!
Xcodeをインストールします!(取得した Apple ID の ID/Password の入力を求められます)
$ xcode-select --install
続いては、HomeBrew
インストールです!
$ /usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)” $ brew update && brew cleanup $ brew tap caskroom/cask $ brew tap caskroom/versions
HomeBrewのインストールが完了したら、Javaをインストールします!!
今回はJava8
のインストールです!
$ brew cask install java8 $ export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
続いて、
MySQL, maven, gradle, sbtをインストールします!
MySQLはデータベース、maven, gradle, sbtはビルドツールです!
$ brew install mysql@5.7 $ brew install maven $ brew install sbt $ brew install gradle
環境変数を設定しましょう!
以下まとめてコピペして実行してください!!
(bash
を利用する場合の手順です!他のシェルを使う場合は自力でお願いします。。。)
$ cat <<EOL >> ~/.bash_profile export JAVA_HOME=`/usr/libexec/java_home -v 1.8` export PATH=$JAVA_HOME/bin:/usr/local/bin:$PATH export PATH=“/usr/local/opt/mysql@5.7/bin:$PATH” EOL source ~/.bash_profile
MySQL の自動起動(任意)
brew install
した時点で MySQLは自動起動される設定になっています。
この設定が邪魔になる場合は自動起動を無効化してください。
- 自動起動を無効にする
$ brew services stop mysql
- 自動起動を有効にする
$ brew services start mysql
MySQL の起動・停止方法と設定ファイルの例
※インストール完了時点で自動起動が有効になっている場合は start/stop コマンドは失敗します
- 起動
$ mysql.server start
- 停止
$ mysql.server stop
- MySQLコンソールへのログイン
$ mysql -uroot
- MySQL に root パス無しでログイン出来るように設定
$ mysql -uroot mysql> ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘’;
- 設定ファイル
~/.my.cnf
を編集
[client] default-character-set = utf8mb4 [mysql] default-character-set = utf8mb4 [mysqld] character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci init-connect=‘SET NAMES utf8mb4;SET AUTOCOMMIT=0’ skip-character-set-client-handshake lower_case_table_names=1 sql_mode=NO_ENGINE_SUBSTITUTION #log-slow-queries=/tmp/slow.log #long-query-time=30 #log-queries-not-using-indexes #log-slow-admin-statements #log=/tmp/query.log max_allowed_packet=32MB
IntelliJ IDEA の各種設定
IntelliJをインストールしましょう!
https://www.jetbrains.com/idea/download/#section=macからダウンロード出来ます!
トライアル期間中は無料で使用可能です!
DevTools でホットリロード(Spring Boot利用時)
pom.xml に spring-boot-devtools を追加
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <version>2.0.1.RELEASE</version> </dependency>
gradle を使っている場合は build.gradle に runtime(実行時) の依存ライブラリとして追加
dependencies { runtime(‘org.springframework.boot:spring-boot-devtools’) }
Preferences -> Compiler -> Build project automatically
にチェックを入れる
Shift+Command+A
でregistry
を選択したうえで、compiler.automake.allow.when.app.running
にチェックを入れ有効にする
自動インポート
Preference -> Editor > General > Auto Import -> Add unambiguous imports on the fly
Preference -> Editor > General > Auto Import -> Optimize imports on the fly
Scala プラグイン導入
IntelliJ IDEA -> Preferences -> Plugins で Scala を検索してインストール
Lombok プラグイン導入
IntelliJ IDEA -> Preferences -> Plugins で Lombok を検索してインストール
このままだとコンパイルエラーが発生するので下記も
Preferences -> Build, Execution, Deployment –> Compiler –> Annotation Processors -> Enable annotation processing
をチェック
Save Actions プラグイン導入
保存時に自動フォーマット+αしてくれるプラグインです。プロジェクト単位で都度設定が必要です
Preference -> Plugins -> Browse Repositories...
でSave Actions
を検索しインストール
`Preferences -> Other Settings -> Save Actions -> “Compile fileとAdd static modifier to methods 以外“にチェックを入れて OK 押す
# Add class qualifier to static member access
# Add class qualifier to static member access outside declaring class only
の2つは片方しか選択できません。下の “outside declaring class only” がついている方にチェックを入れてください
Codic プラグイン導入(オプション)
Preference -> Plugins -> Browse Repositories... で Codic Plugin を検索しインストール
https://codic.jp/signup から GitHub などのアカウントでユーザー登録し、プロジェクト作成 & API アクセスキー取得を済ませておく
取得したアクセスキーを Preference -> Otther Settings -> Codic Plugin に 1 のアクセスキーとプロジェクト名を入力
Inspection
特定のコードに対して警告やエラーとして表示してくれる機能
※警告やエラーがあってもコンパイルは通ります
以下設定は Severity を Warning から Error に変更してください
マジックナンバー禁止
Preferences > Editor > Inspections > Java > Abstraction Issue > Magic number{} の省略を禁止
Preferences > Editor > Inspections > Java > Code style issues > Control flow statement without bracesStringの固定値を比較する場合、“固定値“.equals(string)と強制する
Preferences > Editor > Inspections > Java > Code style issues > ‘expression.equals(“literal”)’ rather than “literal”.equals(expression)'条件文を&&や||を使ってシンプルにする
Preferences > Editor > Inspections > Java > Code style issues > ‘if’ statement may be replaced with && or || expression