Maven资源过滤无法正常工作 - 因为Spring引导依赖性

Jay*_*Jay 82 java maven spring-boot

在maven项目中,我尝试使用maven资源过滤替换一些令牌,但它不起作用.我有一些其他项目可行,但在这个单项目中不起作用,不确定有什么问题.

属性文件位于/src/main/resources/my.properties中

我尝试了不同的maven命令如下,但不起作用.

mvn clean install
mvn clean install resources:resources
Run Code Online (Sandbox Code Playgroud)

my.properties

### Spring boot properties
jdbc.url=${jdbc.url}
ldap.domain=${ldap_domain}
ldap.url=${ldap_url}
Run Code Online (Sandbox Code Playgroud)

的pom.xml

    <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.jai</groupId>
    <artifactId>client</artifactId>
    <version>0.0.6-SNAPSHOT</version>
    <name>client</name>
    <description>client web application</description>
    <packaging>war</packaging>

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


    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-ldap</artifactId>
        </dependency>

    </dependencies>

    <build>
        <finalName>client</finalName>

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

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

            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>exec-bower-install</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <executable>bower</executable>
                            <arguments>
                                <argument>install</argument>
                            </arguments>
                        </configuration>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>

    </build>


    <profiles>
        <!-- localhost environment -->
        <profile>
            <id>local</id>

            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>

            <properties>

                <ldap_domain>mydomain.local</ldap_domain>
                <ldap_url>ldap://server:389</ldap_url>
                <jdbc.url>testttttttttttttttttttttt</jdbc.url>

            </properties>
        </profile>

        </profiles>

</project>
Run Code Online (Sandbox Code Playgroud)

更新: -

我发现这个问题是由于Spring引导依赖引起的.如果我评论该<parent>部分和其他Spring引导依赖项,那么它工作正常并能够替换令牌.但仍不确定如何通过保持弹簧启动来解决这个问题.

Jay*_*Jay 204

最后在我的评论中找到了链接的答案.因为这是一个弹簧启动应用程序...特殊情况......符号应该是

@xxxxx@  instead of ${xxxxx}
Run Code Online (Sandbox Code Playgroud)

所以我的属性文件如下

### Spring boot properties
jdbc.url=@jdbc.url@
ldap.domain=@ldap_domain@
ldap.url=@ldap_url@
Run Code Online (Sandbox Code Playgroud)

  • 我的天!我浪费了几个小时试图解决为什么今天不能正常工作ツ (7认同)
  • 您可以在https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html中获得更多相关信息. (5认同)
  • 这使我的一天;-)非常感谢! (3认同)
  • 只是为了防止人们打猎,这在 SB 1.3.0 中进行了更改。这里是 https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-1.3-Release-Notes#maven-resources-filtering (2认同)