2014-10-26 33 views
0

我想在我的应用中指定一些系统属性(在编译时确定)。在Spring Boot Plugin中定义系统属性

我现在用的是弹簧启动Maven插件编译

目前,根据这个问题:Specify system property to Maven project 我尝试以下设置(然而,这并不工作,因为它是一个不同的插件)

<plugins> 
     <plugin> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-maven-plugin</artifactId> 
      <configuration> 
       <mainClass>application.boot.AppStarter</mainClass> 
       <systemProperties> 
        <systemProperty> 
         <name>application.version</name> 
         <value>${application.version}</value> 
        </systemProperty> 
        <systemProperty> 
         <name>release.date</name> 
         <value>${timestamp}</value> 
        </systemProperty> 
       </systemProperties> 
      </configuration> 
      <executions> 
       <execution> 
        <goals> 
         <goal>repackage</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 

如何指定此插件中的属性?

+0

spring-boot-maven-plugin只是创建一个可执行的jar。你想用系统属性做什么?你的意思是说当你执行生成的jar时你需要在属性文件中提供一些属性?重新打包不支持任何systemProperties参数,如http://docs.spring.io/spring-boot/docs/1.2.0.BUILD-SNAPSHOT/maven-plugin/repackage-mojo.html – coderplus 2014-10-26 15:09:01

+0

我想要两件事。 1)Hermes.version maven pom.xml属性和2)构建jar的日期可以在我的代码中访问。如果它们作为系统属性,我可以使用System.getProperty(“”)访问它们 – mangusbrother 2014-10-26 15:46:19

回答

1

您添加的Java系统属性只能通过它们添加到的进程访问。即使您在Maven构建过程中设法添加了一些系统属性,构建完成后也不会再存在。

如果您将jar发送给其他人,会发生什么情况。您如何预期这些属性可用?

解决方案

请参阅本post,看看如何在运行时 访问artifactId和version以类似的方式,你可以添加时间戳条目以及到src/main/resources/project.properties

buildTimestamp=${timestamp} 

timestamp不是预定义的属性,如project.versionproject.artifactId。因此,您必须设置从Maven属性${maven.build.timestamp}中提取时间戳,并将其设置为您的timestamp属性的值。这已在this question中得到解答。

+0

虽然它正在尝试成功读取属性,但我得到了:无法解析占位符'property.name'字符串值'$ { property.name}' – mangusbrother 2014-10-26 17:01:24

+0

类似的东西? http://stackoverflow.com/questions/20244696/could-not-resolve-placeholder-in-string-value – coderplus 2014-10-26 17:10:45

+0

我的属性正在被读取,就好像我把它们作为正常值读取成功。如果我使用$ {},但它不会 – mangusbrother 2014-10-26 17:40:43

相关问题