/
set-compiler-release.apt.vm
142 lines (124 loc) · 4.22 KB
/
set-compiler-release.apt.vm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
------
Setting the --release of the Java Compiler
------
Mahmoud Anouti
------
2019-12-20
------
~~ Licensed to the Apache Software Foundation (ASF) under one
~~ or more contributor license agreements. See the NOTICE file
~~ distributed with this work for additional information
~~ regarding copyright ownership. The ASF licenses this file
~~ to you under the Apache License, Version 2.0 (the
~~ "License"); you may not use this file except in compliance
~~ with the License. You may obtain a copy of the License at
~~
~~ http://www.apache.org/licenses/LICENSE-2.0
~~
~~ Unless required by applicable law or agreed to in writing,
~~ software distributed under the License is distributed on an
~~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~~ KIND, either express or implied. See the License for the
~~ specific language governing permissions and limitations
~~ under the License.
~~ NOTE: For help with the syntax of this file, see:
~~ http://maven.apache.org/doxia/references/apt-format.html
Setting the <<<--release>>> of the Java Compiler
Starting JDK 9, the <<<javac>>> executable can accept the <<<--release>>>
option to specify against which Java SE release you want to build the project.
For example, you have JDK 11 installed and used by Maven, but you want to
build the project against Java 8.
The <<<--release>>> option ensures that the code is compiled following the
rules of the programming language of the specified release, and that generated
classes target the release as well as the public API of that release. This
means that, unlike the
{{{../examples/set-compiler-source-and-target.html}<<<-source>>> and <<<-target>>> options}},
the compiler will detect and generate an error when using APIs that don't exist
in previous releases of Java SE.
Since version 3.6 of the Compiler Plugin, this option can be provided either
via a property:
+-----
<project>
[...]
<properties>
<maven.compiler.release>8</maven.compiler.release>
</properties>
[...]
</project>
+-----
or by configuring the plugin directly:
+-----
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${project.version}</version>
<configuration>
<release>8</release>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
+-----
<<Note:>> The value in the <<<release>>> parameter follows the new version naming scheme adopted
since Java 9. As such, the release number does not start with 1.x anymore. Also note that the
supported <<<release>>> targets include the release of the currently used JDK plus a limited number
of previous releases.
* Usage on JDK 8
The <<<--release>>> option is not supported using JDK 8. To enable a project that targets Java 8
to be built using JDK 8 and also JDK 9 or later requires the conditional usage of the
<<<--release>>> option.
Conditional parametrization is required for the Compiler Plugin version below <<<3.13.0>>>
or compilerId different that <<<javac>>>.
This may be done through the use of a profile:
+-----
<project>
[...]
<profiles>
[...]
<profile>
<id>set-compiler-release</id>
<activation>
<jdk>[9,)</jdk>
</activation>
<properties>
<maven.compiler.release>8</maven.compiler.release>
</properties>
</profile>
[...]
</profiles>
[...]
</project>
+-----
Since version <<<3.13.0>>> of the Compiler Plugin together with the default <<<javac>>> compilerId you don't need conditional parametrisation of <<<release>>>.
The <<<release>>> parameter will only be effective for Java 9 or above, otherwise the <<<source>>> and <<<target>>> will be passed to the compiler.
So you can simply configure as:
+-----
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${project.version}</version>
<configuration>
<release>8</release>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
+-----