28
28
import org .gradle .api .Project ;
29
29
import org .gradle .api .logging .Logger ;
30
30
import org .gradle .api .logging .Logging ;
31
- import org .gradle .api .model .ObjectFactory ;
32
- import org .gradle .api .provider .Provider ;
33
31
import org .gradle .api .provider .ProviderFactory ;
34
32
import org .gradle .internal .jvm .Jvm ;
35
- import org .gradle .jvm .toolchain .JavaInstallation ;
36
- import org .gradle .jvm .toolchain .JavaInstallationRegistry ;
33
+ import org .gradle .internal .jvm .inspection .JvmInstallationMetadata ;
34
+ import org .gradle .internal .jvm .inspection .JvmMetadataDetector ;
35
+ import org .gradle .jvm .toolchain .internal .InstallationLocation ;
36
+ import org .gradle .jvm .toolchain .internal .SharedJavaInstallationRegistry ;
37
37
import org .gradle .util .GradleVersion ;
38
38
39
39
import javax .inject .Inject ;
50
50
import java .nio .file .Paths ;
51
51
import java .time .ZoneOffset ;
52
52
import java .time .ZonedDateTime ;
53
- import java .util .ArrayList ;
54
53
import java .util .Arrays ;
55
54
import java .util .HashMap ;
56
55
import java .util .Iterator ;
@@ -68,14 +67,18 @@ public class GlobalBuildInfoPlugin implements Plugin<Project> {
68
67
private static final String DEFAULT_VERSION_JAVA_FILE_PATH = "server/src/main/java/org/elasticsearch/Version.java" ;
69
68
private static Integer _defaultParallel = null ;
70
69
71
- private final JavaInstallationRegistry javaInstallationRegistry ;
72
- private final ObjectFactory objects ;
70
+ private final SharedJavaInstallationRegistry javaInstallationRegistry ;
71
+ private final JvmMetadataDetector metadataDetector ;
73
72
private final ProviderFactory providers ;
74
73
75
74
@ Inject
76
- public GlobalBuildInfoPlugin (JavaInstallationRegistry javaInstallationRegistry , ObjectFactory objects , ProviderFactory providers ) {
75
+ public GlobalBuildInfoPlugin (
76
+ SharedJavaInstallationRegistry javaInstallationRegistry ,
77
+ JvmMetadataDetector metadataDetector ,
78
+ ProviderFactory providers
79
+ ) {
77
80
this .javaInstallationRegistry = javaInstallationRegistry ;
78
- this .objects = objects ;
81
+ this .metadataDetector = metadataDetector ;
79
82
this .providers = providers ;
80
83
}
81
84
@@ -105,8 +108,8 @@ public void apply(Project project) {
105
108
params .setRuntimeJavaHome (runtimeJavaHome );
106
109
params .setRuntimeJavaVersion (determineJavaVersion ("runtime java.home" , runtimeJavaHome , minimumRuntimeVersion ));
107
110
params .setIsRutimeJavaHomeSet (Jvm .current ().getJavaHome ().equals (runtimeJavaHome ) == false );
108
- params .setRuntimeJavaDetails (getJavaInstallation (runtimeJavaHome ).getImplementationName ());
109
- params .setJavaVersions (getAvailableJavaVersions (minimumCompilerVersion ));
111
+ params .setRuntimeJavaDetails (getJavaInstallation (runtimeJavaHome ).getDisplayName ());
112
+ params .setJavaVersions (getAvailableJavaVersions ());
110
113
params .setMinimumCompilerVersion (minimumCompilerVersion );
111
114
params .setMinimumRuntimeVersion (minimumRuntimeVersion );
112
115
params .setGradleJavaVersion (Jvm .current ().getJavaVersion ());
@@ -146,20 +149,22 @@ private void logGlobalBuildInfo() {
146
149
final String osVersion = System .getProperty ("os.version" );
147
150
final String osArch = System .getProperty ("os.arch" );
148
151
final Jvm gradleJvm = Jvm .current ();
149
- final String gradleJvmDetails = getJavaInstallation (gradleJvm .getJavaHome ()). getImplementationName ( );
150
-
152
+ JvmInstallationMetadata gradleJvmMetadata = metadataDetector . getMetadata (gradleJvm .getJavaHome ());
153
+ final String gradleJvmVendorDetails = gradleJvmMetadata . getVendor (). getDisplayName ();
151
154
LOGGER .quiet ("=======================================" );
152
155
LOGGER .quiet ("Elasticsearch Build Hamster says Hello!" );
153
156
LOGGER .quiet (" Gradle Version : " + GradleVersion .current ().getVersion ());
154
157
LOGGER .quiet (" OS Info : " + osName + " " + osVersion + " (" + osArch + ")" );
155
158
if (BuildParams .getIsRuntimeJavaHomeSet ()) {
156
- String runtimeJvmDetails = getJavaInstallation (BuildParams .getRuntimeJavaHome ()).getImplementationName ();
157
- LOGGER .quiet (" Runtime JDK Version : " + BuildParams .getRuntimeJavaVersion () + " (" + runtimeJvmDetails + ")" );
159
+ final String runtimeJvmVendorDetails = metadataDetector .getMetadata (BuildParams .getRuntimeJavaHome ())
160
+ .getVendor ()
161
+ .getDisplayName ();
162
+ LOGGER .quiet (" Runtime JDK Version : " + BuildParams .getRuntimeJavaVersion () + " (" + runtimeJvmVendorDetails + ")" );
158
163
LOGGER .quiet (" Runtime java.home : " + BuildParams .getRuntimeJavaHome ());
159
- LOGGER .quiet (" Gradle JDK Version : " + gradleJvm .getJavaVersion () + " (" + gradleJvmDetails + ")" );
164
+ LOGGER .quiet (" Gradle JDK Version : " + gradleJvm .getJavaVersion () + " (" + gradleJvmVendorDetails + ")" );
160
165
LOGGER .quiet (" Gradle java.home : " + gradleJvm .getJavaHome ());
161
166
} else {
162
- LOGGER .quiet (" JDK Version : " + gradleJvm .getJavaVersion () + " (" + gradleJvmDetails + ")" );
167
+ LOGGER .quiet (" JDK Version : " + gradleJvm .getJavaVersion () + " (" + gradleJvmVendorDetails + ")" );
163
168
LOGGER .quiet (" JAVA_HOME : " + gradleJvm .getJavaHome ());
164
169
}
165
170
LOGGER .quiet (" Random Testing Seed : " + BuildParams .getTestSeed ());
@@ -168,8 +173,8 @@ private void logGlobalBuildInfo() {
168
173
}
169
174
170
175
private JavaVersion determineJavaVersion (String description , File javaHome , JavaVersion requiredVersion ) {
171
- JavaInstallation installation = getJavaInstallation (javaHome );
172
- JavaVersion actualVersion = installation .getJavaVersion ();
176
+ InstallationLocation installation = getJavaInstallation (javaHome );
177
+ JavaVersion actualVersion = metadataDetector . getMetadata ( installation .getLocation ()). getLanguageVersion ();
173
178
if (actualVersion .isCompatibleWith (requiredVersion ) == false ) {
174
179
throwInvalidJavaHomeException (
175
180
description ,
@@ -182,46 +187,33 @@ private JavaVersion determineJavaVersion(String description, File javaHome, Java
182
187
return actualVersion ;
183
188
}
184
189
185
- private JavaInstallation getJavaInstallation (File javaHome ) {
186
- JavaInstallation installation ;
187
- if (isCurrentJavaHome (javaHome )) {
188
- installation = javaInstallationRegistry .getInstallationForCurrentVirtualMachine ().get ();
189
- } else {
190
- installation = javaInstallationRegistry .installationForDirectory (objects .directoryProperty ().fileValue (javaHome )).get ();
191
- }
192
-
193
- return installation ;
190
+ private InstallationLocation getJavaInstallation (File javaHome ) {
191
+ return javaInstallationRegistry .listInstallations ()
192
+ .stream ()
193
+ .filter (installationLocation -> isSameFile (javaHome , installationLocation ))
194
+ .findFirst ()
195
+ .get ();
194
196
}
195
197
196
- private List <JavaHome > getAvailableJavaVersions (JavaVersion minimumCompilerVersion ) {
197
- final List <JavaHome > javaVersions = new ArrayList <>();
198
- for (int v = 8 ; v <= Integer .parseInt (minimumCompilerVersion .getMajorVersion ()); v ++) {
199
- int version = v ;
200
- String javaHomeEnvVarName = getJavaHomeEnvVarName (Integer .toString (version ));
201
- if (System .getenv (javaHomeEnvVarName ) != null ) {
202
- File javaHomeDirectory = new File (findJavaHome (Integer .toString (version )));
203
- Provider <JavaInstallation > javaInstallationProvider = javaInstallationRegistry .installationForDirectory (
204
- objects .directoryProperty ().fileValue (javaHomeDirectory )
205
- );
206
- JavaHome javaHome = JavaHome .of (version , providers .provider (() -> {
207
- int actualVersion = Integer .parseInt (javaInstallationProvider .get ().getJavaVersion ().getMajorVersion ());
208
- if (actualVersion != version ) {
209
- throwInvalidJavaHomeException ("env variable " + javaHomeEnvVarName , javaHomeDirectory , version , actualVersion );
210
- }
211
- return javaHomeDirectory ;
212
- }));
213
- javaVersions .add (javaHome );
214
- }
198
+ private boolean isSameFile (File javaHome , InstallationLocation installationLocation ) {
199
+ try {
200
+ return Files .isSameFile (installationLocation .getLocation ().toPath (), javaHome .toPath ());
201
+ } catch (IOException ioException ) {
202
+ throw new UncheckedIOException (ioException );
215
203
}
216
- return javaVersions ;
217
204
}
218
205
219
- private static boolean isCurrentJavaHome (File javaHome ) {
220
- try {
221
- return Files .isSameFile (javaHome .toPath (), Jvm .current ().getJavaHome ().toPath ());
222
- } catch (IOException e ) {
223
- throw new UncheckedIOException (e );
224
- }
206
+ /**
207
+ * We resolve all available java versions using auto detected by gradles tool chain
208
+ * To make transition more reliable we only take env var provided installations into account for now
209
+ */
210
+ private List <JavaHome > getAvailableJavaVersions () {
211
+ return javaInstallationRegistry .listInstallations ().stream ().map (installationLocation -> {
212
+ File installationDir = installationLocation .getLocation ();
213
+ JvmInstallationMetadata metadata = metadataDetector .getMetadata (installationDir );
214
+ int actualVersion = Integer .parseInt (metadata .getLanguageVersion ().getMajorVersion ());
215
+ return JavaHome .of (actualVersion , providers .provider (() -> installationDir ));
216
+ }).collect (Collectors .toList ());
225
217
}
226
218
227
219
private static String getTestSeed () {
0 commit comments