Enhance the way to get dubbo version (#11574)

* Enhance the way to get dubbo version

* Catch Throwable

* Reimplemented using antrun

* Enable failOnError
This commit is contained in:
Mengyang Tang 2023-02-22 20:26:39 +08:00 committed by GitHub
parent b62883c177
commit 2897c85674
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 79 additions and 5 deletions

View File

@ -100,8 +100,38 @@
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>get-version-infos</id>
<phase>compile</phase>
<configuration>
<failOnError>true</failOnError>
<target>
<property name="version_file"
value="${project.build.outputDirectory}/META-INF/version"/>
<!-- get the current version of dubbo -->
<echo message="revision=${revision}${line.separator}" file="${version_file}"/>
<echo message="git.commit.id=" file="${version_file}" append="true"/>
<!-- get the latest commit id -->
<exec executable="git" output="${version_file}" error=".git.exec.error" append="true"
timeout="3000" failifexecutionfails="false">
<arg line="rev-parse HEAD"/>
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -19,15 +19,19 @@ package org.apache.dubbo.common;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.ClassUtils;
import org.apache.dubbo.common.utils.ConfigUtils;
import org.apache.dubbo.common.utils.StringUtils;
import java.io.IOException;
import java.net.URL;
import java.security.CodeSource;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -46,8 +50,9 @@ public final class Version {
public static final String DEFAULT_DUBBO_PROTOCOL_VERSION = "2.0.2";
// version 1.0.0 represents Dubbo rpc protocol before v2.6.2
public static final int LEGACY_DUBBO_PROTOCOL_VERSION = 10000; // 1.0.0
// Dubbo implementation version, usually is jar version.
private static final String VERSION = getVersion(Version.class, "");
// Dubbo implementation version.
private static String VERSION;
private static String LATEST_COMMIT_ID;
/**
* For protocol compatibility purpose.
@ -61,6 +66,21 @@ public final class Version {
static {
// check if there's duplicated jar
Version.checkDuplicate(Version.class);
// get dubbo version and last commit id
try {
Properties properties =
ConfigUtils.loadProperties(Collections.emptySet(), "META-INF/version");
VERSION = Optional.ofNullable(properties.getProperty("revision"))
.filter(StringUtils::isNotBlank)
.orElseGet(() -> getVersion(Version.class, ""));
LATEST_COMMIT_ID = Optional.ofNullable(properties.getProperty("git.commit.id")).orElse("");
} catch (Throwable e) {
logger.warn(COMMON_UNEXPECTED_EXCEPTION, "", "", "continue the old logic, ignore exception " + e.getMessage(), e);
VERSION = getVersion(Version.class, "");
LATEST_COMMIT_ID = "";
}
}
private Version() {
@ -74,6 +94,10 @@ public final class Version {
return VERSION;
}
public static String getLastCommitId() {
return LATEST_COMMIT_ID;
}
/**
* Compare versions
*

View File

@ -0,0 +1 @@
# This is a placeholder file, the real file will be output when the project compiles

View File

@ -91,4 +91,14 @@ class VersionTest {
Assertions.assertTrue(Version.isRelease263OrHigher("2.6.3"));
Assertions.assertTrue(Version.isRelease263OrHigher("2.6.3.0"));
}
}
@Test
void testGetVersion() {
Assertions.assertEquals("1.0.0", Version.getVersion());
}
@Test
void testGetLastCommitId() {
Assertions.assertEquals("82a29fcd674216fe9bea10b6efef3196929dd7ca", Version.getLastCommitId());
}
}

View File

@ -0,0 +1,3 @@
# This is a test file
revision=1.0.0
git.commit.id=82a29fcd674216fe9bea10b6efef3196929dd7ca

View File

@ -124,6 +124,7 @@
<maven_jacoco_version>0.8.8</maven_jacoco_version>
<maven_flatten_version>1.3.0</maven_flatten_version>
<maven_enforce_version>3.2.1</maven_enforce_version>
<maven_antrun_version>3.1.0</maven_antrun_version>
<arguments />
<checkstyle.skip>true</checkstyle.skip>
<checkstyle_unix.skip>true</checkstyle_unix.skip>
@ -801,6 +802,11 @@
<artifactId>jetty-maven-plugin</artifactId>
<version>${maven_jetty_version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>${maven_antrun_version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>