eclipse 如何在运行Tomcat时获取包版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14934299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get package version at running Tomcat?
提问by Paul Verest
How to get package version at running Tomcat?
如何在运行Tomcat时获取包版本?
I try getClass().getPackage().getImplementationVersion() but this always returns null
.
I guess it is because my war wasn't yet packaged and Tomcat executes .classes (aka exploded war).
META-INF\MANIFEST.MF
is present in final war, but not in target\<project.name>\META-INF
folder
我尝试 getClass().getPackage().getImplementationVersion() 但这总是返回null
. 我想这是因为我的War还没有打包,Tomcat 执行 .classes(又名爆炸War)。
META-INF\MANIFEST.MF
存在于最终War中,但不在target\<project.name>\META-INF
文件夹中
Package objects contain version information about the implementation and specification of a Java package. This versioning information is retrieved and made available by the ClassLoader instance that loaded the class(es). Typically, it is stored in the manifest that is distributed with the classes.
包对象包含有关 Java 包的实现和规范的版本信息。此版本信息由加载类的 ClassLoader 实例检索并提供。通常,它存储在与类一起分发的清单中。
Related is Get Maven artifact version at runtime
UPDATE. Before I have already added configuration for war build. But when running Tomcat from Eclipse, I get null
.
更新。在我已经为War构建添加配置之前。但是当从 Eclipse 运行 Tomcat 时,我得到null
.
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
<!-- this actually moves classes from \WEB-INF\classes to new jar
<archiveClasses>true</archiveClasses>
-->
</configuration>
</plugin>
回答by Paul Verest
Solved by
1) Creating META-INF\MANIFEST.MF in webapp folder with mvn war:manifest
2) Coding
通过
1) 在 webapp 文件夹中创建 META-INF\MANIFEST.MF 解决mvn war:manifest
2) 编码
version = getClass().getPackage().getImplementationVersion();
if (version==null) {
Properties prop = new Properties();
try {
prop.load(getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF"));
version = prop.getProperty("Implementation-Version");
} catch (IOException e) {
logger.error(e.toString());
}
}
logger.info("Starting App version "+version);
Thanks to @javadude answer at How do I read the manifest file for a webapp running in apache tomcat?
感谢@javadude 在如何读取在 apache tomcat 中运行的 web 应用程序的清单文件的回答?
回答by Stephen C
The normal source of a package's version / title / vendor information is attributes in the JAR file manifest. See the JAR File Specification.
包的版本/标题/供应商信息的正常来源是 JAR 文件清单中的属性。请参阅JAR 文件规范。
If the getters return null
that means that the corresponding attribute has not been specified. (And that is normal for regular JAR files too.)
如果 getter 返回null
,则表示尚未指定相应的属性。(这对于常规 JAR 文件也是正常的。)
Yes, it is related to the linked question. The accepted answer describes what to do to tell Maven to add some Maven version details to a JAR manifest so that you can access them at runtime using the Package
API.
是的,它与链接的问题有关。接受的答案描述了如何告诉 Maven 将一些 Maven 版本详细信息添加到 JAR 清单,以便您可以在运行时使用Package
API访问它们。
How to get package version at running Tomcat?
如何在运行Tomcat时获取包版本?
You need to create JAR files, and put the version info into the respective JAR manifests. Apparently you can also do this at the WAR file level.
您需要创建 JAR 文件,并将版本信息放入相应的 JAR 清单中。显然,您也可以在 WAR 文件级别执行此操作。
Reference: "Deploy: War File Versioning and Manifest Reader"by Fred Puls.
参考:“部署:War文件版本控制和清单阅读器”,作者 Fred Puls。