diff --git a/src/main/java/io/github/bonigarcia/wdm/WebDriverManager.java b/src/main/java/io/github/bonigarcia/wdm/WebDriverManager.java index 6dc5d3822..9ab56a487 100644 --- a/src/main/java/io/github/bonigarcia/wdm/WebDriverManager.java +++ b/src/main/java/io/github/bonigarcia/wdm/WebDriverManager.java @@ -31,6 +31,7 @@ import static io.github.bonigarcia.wdm.config.OperatingSystem.WIN; import static io.github.bonigarcia.wdm.online.Downloader.deleteFile; import static io.github.bonigarcia.wdm.versions.Shell.runAndWait; +import static io.github.bonigarcia.wdm.versions.VersionDetector.getWdmVersion; import static java.lang.Integer.parseInt; import static java.lang.String.valueOf; import static java.lang.System.getenv; @@ -219,6 +220,10 @@ protected abstract List getDriverUrls(String driverVersion) protected String downloadedDriverPath; protected WebDriverManager() { + Optional wdVersion = getWdmVersion(getClass()); + if (wdVersion.equals(wdVersion)) { + log.debug("Using WebDriverManager {}", wdVersion.get()); + } config = new Config(); webDriverList = new CopyOnWriteArrayList<>(); } @@ -1426,7 +1431,6 @@ protected UrlHandler createUrlHandler(String driverVersion) String shortDriverName = getShortDriverName(); UrlHandler urlHandler = new UrlHandler(config(), candidateUrls, driverVersion, shortDriverName, this::buildUrl); - log.trace("All driver URLs: {}", candidateUrls); boolean getLatest = isUnknown(driverVersion); boolean continueSearchingVersion; diff --git a/src/main/java/io/github/bonigarcia/wdm/versions/VersionDetector.java b/src/main/java/io/github/bonigarcia/wdm/versions/VersionDetector.java index 6df83c15e..0f6782af9 100644 --- a/src/main/java/io/github/bonigarcia/wdm/versions/VersionDetector.java +++ b/src/main/java/io/github/bonigarcia/wdm/versions/VersionDetector.java @@ -44,8 +44,13 @@ import java.util.Properties; import java.util.stream.Collectors; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathFactory; + import org.apache.commons.io.IOUtils; import org.slf4j.Logger; +import org.w3c.dom.Document; import io.github.bonigarcia.wdm.config.Config; import io.github.bonigarcia.wdm.config.OperatingSystem; @@ -438,4 +443,65 @@ public boolean isSnap() { return isSnap; } + public static final Optional getWdmVersion(Class clazz) { + try { + String className = clazz.getName(); + String classfileName = "/" + className.replace('.', '/') + ".class"; + URL classfileResource = clazz.getResource(classfileName); + if (classfileResource != null) { + Path absolutePackagePath = Paths.get(classfileResource.toURI()) + .getParent(); + int packagePathSegments = className.length() + - className.replace(".", "").length(); + Path path = absolutePackagePath; + for (int i = 0, segmentsToRemove = packagePathSegments + + 2; i < segmentsToRemove; i++) { + path = path.getParent(); + } + Path pom = path.resolve("pom.xml"); + try (InputStream is = Files.newInputStream(pom)) { + Document doc = DocumentBuilderFactory.newInstance() + .newDocumentBuilder().parse(is); + doc.getDocumentElement().normalize(); + String version = (String) XPathFactory.newInstance() + .newXPath().compile("/project/version") + .evaluate(doc, XPathConstants.STRING); + if (version != null) { + version = version.trim(); + if (!version.isEmpty()) { + return Optional.of(version); + } + } + } + } + } catch (Exception e) { + // Ignore + } + + try (InputStream is = clazz.getResourceAsStream( + "/META-INF/maven/io.github.bonigarcia/webdrivermanger/pom.properties")) { + if (is != null) { + Properties p = new Properties(); + p.load(is); + String version = p.getProperty("version", "").trim(); + if (!version.isEmpty()) { + return Optional.of(version); + } + } + } catch (Exception e) { + // Ignore + } + + String version = null; + Package pkg = clazz.getPackage(); + if (pkg != null) { + version = pkg.getImplementationVersion(); + if (version == null) { + version = pkg.getSpecificationVersion(); + } + } + version = version == null ? "" : version.trim(); + return version.isEmpty() ? Optional.empty() : Optional.of(version); + } + }