package io.quarkus.cli.core;

import java.io.BufferedReader;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.util.Properties;

import io.smallrye.common.classloader.ClassPathUtils;
import picocli.CommandLine;

public class QuarkusCliVersion implements CommandLine.IVersionProvider {

    private static String version;

    public static String version() {
        if (version != null) {
            return version;
        }
        final URL quarkusPropsUrl = Thread.currentThread().getContextClassLoader().getResource("quarkus.properties");
        if (quarkusPropsUrl == null) {
            throw new RuntimeException("Failed to locate quarkus.properties on the classpath");
        }
        final Properties props = new Properties();
        ClassPathUtils.consumeAsPath(quarkusPropsUrl, p -> {
            try (BufferedReader reader = Files.newBufferedReader(p)) {
                props.load(reader);
            } catch (IOException e) {
                throw new RuntimeException("Failed to load quarkus.properties", e);
            }
        });
        version = props.getProperty("quarkus-core-version");
        if (version == null) {
            throw new RuntimeException("Failed to locate quarkus-core-version property in the bundled quarkus.properties");
        }
        return version;
    }

    @Override
    public String[] getVersion() throws Exception {
        return new String[] { version() };
    }

}

@@ -451,32 +451,84 @@
 rsion;%0A        %7D
+%0A%0A        final Properties props = new Properties();
 %0A        final U
@@ -537,24 +537,29 @@
  quarkusProp
+ertie
 sUrl = Threa
@@ -654,24 +654,29 @@
 (quarkusProp
+ertie
 sUrl == null
@@ -785,24 +785,25 @@
       %7D%0A
+%0A
         
 final Pr
@@ -798,59 +798,242 @@
     
-final Properties props = new Properties();%0A
+// we have a special case for file and jar as using getResourceAsStream() on Windows might cause file locks%0A        if (%22file%22.equals(quarkusPropertiesUrl.getProtocol()) %7C%7C %22jar%22.equals(quarkusPropertiesUrl.getProtocol())) %7B%0A 
         
 Clas
@@ -1028,16 +1028,19 @@
         
+   
 ClassPat
@@ -1071,16 +1071,21 @@
 rkusProp
+ertie
 sUrl, p 
@@ -1101,16 +1101,20 @@
         
+  
+  
 try (Buf
@@ -1180,16 +1180,20 @@
         
+    
 props.lo
@@ -1216,16 +1216,20 @@
         
+   
+ 
 %7D catch 
@@ -1254,32 +1254,36 @@
                 
+    
 throw new Runtim
@@ -1334,16 +1334,330 @@
 s%22, e);%0A
+                %7D%0A            %7D);%0A        %7D else %7B%0A            try %7B%0A                props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(%22quarkus.properties%22));%0A            %7D catch (IOException e) %7B%0A                throw new IllegalStateException(%22Failed to load quarkus.properties%22, e);%0A
         
@@ -1663,34 +1663,33 @@
      %7D%0A        %7D
-);
+%0A
 %0A        version
@@ -1891,32 +1891,33 @@
 es%22);%0A        %7D%0A
+%0A
         return v

Simplify loading of Quarkus CLI version