使用 Spring Boot 进行 Maven 资源过滤:无法解析占位符 [英] Maven Resource Filtering with Spring Boot: Could not resolve placeholder

查看:33
本文介绍了使用 Spring Boot 进行 Maven 资源过滤:无法解析占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对 Java 和 Maven 有点陌生,但我试图让 Maven 处理数据库连接属性,这样我就可以让 Maven 构建在开发/阶段/生产环境之间更改它们,但我遇到了过滤和资源问题.也不知道我在这里做错了什么.

Kind of new to Java and Maven, but I trying to get Maven to handle the database connection properties so I can have the maven build change them between dev/stage/prod environments and I am running into issues with filtering and resources. Not sure what I am doing wrong here either.

POM 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.comapny</groupId>
<artifactId>reporting</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>reporting</name>
<url>http://maven.apache.org</url>

<properties>
    <db.jdbcUrl>jdbc:aURL</db.jdbcUrl>
    <db.jdbcUn>aUser</db.jdbcUn>
    <db.jdbcPw>aPassword</db.jdbcPw>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <springframework.version>4.2.3.RELEASE</springframework.version>
    <springframework.jdbc.version>4.1.4.RELEASE</springframework.jdbc.version>
    <hadoop.version>2.7.1.2.3.4.2-1</hadoop.version>
    <hbase.version>1.1.2.2.3.4.2-1</hbase.version>
    <phoenix.version>4.4.0.2.3.4.2-1</phoenix.version>
    <junit.version>4.12</junit.version>
    <mail.version>1.4.3</mail.version>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.0.RELEASE</version>
</parent>

<repositories>
    <repository>
        <id>github-releases</id>
        <url>http://oss.sonatype.org/content/repositories/github-releases/</url>
    </repository>
    <repository>
        <id>clojars.org</id>
        <url>http://clojars.org/repo</url>
    </repository>
    <repository>
        <id>hortonworks</id>
        <url>http://repo.hortonworks.com/content/repositories/releases/</url>
    </repository>
    <repository>
        <id>twitter4j</id>
        <url>http://twitter4j.org/maven2</url>
    </repository>
</repositories>

<dependencies>

    <!-- <dependency> <groupId>jdk.tools</groupId> <artifactId>jdk.tools</artifactId> 
        <version>1.7.0_05</version> <scope>system</scope> <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath> 
        </dependency> -->

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <!-- <version>1.2.3.RELEASE</version> -->
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>xerces</groupId>
        <artifactId>xercesImpl</artifactId>
        <version>2.9.1</version>
    </dependency>
    <dependency>
        <groupId>xalan</groupId>
        <artifactId>xalan</artifactId>
        <version>2.7.1</version>
    </dependency>
    <dependency>
        <groupId>jdk.tools</groupId>
        <artifactId>jdk.tools</artifactId>
        <version>1.7.0_05</version>
        <scope>system</scope>
        <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
    </dependency>

    <dependency>
        <groupId>org.apache.phoenix</groupId>
        <artifactId>phoenix-core</artifactId>
        <version>${phoenix.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>log4j-over-slf4j</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
            <!-- <exclusion> <artifactId>joda-time</artifactId> <groupId>joda-time</groupId> 
                </exclusion> -->
        </exclusions>
    </dependency>
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
    </dependency>

    <!-- Hadoop Dependencies -->

    <dependency>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-util</artifactId>
        <version>6.1.26</version>
    </dependency>
    <dependency>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-sslengine</artifactId>
        <version>6.1.26</version>
    </dependency>


    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
        <version>${hadoop.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- HBase Dependcies -->
    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-client</artifactId>
        <version>${hbase.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>log4j-over-slf4j</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
    </dependency>


    <!-- Other -->
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mailapi</artifactId>
        <version>${mail.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>${mail.version}</version>
    </dependency>

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>

    <!-- End of Other -->

    <!-- Log4J 2.x 2.4.1 -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.5</version>
    </dependency>

    <dependency>
        <groupId>com.github.omkreddy</groupId>
        <artifactId>log4j2-kafka-appender</artifactId>
        <version>1.0</version>
    </dependency>

    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-clients</artifactId>
        <version>0.9.0.0</version>
    </dependency>

    <!-- Test -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <scope>test</scope>
    </dependency>

</dependencies>

<build>

    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.7</version>
            <configuration>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <showWarnings>false</showWarnings>
            </configuration>
        </plugin>
    </plugins>

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>db.properties</include>
            </includes>
        </resource>
    </resources>

    <testResources>
        <testResource>
            <directory>src/test/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>db.properties</include>
            </includes>
        </testResource>
    </testResources>

</build>

db.properties

db.properties

jdbc.url= ${db.jdbcUrl}
jdbc.username= ${db.jdbcUn}
jdbc.password= ${db.jdbcPw}

Maven 版本 3.3.8

Maven version 3.3.8

JDK 1.8.0-72

JDK 1.8.0-72

命令行

mvn test -P development -X

错误

 java.lang.IllegalArgumentException: Could not resolve placeholder 'db.jdbcUrl' in string value "${db.jdbcUrl}"

任何帮助将不胜感激.

推荐答案

首先,您不需要使用配置文件.资源 db.properties 是一个测试资源,所以它应该位于 src/test/resources 下而不是 src/main/resources 下.使用配置文件会使您的构建复杂化,您应该只将它们作为最后一个条件.

First of all, you don't need to use a profile. The resources db.properties is a test resource so it should be located under src/test/resources and not under src/main/resources. Using profiles will complicate your build, you should only resort to them as a last condition.

您遇到此问题的原因是 Spring Boot 将令牌过滤器重新定义为 @ 而不是默认的 ${*}.来自 文档:

The reason you're having this problem is that Spring Boot redefines the token filter to be @ instead of the default ${*}. From the docs:

如果您是从 spring-boot-starter-parent POM 继承的,maven-resources-plugins 的默认过滤器令牌已从 更改${*}@(即 @maven.token@ 而不是 ${maven.token})以防止冲突带有 Spring 风格的占位符.如果您直接为 application.properties 启用了 maven 过滤,您可能还想更改默认过滤器标记以使用其他分隔符.

If you are inheriting from the spring-boot-starter-parent POM, the default filter token of the maven-resources-plugins has been changed from ${*} to @ (i.e. @maven.token@ instead of ${maven.token}) to prevent conflicts with Spring-style placeholders. If you have enabled maven filtering for the application.properties directly, you may want to also change the default filter token to use other delimiters.

这意味着您应该改为:

jdbc.url= @db.jdbcUrl@
jdbc.username= @db.jdbcUn@
jdbc.password= @db.jdbcPw@

对于 db.properties 文件.

然后您需要删除您的 部分并将其替换为:

Then you need to remove your <resources> section and replace it with:

<testResources>
    <testResource>
        <directory>src/test/resources</directory>
        <filtering>true</filtering>
        <includes>
            <include>db.properties</include>
        </includes>
    </testResource>
</testResources>

这篇关于使用 Spring Boot 进行 Maven 资源过滤:无法解析占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆