【问题标题】:How to acces POM project name from properties file如何从属性文件访问 POM 项目名称
【发布时间】:2017-01-29 10:13:32
【问题描述】:

我有 logback.properties 文件,其中包含以下属性。

#logging
logging.server=
logging.port=
logging.path=/opt/${project.name}/logs

在我的POM 文件中,我有<name> 标记,它指定了项目名称。我想将此名称注入到上面的lockback 等几个属性文件中。执行上述操作会创建一个名为project.name_IS_UNDEFINED 的文件夹。是否可以访问项目名称以及如何访问?

更新 Ralph 给出了正确答案,但请查看我的评论,因为对于 Spring Boot 应用程序,您需要使用 %project.name% 而不是 ${project.name}

【问题讨论】:

标签: spring maven properties spring-boot


【解决方案1】:

您需要在运行时将 maven 编译时脚本中定义的属性传输到应用程序中。

最简单的方法是使用maven's resource filtering(名字有点误导,不是选择文件的过滤器,是文本/资源文件的属性替换)为了让maven替换${project.name}在你的 logback.properties 文件中。

真的 ${basedir}/src/main/resources

如果您只想为一个文件启用资源过滤(而不是其他文件以防止 maven 替换其他标记,那么您可以使用这个 sn-p:

<resources>
    <!-- enable filtering for logback.properties only -->
    <resource>
        <filtering>false</filtering>
        <directory>${basedir}/src/main/resources</directory>
        <includes>
            <include>**/*</include>
        </includes>
        <excludes>
            <exclude>WHEREVER_LOCATED/logback.properties</exclude>
        </excludes>
    </resource>
    <resource>
        <filtering>true</filtering>
        <directory>${basedir}/src/main/resources</directory>
        <includes>
            <include>WHEREVER_LOCATED/logback.properties</include>
        </includes>
    </resource>
</resources>

【讨论】:

  • 谢谢,拉尔夫。你的回答对我有帮助,因为我错过了过滤:)。但是,由于我正在开发 Spring Boot 应用程序,因此这并没有完全解决我的问题。相反,我不得不使用 %project.name% 看到这里stackoverflow.com/questions/36501017/…
【解决方案2】:
猜你喜欢
  • 2013-01-23
  • 2023-03-29
  • 2018-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-18
  • 1970-01-01
  • 2012-03-31
相关资源
最近更新 更多