Skip to content

Commit

Permalink
Build jar with correct version string automatically (Fixes #280)
Browse files Browse the repository at this point in the history
  • Loading branch information
hierynomus committed Nov 23, 2016
1 parent 1feb7fe commit 256e65d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
9 changes: 9 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/net/schmizz/sshj/DefaultConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions src/test/resources/sshj.properties
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 256e65d

Please sign in to comment.