如何从属性文件中访问POM项目名称

问题描述 投票:0回答:3

我有

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
3个回答
2
投票

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

最简单的方法就是使用maven的资源过滤(名字有点误导,不是选择文件的过滤器,而是文本/资源文件的属性替换),让maven替换

${project.name}
在你的 logback.properties 文件中。

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

如果您只想为一个文件启用资源过滤(而不是为其他文件启用资源过滤,以防止 Maven 替换其他标记,那么您可以使用以下代码片段:

<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>


0
投票

您可以使用

@project.name@

以与 application.properties 中相同的方式进行尝试
logging.path=/opt/@project.name@/logs
© www.soinside.com 2019 - 2024. All rights reserved.