java 在 Spring 配置文件中访问 maven 项目版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3886753/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:50:54  来源:igfitidea点击:

Access maven project version in Spring config files

javaspringmaven-2mavenwicket

提问by Tauren

I would like to display the currently running version of my web application in the page. The project is based on Maven, Spring, and Wicket.

我想在页面中显示我的 Web 应用程序当前运行的版本。该项目基于 Maven、Spring 和 Wicket。

I'd like to somehow get the value of maven's ${project.version}and use it in my spring XML files, similarly to the way I use the Spring PropertyPlaceholderConfigurer to read a property file for settings that I use in my application.

我想以某种方式获取 maven 的值${project.version}并在我的 spring XML 文件中使用它,类似于我使用 Spring PropertyPlaceholderConfigurer 读取我在应用程序中使用的设置的属性文件的方式。

If I had the maven project.versionavailable as a variable in my Spring config, I could do something like this:

如果我project.version在 Spring 配置中将 Maven用作变量,我可以执行以下操作:

<bean id="applicationBean" class="com.mysite.web.WicketApplication">
<property name="version"><value>${project.version}</value></property>
</bean>

How can I do this?

我怎样才能做到这一点?

回答by Pascal Thivent

You can use Maven filteringas already suggested.

您可以按照已经建议的方式使用 Maven过滤

Or you could just read the pom.propertiesfile created by Maven under META-INF directory directly with Spring:

或者你可以pom.properties直接用 Spring读取META-INF 目录下由 Maven 创建的文件:

<util:properties id="pom" 
     location="classpath:META-INF/groupId/artifactId/pom.properties" />

and use the bean.

并使用豆子。

The only drawback of the later approach is that the pom.propertiesis created at packagephase time (and won't be there during, say, test).

后一种方法的唯一缺点是它pom.properties是在package阶段时间创建的(并且不会在那里,比如说,test)。

回答by drekka

One technique would be to use mavens filtering. You can insert placeholders in resource files like which then get replaced with values from the build during the resource phase.

一种技术是使用 mavens 过滤。您可以在资源文件中插入占位符,然后在资源阶段将其替换为构建中的值。

Look up "How do I filter resource files?" in the Maven getting started guide

查找“如何过滤资源文件?” 在Maven 入门指南中

回答by tzolov

Use the @PropertySource annotation to add the pom.properties file created by Maven in the META-INF directory. Note that the file doesn't exist until the packagephase. To avoid errors during testing set the ignoreResourceNotFound=trueand add a default value on the property being read (e.g. nonebelow). 

使用@PropertySource注解在META-INF目录下添加Maven创建的pom.properties文件。请注意,该文件直到打包阶段才存在。为了避免在测试过程中出现错误,设置ignoreResourceNotFound=true并在正在读取的属性上添加一个默认值(例如下面没有)。 

@Service
@PropertySource(value = "classpath:META-INF/maven/io.pivotal.poc.tzolov/hawq-rest-server/pom.properties", ignoreResourceNotFound=true)
public class MyClass {
    private String applicationPomVersion;

    @Autowired
    public MyClass(@Value("${version:none}") String applicationVersion ) {
        this.applicationPomVersion = applicationVersion;
    }

    public String getApplicationPomVersion() {
        return this.applicationPomVersion;
    }
}

回答by Carlos

I think the right way of gather application version is the one explained in this thread: https://stackoverflow.com/a/2713013/840635through getClass().getPackage().getImplementationVersion().

我认为收集应用程序版本的正确方法是在此线程中解释的方法:https: //stackoverflow.com/a/2713013/840635getClass().getPackage().getImplementationVersion().

回答by Mond Raymond

We deploy property files outside of the web app. The files can then be filtered at deployment time.

我们在 Web 应用程序之外部署属性文件。然后可以在部署时过滤这些文件。

If using Jetty one can put the file under $JETTY_HOME/resources or use the extraClassPath feature to load the property file at runtime.

如果使用 Jetty 可以将文件放在 $JETTY_HOME/resources 下或使用 extraClassPath 功能在运行时加载属性文件。