代码之家  ›  专栏  ›  技术社区  ›  dynamo

我的.properties文件没有被提取,我的maven配置文件属性也没有被注入

  •  -1
  • dynamo  · 技术社区  · 1 年前

    编辑:如果你否决了我的投票,那么请至少评论一下为什么。

    我正试图通过在pom.xml中使用maven概要文件,将特定的细节注入到我的datasource.properties文件中

    出于某种原因,它将详细信息应用于其他需要的地方,例如hibernate.cfg.xml,但它没有将其应用于我的datasource.properties?

    我已经添加了资源所在位置的配置,但不确定我缺少什么?

    这是我的pom.xml中的相关信息

    .......
    <profiles>
            <profile>
                <id>development</id>
                <activation>
                    <activeByDefault>true</activeByDefault>
                    <property>
                        <name>environment</name>
                        <value>dev</value>
                    </property>
                </activation>
                <properties>
                     <db.username>some username</db.username>
                     <db.password>some password</db.password>
                     <db.url>some db url</db.url>
                </properties>
            </profile>
        </profiles>
        
        <properties>
            <db.username>some username</db.username>
            <db.password>some password</db.password>
            <db.url>some db url</db.url>
        </properties>
        
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.3.1</version>
                    <configuration>
                        <warName>APP</warName>
                        <webResources>
                            <resource>
                                <directory>src/main/webapp/META-INF</directory>
                                <filtering>true</filtering>
                                <targetPath>META-INF</targetPath>
                            </resource>                     
                            <resource>
                                <directory>src/main/resources</directory>
                                <filtering>true</filtering>
                                <includes>
                                    <include>**/hibernate.cfg.xml</include>
                                    <include>**/datasource.properties</include>
                                </includes>
                            </resource>
                            <resource>
                                <directory>src/main/webapp</directory>
                                <filtering>true</filtering>
                            </resource>
                        </webResources>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    .......
    

    以下是我的datasource.properties文件中的格式

    datasource.driverClassName=some driver
    datasource.url=${db.url}
    datasource.username=${db.username}
    datasource.password=${db.password}
    datasource.initialPoolSize=10
    datasource.minPoolSize=10
    datasource.maxPoolSize=20
    datasource.maxStatements=50
    
    
    1 回复  |  直到 1 年前
        1
  •  1
  •   Jens    1 年前

    如果您想用maven替换构建时的占位符,则必须使用资源筛选:

    将此添加到您的 <build> pom.xml中的部分

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    

    在运行时不替换值和访问环境变量的代码