【问题标题】:Spring and maven how to put properties file outside jar? [duplicate]Spring和maven如何将属性文件放在jar之外? [复制]
【发布时间】:2015-06-27 20:07:19
【问题描述】:

我正在使用 Spring、Logback 和 maven。我想将我的所有设置都放在我用 maven 构建的 jar 之外的属性中。 所以我从 Logback.xml 中移动了所有设置。现在看起来像:

<configuration>

    <property resource="application.properties" />

    <timestamp key="byDate" datePattern="yyyyMMdd"/>

    <!-- Send messages to System.out - CONSOLE -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
        </encoder>
        <withJansi>true</withJansi>
    </appender>

    <!-- Send messages to a file -->
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>${logging.path}/${spring.application.name}-${byDate}.log</file>
        <append>true</append>
        <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="${logging.level}">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>

</configuration>

我的 application.properties 文件是:

#Spring settings
spring.application.name=MyApp
server.port=8087

# Logging settings
logging.level=INFO
logging.path=/Users/...some path.../logs/

我把这个放到豆子上:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="file:application.properties"/>
    </bean>

并从使用插件的 maven 构建中排除 application.properties:

<plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <copy todir="target" overwrite="true">
                                    <fileset dir="src/main/resources/">
                                        <include name="*.properties"/>
                                    </fileset>
                                </copy>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

但这对我不起作用。日志文件名松散的 datePattern 并且我在属性文件中所做的任何更改都没有改变。怎么了?请。

更新:没有错误。一切正常。我可以通过在 IDE 中运行来更改所有设置。所以我相信我在 maven build 上做错了什么。

【问题讨论】:

  • 就我而言,没有错误。一切正常。我可以通过在 IDE 中运行来更改所有设置。所以我相信我在 maven build 上做错了什么。
  • "一切正常。" 那么你的问题是什么?
  • "一切正常。" - 在 IDE 中,仅在 IDE 中。使用 Maven 构建 jar 后我遇到了问题。

标签: java spring maven logback


【解决方案1】:

在您的 maven antrun 插件中,您正在复制 application.properties,而不是移动,因此您的 jar 中总是有 application.properties 的副本! 尝试使用 maven 标准 jar 插件配置来排除您的属性文件:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.6</version>
    <configuration>
      <excludes>
        <exclude>path/to/application.properties</exclude>
      </excludes>
    </configuration>
  </plugin>

并确保执行时排除的文件在您的应用类路径中

【讨论】:

  • 感谢您的帮助!这一切对新手来说都很难。我刚试过。结果是:logging.path 正在从我的文件中读取,但是我在日志文件的名称中丢失了日期(现在它只是 spring.log)并且 logging.level 也不起作用。
猜你喜欢
  • 1970-01-01
  • 2019-06-12
  • 2017-06-04
  • 1970-01-01
  • 1970-01-01
  • 2018-04-22
  • 2013-03-11
  • 2010-10-20
  • 2014-07-22
相关资源
最近更新 更多