AWSでJava開発環境構築する
概要
最近PGする機会がめっきり減ってきたので、コーディング力を維持する(というか成長させる)ために、Javaでも書いてみようと思いました。 せっかくなのでクラウドの統合開発環境の代表格(?)であるAWSを使ってみます。
やること一覧
- Gitでリポジトリ作成
- Cloud9 の環境作成
- READMEをGitにpush
- プログラムを一本push
1. Gitでリポジトリ作成
いつもの画面が出てきます。
2. Cloud9 の環境構築
AWSマネジメントコンソールの検索バーにCloud9と入力
Cloud9が選択肢に出てくるのでクリック
「Create environment」をクリック
名前と概要を書いて「Next step」をクリック
高性能でなくていいですし、デフォルトからセッティングをいじる必要もなさそうと判断して
そのまま「Next step」をクリック
確認画面ですね。Cloud9環境を使用するにあたっての注意事項が書いてあるので、
読んでから「Create environment」をクリック
- 個人でバックアップをとる
- 環境は自分で最新化する
- AWSアカウントを「AWS CloudTrail」にする
- 信頼できるユーザにだけ公開する
数分待ちます。
おまちどーん!できました!
3. READMEをGitにpush
Gitリポジトリに書いてある始め方に沿って進めます。
ec2-user:~/environment $ git init Initialized empty Git repository in /home/ec2-user/environment/.git/ ec2-user:~/environment (master) $ git add README.md ec2-user:~/environment (master) $ git commit -m "first commit" [master (root-commit) 66bbe65] first commit Committer: EC2 Default User <ec2-user@ip-172-31-32-251.us-east-2.compute.internal> Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly: git config --global user.name "Your Name" git config --global user.email you@example.com After doing this, you may fix the identity used for this commit with: git commit --amend --reset-author 1 file changed, 14 insertions(+) create mode 100644 README.md ec2-user:~/environment (master) $
ユーザ登録していないので怒られました。ユーザ登録して引き続きpush 。
ec2-user:~/environment (master) $ git config --global user.name "Mick Danjoh" ec2-user:~/environment (master) $ git config --global user.email *****@gmail.com ec2-user:~/environment (master) $ git branch -M main ec2-user:~/environment (main) $ git branch * main ec2-user:~/environment (main) $ git branch -a * main ec2-user:~/environment (main) $ git remote add origin https://github.com/danjo-m/lemon.git ec2-user:~/environment (main) $ git push -u origin main Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 511 bytes | 255.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://github.com/danjo-m/lemon.git * [new branch] main -> main Branch 'main' set up to track remote branch 'main' from 'origin'. ec2-user:~/environment (main) $
リポジトリにpushできました。
4. プログラムを一本push
まじで何でもないプログラムを作ってpushします。
あらかじめJDKが入っていることを確認。
ec2-user:~/environment (main) $ java -version openjdk version "11.0.10" 2021-01-19 LTS OpenJDK Runtime Environment Corretto-11.0.10.9.1 (build 11.0.10+9-LTS) OpenJDK 64-Bit Server VM Corretto-11.0.10.9.1 (build 11.0.10+9-LTS, mixed mode) ec2-user:~/environment (main) $
いつものHello World
ec2-user:~/environment (main) $ ls README.md Sample1.java ec2-user:~/environment (main) $ javac Sample1.java ec2-user:~/environment (main) $ java Sample1 Hello World! ec2-user:~/environment (main) $
Hello Worldできました。
こいつをpushしておきます。
ec2-user:~/environment (main) $ git add Sample1.java ec2-user:~/environment (main) $ git commit -m "Create first pg" On branch main Your branch is ahead of 'origin/main' by 1 commit. (use "git push" to publish your local commits) Untracked files: .c9/ Sample1.class nothing added to commit but untracked files present ec2-user:~/environment (main) $ git push origin main Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 409 bytes | 204.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://github.com/danjo-m/lemon.git 66bbe65..3b9fb5f main -> main ec2-user:~/environment (main) $
gitignoreやクラスファイルの整理はおいおい。
おまけ:Gitコマンド
Githubで情報提供されていたコマンドのオプションをすっかり忘れていたので、
使ったものだけでも整理。
- git commit -m "comment"
-mで単一コメントを記述する。
対象ファイルを書かない場合はステージされている(addした)全ファイルをコミット。 - git branch -M <branch_name>
「branch_name」のカレントブランチの名前を変更。
-mだと上書き禁止。-Mだと強制上書き。 - git push -u <remote_path> <local_branch>:<remote_branch>
「remote_path」へ「local_branch」を「remote_branch」へpush。
ブランチ名はローカルとリモートが同じ場合は1つに省略可。
-uオプションで、次回からgit push
だけで今回と同じ操作ができる。
「remote_path」は省略名で指定可能。省略名はgit remote -v
で確認。
ec2-user:~/environment (main) $ git remote -v origin https://github.com/danjo-m/lemon.git (fetch) origin https://github.com/danjo-m/lemon.git (push) ec2-user:~/environment (main) $
参考リンク
Gitチートシート
[Git]git branch コマンドの -m と -M オプションの違い
git pushのオプション -u とは
以上!