代码之家  ›  专栏  ›  技术社区  ›  Tauren

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

  •  5
  • Tauren  · 技术社区  · 15 年前

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

    ${project.version} 在我的spring XML文件中使用它,类似于我使用springpropertyplaceholderconfig来读取我在应用程序中使用的设置的属性文件。

    project.version

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

    我该怎么做?

    5 回复  |  直到 15 年前
        1
  •  11
  •   Pascal Thivent    15 年前

    你可以用Maven filtering 如前所述。

    pom.properties Maven在META-INF目录下直接用Spring创建的文件:

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

    用豆子。

    聚甲醛性能 package 相位时间(比如说, test

        2
  •  7
  •   Wesley Womack drekka    11 年前

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

    Maven getting started guide

        3
  •  1
  •   florian h    9 年前

    包裹 阶段。为了避免测试过程中出现错误,请设置 ignoreResourceNotFound=真 下面)。

    @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;
        }
    }
    
        4
  •  0
  •   Mond Raymond    14 年前

    如果使用Jetty,可以将文件放在$Jetty_HOME/resources下,或者使用extraClassPath特性在运行时加载属性文件。

        5
  •  0
  •   Community Mohan Dere    8 年前

    https://stackoverflow.com/a/2713013/840635 getClass().getPackage().getImplementationVersion() .

    推荐文章