0
我編寫了一個程序來獲取包的版本號。它在Intellij中運行良好。但是當我在命令行中運行這個jar文件時,返回值爲null。爲什麼getPackage()。getSpecificationVersion()在命令行中返回null
我的Maven項目
<groupId>com.client.version</groupId>
<artifactId>version-specification</artifactId>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<Build-Number>${project.version}</Build-Number>
<Timestamp>yyyy-MM-dd HH:mm:ss</Timestamp>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
這個項目只包含一個類:獲得版本號
package com.client.version;
public class ClientVersion {
public ClientVersion() {
}
public static String getCurrentVersion() {
return ClientVersion.class.getPackage().getSpecificationVersion();
}
}
我創造了另一個項目
<groupId>com.client.test</groupId>
<artifactId>test-version</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.client.version</groupId>
<artifactId>version-specification</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
並稱爲getCurrentVersion
方法。
public class test {
public static void main(String[] args) {
System.out.println(ClientVersion.getCurrentVersion());
}
}
當我在IntelliJ中運行此程序時,輸出爲1.0
。 但是當我打包一個jar文件並在命令行 java -cp target/test-version-1.0-SNAPSHOT.jar test.test
中運行此主方法時,輸出爲null
,爲什麼?