Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the method to fetch dubbo version from dubboVersion.properties filled by pom.xml #1834

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions dubbo-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,12 @@
<artifactId>fst</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources/</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
45 changes: 33 additions & 12 deletions dubbo-common/src/main/java/com/alibaba/dubbo/common/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions dubbo-common/src/main/resources/dubboVersion.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dubbo.version=${project.version}
Original file line number Diff line number Diff line change
@@ -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);
}
}