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
,为什么?