在Eclipse中运行时从pom获取maven项目版本和工件ID
当我遇到这个问题时,我正在查找如何从maven pom或manifest获取应用程序名称(工件ID)和版本。 在运行时获取Maven工件版本 。
当我打包项目时,上面的工作对我来说,但是当我尝试使用eclipse运行程序时,我似乎无法工作。 我在构建时尝试使用.properties方法,因为我认为这不依赖于包,但我仍然没有得到结果。 如果有人有这个问题的想法或解决方案,将不胜感激。
我的最后一次尝试如下。 这在打包(使用)时使用清单,并在eclipse中运行时尝试获取.properties文件。
String appVersion = getClass().getPackage().getImplementationVersion(); if(appVersion == null || "".equals(appVersion)) { appVersion = Glob.getString(appVersion); if(appVersion == null || "".equals(appVersion)) { System.exit(0); } }
创建属性文件
src/main/resources/project.properties
以下内容
version=${project.version} artifactId=${project.artifactId}
现在打开maven资源过滤
src/main/resources true
以便将此文件处理成
target/classes/project.properties
与此类似的一些内容
version=1.5 artifactId=my-artifact
现在你可以阅读这个属性文件来获得你想要的东西,这应该每次都有效。
final Properties properties = new Properties(); properties.load(this.getClassLoader().getResourceAsStream("project.properties")); System.out.println(properties.getProperty("version")); System.out.println(properties.getProperty("artifactId"));