diff --git a/build.gradle b/build.gradle index af21e307c..903b4b887 100644 --- a/build.gradle +++ b/build.gradle @@ -64,6 +64,15 @@ if (JavaVersion.current().isJava8Compatible()) { } } +task writeSshjVersionProperties << { + project.file("${project.buildDir}/resources/main").mkdirs() + project.file("${project.buildDir}/resources/main/sshj.properties").withWriter { w -> + w.append("sshj.version=${version}") + } +} + +jar.dependsOn writeSshjVersionProperties + jar { manifest { instruction "Bundle-Description", "SSHv2 library for Java" diff --git a/src/main/java/net/schmizz/sshj/DefaultConfig.java b/src/main/java/net/schmizz/sshj/DefaultConfig.java index 9f3573bf2..03fd2cabf 100644 --- a/src/main/java/net/schmizz/sshj/DefaultConfig.java +++ b/src/main/java/net/schmizz/sshj/DefaultConfig.java @@ -38,10 +38,8 @@ import net.schmizz.sshj.userauth.keyprovider.PuTTYKeyFile; import org.slf4j.Logger; -import java.util.Arrays; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; +import java.io.IOException; +import java.util.*; /** * A {@link net.schmizz.sshj.Config} that is initialized as follows. Items marked with an asterisk are added to the config only if @@ -68,13 +66,11 @@ public class DefaultConfig extends ConfigImpl { - private static final String VERSION = "SSHJ_0_17_2"; - private Logger log; public DefaultConfig() { setLoggerFactory(LoggerFactory.DEFAULT); - setVersion(VERSION); + setVersion(readVersionFromProperties()); final boolean bouncyCastleRegistered = SecurityUtils.isBouncyCastleRegistered(); initKeyExchangeFactories(bouncyCastleRegistered); initRandomFactory(bouncyCastleRegistered); @@ -86,6 +82,18 @@ public DefaultConfig() { setKeepAliveProvider(KeepAliveProvider.HEARTBEAT); } + private String readVersionFromProperties() { + try { + Properties properties = new Properties(); + properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("sshj.properties")); + String property = properties.getProperty("sshj.version"); + return "SSHJ_" + property.replace('-', '_'); // '-' is a disallowed character, see RFC-4253#section-4.2 + } catch (IOException e) { + log.error("Could not read the sshj.properties file, returning an 'unknown' version as fallback."); + return "SSHJ_VERSION_UNKNOWN"; + } + } + @Override public void setLoggerFactory(LoggerFactory loggerFactory) { super.setLoggerFactory(loggerFactory); diff --git a/src/test/resources/sshj.properties b/src/test/resources/sshj.properties new file mode 100644 index 000000000..d60e1ce6d --- /dev/null +++ b/src/test/resources/sshj.properties @@ -0,0 +1,17 @@ +# +# Copyright (C)2009 - SSHJ Contributors +# +# Licensed 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. +# + +sshj.version=0.18.0