sam initでjava17環境！

## 2023年4月末でAWSでJava17の環境を用意ができた。

なので、`sam init` でjava17環境を選べるようになった！

<img alt="Java17.jpg" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/83390/6a8f621f-c78a-8ed0-1d56-cf55be7019b3.jpeg" width="300">

生成されたtemplate.yamlに `Runtime: java17` があることがわかる。

![Screenshot 2023-07-09 at 17.41.50.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/83390/5b9651cf-cfcf-f81f-27aa-95204ffa76ae.png)

ローカルではビルドでき `~/.gradlew build` など成功する筈。

```zsh
% java --version
openjdk 17.0.7 2023-04-18
IBM Semeru Runtime Open Edition 17.0.7.0 (build 17.0.7+7)
Eclipse OpenJ9 VM 17.0.7.0 (build openj9-0.38.0, JRE 17 Mac OS X amd64-64-Bit Compressed References 20230418_450 (JIT enabled, AOT enabled)
OpenJ9   - d57d05932
OMR      - 855813495
JCL      - 9d7a231edbc based on jdk-17.0.7+7)


% echo $JAVA_HOME
/Users/ignacio/.sdkman/candidates/java/current

% cd HelloWorldFunction; ./gradlew build; cd ..     
Starting a Gradle Daemon, 2 incompatible Daemons could not be reused, use --status for details

BUILD SUCCESSFUL in 9s
4 actionable tasks: 4 executed

% sam build
...
Build Succeeded

```

## 失敗した場合

gradlew build単独で成功するが sam buildが失敗することがある。

```
% sam build
Starting Build use cache                                                                                                                                                          
Cache is invalid, running build and copying resources for following functions (HelloWorldFunction)                                                                                
Building codeuri: /Users/ignacio/Documents/code/nacho/aws-lambda/assignment-2l/HelloWorldFunction runtime: java17 metadata: {} architecture: x86_64 functions: HelloWorldFunction 
/Users/ignacio/Documents/code/nacho/aws-lambda/assignment-2l/HelloWorldFunction/gradlew is using a JVM with major version 20 which is newer than 17 that is supported by AWS      
Lambda. The compiled function code may not run in AWS Lambda unless the project has been configured to be compatible with Java 17 using 'targetCompatibility' in Gradle.          
Running JavaGradleWorkflow:GradleBuild                                                                                                                                            

Build Failed
Error: JavaGradleWorkflow:GradleBuild - Gradle Failed: FAILURE: Build failed with an exception.

* What went wrong:
Could not open cp_init generic class cache for initialization script '/var/folders/8m/h4r394gd1cg67z5g5whq0cjh0000gn/T/tmp6ee80m4a/lambda-build-init.gradle' (/Users/ignacio/.gradle/caches/7.3.3/scripts/ac0qs36aeafdloj2i01vxlr44).
> BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 64

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 734ms
```

理由 `gradle-wrapper.properties` ファイルに記入されているgradle のバージョンが少し古めだから。 https://services.gradle.org/distributions/ にアクセスし、新めなものを選べばいい　（2023年７月時点では8.1.1は良さそうだ）

```diff
-distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
```


## 参考リンク


https://aws.amazon.com/jp/about-aws/whats-new/2023/04/aws-lambda-java-17/


https://stackoverflow.com/questions/68597899/bug-exception-in-phase-semantic-analysis-in-source-unit-buildscript-unsup/75495859#75495859