diff --git a/dubbo-common/pom.xml b/dubbo-common/pom.xml
index 63b15fbe78f..c09aa470a21 100644
--- a/dubbo-common/pom.xml
+++ b/dubbo-common/pom.xml
@@ -70,4 +70,12 @@
fst
+
+
+
+ src/main/resources/
+ true
+
+
+
\ No newline at end of file
diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/Version.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/Version.java
index 97e3a2173dc..09d61eee067 100644
--- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/Version.java
+++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/Version.java
@@ -19,11 +19,15 @@
import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.alibaba.dubbo.common.utils.ClassHelper;
+import com.alibaba.dubbo.common.utils.StringUtils;
+import java.io.IOException;
+import java.io.InputStream;
import java.net.URL;
import java.security.CodeSource;
import java.util.Enumeration;
import java.util.HashSet;
+import java.util.Properties;
import java.util.Set;
/**
@@ -34,6 +38,8 @@ public final class Version {
private static final String DEFAULT_DUBBO_VERSION = "2.0.0";
private static final Logger logger = LoggerFactory.getLogger(Version.class);
private static final String VERSION = getVersion(Version.class, DEFAULT_DUBBO_VERSION);
+ private static final String DUBBO_VERSION_PROPERTIES_PATH = "/dubboVersion.properties";
+ private static final String DUBBO_VERSION_KEY = "dubbo.version";
static {
// check if there's duplicated jar
@@ -47,24 +53,38 @@ public static String getVersion() {
return VERSION;
}
-
- private static boolean hasResource(String path) {
+ /**
+ * get version from dubboVersion.properties filled by pom.xml
+ *
+ * @return
+ */
+ private static String getVersionFromConfigFile() {
+ String version = null;
try {
- return Version.class.getClassLoader().getResource(path) != null;
- } catch (Throwable t) {
- return false;
+ InputStream inputStream = Version.class.getResourceAsStream(DUBBO_VERSION_PROPERTIES_PATH);
+ Properties properties = new Properties();
+ properties.load(inputStream);
+ version = properties.getProperty(DUBBO_VERSION_KEY);
+ } catch (IOException e) {
+ logger.error("return version error " + e.getMessage(), e);
}
+ return version;
}
public static String getVersion(Class> cls, String defaultVersion) {
try {
- // find version info from MANIFEST.MF first
- String version = cls.getPackage().getImplementationVersion();
- if (version == null || version.length() == 0) {
- version = cls.getPackage().getSpecificationVersion();
+ // find version info from dubboVersion.properties
+ String version = getVersionFromConfigFile();
+ if (StringUtils.isNotEmpty(version)) {
+ return version;
}
- if (version == null || version.length() == 0) {
- // guess version fro jar file name if nothing's found from MANIFEST.MF
+ // find version info from MANIFEST.MF first
+ version = cls.getPackage().getImplementationVersion();
+ if (StringUtils.isNotEmpty(version)) {
+ String specificationVersion = cls.getPackage().getSpecificationVersion();
+ return StringUtils.isNotEmpty(specificationVersion) ? specificationVersion : defaultVersion;
+ } else {
+ // guess version from jar file name if nothing's found from from dubboVersion.properties and MANIFEST.MF
CodeSource codeSource = cls.getProtectionDomain().getCodeSource();
if (codeSource == null) {
logger.info("No codeSource for class " + cls.getName() + " when getVersion, use default version " + defaultVersion);
@@ -89,11 +109,12 @@ public static String getVersion(Class> cls, String defaultVersion) {
}
}
version = file;
+ return StringUtils.isNotEmpty(version) ? version : defaultVersion;
}
}
}
// return default version if no version info is found
- return version == null || version.length() == 0 ? defaultVersion : version;
+ return defaultVersion;
} catch (Throwable e) {
// return default version when any exception is thrown
logger.error("return default version, ignore exception " + e.getMessage(), e);
diff --git a/dubbo-common/src/main/resources/dubboVersion.properties b/dubbo-common/src/main/resources/dubboVersion.properties
new file mode 100644
index 00000000000..26d40fe53c4
--- /dev/null
+++ b/dubbo-common/src/main/resources/dubboVersion.properties
@@ -0,0 +1 @@
+dubbo.version=${project.version}
\ No newline at end of file
diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/VersionTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/VersionTest.java
new file mode 100644
index 00000000000..97703de3753
--- /dev/null
+++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/VersionTest.java
@@ -0,0 +1,42 @@
+/*
+ * 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 com.alibaba.dubbo.common;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * version test
+ */
+public class VersionTest {
+
+ public static final String DEFAULT_VERSION = "2.0.0";
+
+ @Test
+ public void testGetVersion() {
+ String version = Version.getVersion();
+ Assert.assertNotNull(version);
+ Assert.assertNotEquals(version, DEFAULT_VERSION);
+ }
+
+ @Test
+ public void testGetClassVersion() {
+ String version = Version.getVersion(this.getClass(), DEFAULT_VERSION);
+ Assert.assertNotNull(version);
+ Assert.assertNotEquals(version, DEFAULT_VERSION);
+ }
+}