如何将Spring Boot配置文件与Maven配置文件集成?

时间:2019-07-01 21:09:47

标签: spring maven spring-boot spring-boot-maven-plugin maven-profiles

我正在尝试将Spring Boot个人资料与Maven个人资料集成在一起,但是由于某些原因,总是会拾取default个人资料。

MVN干净测试-Dspring.profiles.active = prod(正在工作)

日志:

2019-07-01 17:02:15.013  INFO 21872 --- [           main] com.example.BrowserTest                  : The following profiles are active: prod
2019-07-01 17:02:15.405  INFO 21872 --- [           main] com.example.BrowserTest                  : Started BrowserTest in 0.743 seconds (JVM running for 1.757)
************************************************************************
************************************************************************
Site: https://www.yahoo.com
************************************************************************
************************************************************************
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.509 s - in com.example.BrowserTest

mvn干净测试-Prod(不起作用,总是选择默认配置文件)

日志:

2019-07-01 17:03:20.136  INFO 17532 --- [           main] com.example.BrowserTest                  : The following profiles are active: @activatedProperties@
2019-07-01 17:03:20.535  INFO 17532 --- [           main] com.example.BrowserTest                  : Started BrowserTest in 0.727 seconds (JVM running for 1.706)
************************************************************************
************************************************************************
Site: http://www.default.com
************************************************************************
************************************************************************
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.456 s - in com.example.BrowserTest

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>profiles-junit-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>profiles-junit-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

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

    <build>
        <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>
                <configuration>
                    <profiles>
                        <profile>dev</profile>
                        <profile>prod</profile>
                    </profiles>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <activatedProperties>dev</activatedProperties>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <activatedProperties>prod</activatedProperties>
            </properties>
        </profile>
    </profiles>

</project>

src / test / resources / application.properties

spring.profiles.active=@activatedProperties@
site=http://www.default.com

src / test / resources / application-dev.properties

site=https://www.google.com

src / test / resources / application-prod.properties

site=https://www.yahoo.com

BrowserTest.java

public class BrowserTest extends ProfilesJunitDemoApplicationTests {

    @Value("${site}")
    private String site;

    //mvn clean test
    //mvn clean test -Dspring.profiles.active=dev
    //mvn clean test -Dspring.profiles.active=prod
    //mvn clean test -Pprod - NOT WORKING :(
    @Test
    public void homePage() {
        System.out.println("************************************************************************");
        System.out.println("************************************************************************");
        System.out.println("Site: " + site);
        System.out.println("************************************************************************");
        System.out.println("************************************************************************");
        assertNotNull(site);
    }

}

2 个答案:

答案 0 :(得分:1)

有两种方法:

  1. from django.http import HttpResponse from django.shortcuts import render import operator import string def home(request): return render(request, 'home.html', ) def count(request): fulltext = request.GET['fulltext'] def about(request): return render(request, 'about.html',) #Deletes empty spaces in fulltext and save it as wordlist wordlist = fulltext.split() #Declare an empty list worddictionary = {} #For each word in wordlist, if exist in list add 1, if not, add it to list for word in wordlist: word = word.lower() if word in worddictionary: worddictionary[word] += 1 else: worddictionary[word] = 1 #Sort list from highest occurence to least sortedwords = sorted(worddictionary.items(), key=operator.itemgetter(1), reverse=True) return render(request, 'count.html',{'fulltext':fulltext, 'count':len(wordlist), 'sortedwords': sortedwords}) 移至src/test/resources/application.properties

  2. 添加src/main/resources/application.properties

testResources

答案 1 :(得分:0)

根据文档profilesjava.lang.String[]

因此认为应该对其进行定义:

    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <profiles>
                    dev,prod
                </profiles>
            </configuration>
        </plugin>
    </plugins>

尚未测试过,但我认为是这样,您定义了要激活的所有配置文件(不是我认为您希望同时激活dev和prod)。

Spring boot plugin profiles