[ 
https://issues.apache.org/jira/browse/MJAR-62?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17829780#comment-17829780
 ] 

ASF GitHub Bot commented on MJAR-62:
------------------------------------

slawekjaranowski commented on code in PR #73:
URL: https://github.com/apache/maven-jar-plugin/pull/73#discussion_r1535215589


##########
src/main/java/org/apache/maven/plugins/jar/ToolchainsJdkSpecification.java:
##########
@@ -0,0 +1,93 @@
+/*
+ * 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.jar;
+
+import javax.inject.Named;
+import javax.inject.Singleton;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+
+import org.apache.maven.toolchain.Toolchain;
+import org.codehaus.plexus.util.cli.CommandLineException;
+import org.codehaus.plexus.util.cli.CommandLineUtils;
+import org.codehaus.plexus.util.cli.Commandline;
+
+/**
+ * Component provided JDK specification based on toolchains.
+ */
+@Named
+@Singleton
+class ToolchainsJdkSpecification {
+
+    private final Map<Path, String> cache = new HashMap<>();
+
+    public synchronized Optional<String> getJDKSpecification(Toolchain 
toolchain) {
+        Optional<Path> javaCPath = getJavaCPath(toolchain);
+        return javaCPath.map(path -> cache.computeIfAbsent(path, 
this::getSpecForPath));
+    }
+
+    private Optional<Path> getJavaCPath(Toolchain toolchain) {
+        return 
Optional.ofNullable(toolchain.findTool("javac")).map(Paths::get).map(this::getCanonicalPath);
+    }
+
+    private Path getCanonicalPath(Path path) {
+        try {
+            return path.toRealPath();
+        } catch (IOException e) {
+            if (path.getParent() != null) {
+                return 
getCanonicalPath(path.getParent()).resolve(path.getFileName());
+            } else {
+                throw new UncheckedIOException(e);
+            }
+        }
+    }
+
+    private String getSpecForPath(Path path) {
+        try {
+            Commandline cl = new Commandline(path.toString());
+            cl.createArg().setValue("-version");
+            CommandLineUtils.StringStreamConsumer out = new 
CommandLineUtils.StringStreamConsumer();
+            CommandLineUtils.StringStreamConsumer err = new 
CommandLineUtils.StringStreamConsumer();
+            CommandLineUtils.executeCommandLine(cl, out, err);
+            String version = out.getOutput().trim();
+            if (version.isEmpty()) {
+                version = err.getOutput().trim();
+            }
+            if (version.startsWith("javac ")) {
+                version = version.substring(6);
+                if (version.startsWith("1.")) {
+                    version = version.substring(0, 3);
+                } else {
+                    version = version.substring(0, 2);
+                }
+                return version;
+            } else {
+                return null;
+            }
+        } catch (CommandLineException e) {
+            throw new RuntimeException(e);

Review Comment:
   introduce own class for unchecked exception
   it is used in: `Map.computeIfAbsent` instead of it I need to check map item 
and add new item ... code will be more complicated





> Build-Jdk in Manifest.mf does not reflect which compiler version actually 
> compiled the classes in the jar
> ---------------------------------------------------------------------------------------------------------
>
>                 Key: MJAR-62
>                 URL: https://issues.apache.org/jira/browse/MJAR-62
>             Project: Maven JAR Plugin
>          Issue Type: Bug
>            Reporter: Stefan Magnus Landrø
>            Assignee: Slawomir Jaranowski
>            Priority: Major
>             Fix For: 3.4.0
>
>         Attachments: example-app.zip
>
>
> Manifest.mf does not reflect the version of the compiler that built the jar, 
> but defaults to the version that maven runs under:  Build-Jdk: 
> ${java.version}.
> I believe this makes users uncertain of which compiler was actually used to 
> build the classes.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to