[ https://issues.apache.org/jira/browse/MTOOLCHAINS-49?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17827606#comment-17827606 ]
ASF GitHub Bot commented on MTOOLCHAINS-49: ------------------------------------------- slawekjaranowski commented on code in PR #14: URL: https://github.com/apache/maven-toolchains-plugin/pull/14#discussion_r1526841943 ########## src/main/java/org/apache/maven/plugins/toolchain/jdk/ToolchainDiscoverer.java: ########## @@ -0,0 +1,468 @@ +/* + * 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. + */ +package org.apache.maven.plugins.toolchain.jdk; + +import javax.inject.Named; +import javax.inject.Singleton; + +import java.io.IOException; +import java.io.Reader; +import java.io.Writer; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.apache.maven.toolchain.model.PersistedToolchains; +import org.apache.maven.toolchain.model.ToolchainModel; +import org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Reader; +import org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Writer; +import org.codehaus.plexus.util.xml.Xpp3Dom; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static java.util.Comparator.comparing; +import static org.apache.maven.plugins.toolchain.jdk.SelectJdkToolchainMojo.TOOLCHAIN_TYPE_JDK; + +/** + * Toolchain discoverer service + */ +@Named +@Singleton +public class ToolchainDiscoverer { + + public static final String JAVA = "java."; + public static final String VERSION = "version"; + public static final String RUNTIME_NAME = "runtime.name"; + public static final String RUNTIME_VERSION = "runtime.version"; + public static final String VENDOR = "vendor"; + public static final String VENDOR_VERSION = "vendor.version"; + public static final String[] PROPERTIES = {VERSION, RUNTIME_NAME, RUNTIME_VERSION, VENDOR, VENDOR_VERSION}; + + public static final String CURRENT = "current"; + public static final String ENV = "env"; + public static final String LTS = "lts"; + + public static final List<String> SORTED_PROVIDES = Collections.unmodifiableList( + Arrays.asList(VERSION, RUNTIME_NAME, RUNTIME_VERSION, VENDOR, VENDOR_VERSION, CURRENT, LTS, ENV)); + + public static final String DISCOVERED_TOOLCHAINS_CACHE_XML = ".m2/discovered-toolchains-cache.xml"; + + public static final String JDK_HOME = "jdkHome"; + public static final String JAVA_HOME = "java.home"; + + private static final String COMMA = ","; + public static final String USER_HOME = "user.home"; + + private final Logger log = LoggerFactory.getLogger(getClass()); + + private volatile Map<Path, ToolchainModel> cache; + private volatile boolean cacheModified; + private volatile Set<Path> foundJdks; + + /** + * Build the model for the current JDK toolchain + */ + public Optional<ToolchainModel> getCurrentJdkToolchain() { + Path currentJdkHome = getCanonicalPath(Paths.get(System.getProperty(JAVA_HOME))); + if (hasJavaC(currentJdkHome)) { + // in case the current JVM is not a JDK + return Optional.empty(); + } + ToolchainModel model = new ToolchainModel(); + model.setType(TOOLCHAIN_TYPE_JDK); + Stream.of(PROPERTIES).forEach(k -> { + String v = System.getProperty(JAVA + k); + if (v != null) { + model.addProvide(k.substring(JAVA.length()), v); Review Comment: ``` model.addProvide(k, v); ``` ########## src/main/java/org/apache/maven/plugins/toolchain/jdk/ToolchainDiscoverer.java: ########## @@ -0,0 +1,468 @@ +/* + * 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. + */ +package org.apache.maven.plugins.toolchain.jdk; + +import javax.inject.Named; +import javax.inject.Singleton; + +import java.io.IOException; +import java.io.Reader; +import java.io.Writer; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.apache.maven.toolchain.model.PersistedToolchains; +import org.apache.maven.toolchain.model.ToolchainModel; +import org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Reader; +import org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Writer; +import org.codehaus.plexus.util.xml.Xpp3Dom; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static java.util.Comparator.comparing; +import static org.apache.maven.plugins.toolchain.jdk.SelectJdkToolchainMojo.TOOLCHAIN_TYPE_JDK; + +/** + * Toolchain discoverer service + */ +@Named +@Singleton +public class ToolchainDiscoverer { + + public static final String JAVA = "java."; + public static final String VERSION = "version"; + public static final String RUNTIME_NAME = "runtime.name"; + public static final String RUNTIME_VERSION = "runtime.version"; + public static final String VENDOR = "vendor"; + public static final String VENDOR_VERSION = "vendor.version"; + public static final String[] PROPERTIES = {VERSION, RUNTIME_NAME, RUNTIME_VERSION, VENDOR, VENDOR_VERSION}; + + public static final String CURRENT = "current"; + public static final String ENV = "env"; + public static final String LTS = "lts"; + + public static final List<String> SORTED_PROVIDES = Collections.unmodifiableList( + Arrays.asList(VERSION, RUNTIME_NAME, RUNTIME_VERSION, VENDOR, VENDOR_VERSION, CURRENT, LTS, ENV)); + + public static final String DISCOVERED_TOOLCHAINS_CACHE_XML = ".m2/discovered-toolchains-cache.xml"; + + public static final String JDK_HOME = "jdkHome"; + public static final String JAVA_HOME = "java.home"; + + private static final String COMMA = ","; + public static final String USER_HOME = "user.home"; + + private final Logger log = LoggerFactory.getLogger(getClass()); + + private volatile Map<Path, ToolchainModel> cache; + private volatile boolean cacheModified; + private volatile Set<Path> foundJdks; + + /** + * Build the model for the current JDK toolchain + */ + public Optional<ToolchainModel> getCurrentJdkToolchain() { + Path currentJdkHome = getCanonicalPath(Paths.get(System.getProperty(JAVA_HOME))); + if (hasJavaC(currentJdkHome)) { Review Comment: ``` if (!hasJavaC(currentJdkHome)) ``` > Automatic discovery of JDK toolchains > ------------------------------------- > > Key: MTOOLCHAINS-49 > URL: https://issues.apache.org/jira/browse/MTOOLCHAINS-49 > Project: Maven Toolchains Plugin > Issue Type: Improvement > Reporter: Guillaume Nodet > Assignee: Guillaume Nodet > Priority: Major > -- This message was sent by Atlassian Jira (v8.20.10#820010)