복붙노트

[SPRING] 스프링 부트로 Maven 리소스 필터링 : 자리 표시자를 해결할 수 없습니다.

SPRING

스프링 부트로 Maven 리소스 필터링 : 자리 표시자를 해결할 수 없습니다.

일종의 Java와 Maven에 익숙하지만 Maven이 데이터베이스 연결 속성을 처리하도록하기 위해 maven 빌드를 dev / stage / prod 환경 사이에서 변경하고 필터링 및 리소스 문제가 발생했습니다. 내가 여기서 뭘 잘못하고 있는지도 모르겠다.

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

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

Maven 버전 3.3.8

JDK 1.8.0-72

명령 행

mvn test -P development -X

오류

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

어떤 도움이라도 대단히 감사하겠습니다.

해결법

  1. ==============================

    1.우선, 프로파일을 사용할 필요가 없습니다. db.properties 리소스는 테스트 리소스이므로 src / main / resources 아래가 아니라 src / test / resources 아래에 위치해야합니다. 프로필을 사용하면 빌드가 복잡해 지므로 마지막 조건으로 만 사용해야합니다.

    우선, 프로파일을 사용할 필요가 없습니다. db.properties 리소스는 테스트 리소스이므로 src / main / resources 아래가 아니라 src / test / resources 아래에 위치해야합니다. 프로필을 사용하면 빌드가 복잡해 지므로 마지막 조건으로 만 사용해야합니다.

    이 문제가 발생하는 이유는 스프링 부트가 토큰 필터를 기본 $ {*} 대신 @로 재정의한다는 것입니다. 문서에서 :

    즉, 대신 다음을 수행해야합니다.

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

    db.properties 파일 용.

    그런 다음 섹션을 제거하고 다음 섹션으로 대체해야합니다.

    <testResources>
        <testResource>
            <directory>src/test/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>db.properties</include>
            </includes>
        </testResource>
    </testResources>
    
  2. from https://stackoverflow.com/questions/35417086/maven-resource-filtering-with-spring-boot-could-not-resolve-placeholder by cc-by-sa and MIT license