'Compile and execute a JDK preview feature with Maven
With JDK/12 EarlyAccess Build 10, the JEP-325 Switch Expressions has been integrated as a preview feature in the JDK. A sample code for the expressions (as in the JEP as well):
Scanner scanner = new Scanner(System.in);
Day day = Day.valueOf(scanner.next());
switch (day) {
case MONDAY, TUESDAY -> System.out.println("Back to work.") ;
case WEDNESDAY -> System.out.println("Wait for the end of week...") ;
case THURSDAY,FRIDAY -> System.out.println("Plan for the weekend?");
case SATURDAY, SUNDAY -> System.out.println("Enjoy the holiday!");
}
where Day being an enum as
public enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
The Preview Language and VM Features JEP-12 already elaborate how a feature can be enabled during compile and runtime using javac and java.
How can one try out this feature using Maven?
Solution 1:[1]
To enable preview feature you must define --enable-preview in pom.xml under compilerArgs
in below I mention how to enable preview feature with java 13.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>13</release>
<compilerArgs>
--enable-preview
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<argLine>--enable-preview</argLine>
</configuration>
</plugin>
</plugins>
</build>
Solution 2:[2]
Since version 3.10.1 of the Maven Compiler Plugin, there is a dedicated parameter for enabling preview features:
<enablePreview>Set to true to Enable preview language features of the java compiler
- Type:
boolean- Since:
3.10.1- Required:
No- User Property:
maven.compiler.enablePreview- Default:
false
Example:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<release>${java.version}</release>
<enablePreview>true</enablePreview>
</configuration>
</plugin>
Solution 3:[3]
This must be added in the workflow code:
auth:
type: OIDC
See https://cloud.google.com/workflows/docs/calling-run-functions?authuser=1#add_auth_info
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | beatngu13 |
| Solution 3 | user3205999 |
