【问题标题】:Spring + Maven + .properties filesSpring + Maven + .properties 文件
【发布时间】:2014-06-29 13:44:55
【问题描述】:

我使用 Spring 和 Maven 构建我的项目,并且有两个文件位于 src/main/resources 路径(application.properties、log4j.properties)。如果我在 IDE 中打开我的项目并运行它,一切正常,可以更改 application.properties 文件中的值并使用更改的值再次运行项目。但是当我运行命令 mvn clean install 时,是的,我得到了 .jar 文件,但我无法使用 .properties 文件配置这个 .jar。此 .jar 在构建之前使用属性文件中的值。

我不知道这是否重要,但这是我在 Spring 应用程序上下文中定义属性文件的方式。

@PropertySource({"application.properties", "log4j.properties"})
public class AppConfig {

我将这些插件放在 pom.xml 文件中。可以吗?你能帮我吗?看起来构建的 .jar 项目不知道同一目录级别的 .properties 文件。

   <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.2</version>
            <!-- nothing here -->
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2-beta-4</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>cz.cvut.fit.bouchja1.ensemble.main.EnsembleApp</mainClass>
                    </manifest>
                    <manifestEntries>
                        <Class-Path>.</Class-Path> <!-- HERE IS THE IMPORTANT BIT -->
                    </manifestEntries>                        
                </archive>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>                    
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>

    </plugins>     

【问题讨论】:

  • 如果您离开 Class-Path 会发生什么?
  • 你的意思是我删除了它吗?我会试试的。
  • 您是否尝试为@PropertySource 指定文件路径?我认为这会起作用:@PropertySource({"classpath:application.properties", "classpath:log4j.properties"})
  • @gerardribas 不幸的是:(

标签: java spring maven properties


【解决方案1】:

对于运行时属性,请使用Properties Api

【讨论】:

  • 我已经定义了这个......我的程序知道属性文件,但问题是在 maven 安装(并创建一个 .jar 文件)之后我想要一个从外部更改属性的选项。 ..所以旁边会有.jar文件和.properties文件。
  • 你是对的......但是有一些适用于 Spring 的原生解决方案吗?我已经用 Spring 编写了所有项目和属性加载。
【解决方案2】:

好吧,看来解决办法是这样的:

@PropertySource({"file:application.properties", "file:log4j.properties"})

运行 mvn clean install 并创建一个 .jar 文件后,我可以在 .jar 旁边添加 application.properties 和 log4j.properties 文件,我可以使用属性文件配置项目。

但是:当我想从 Netbeans 运行项目时,文件“不存在:...因为它没有在类路径中指定。我认为这可能会有所帮助:

@PropertySource({"classpath:log4j.properties", "classpath:application.properties", "file:application.properties", "file:log4j.properties"})

但不是真的。此尝试后抛出异常:

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to load bean class: cz.cvut.fit.bouchja1.ensemble.spring.AppConfig; nested exception is java.io.FileNotFoundException: application.properties (Directory or file does not exist)

编辑:这正是我正在寻找的解决方案How to Exclude poperties file from jar file?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-09
    相关资源
    最近更新 更多