profile
在pom文件中配置 開發和測試環境的 profile信息,
<profiles>
<profile>
<!-- 開發環境 -->
<id>dev</id>
<properties>
<user>root</user>
<password>root</password>
<ip>192.168.233.123</ip>
<active>dev</active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 測試環境 -->
<id>test</id>
<properties>
<user>root</user>
<password>root</password>
<ip>192.168.233.124</ip>
<active>test</active>
</properties>
</profile>
</profiles>
引入插件
<build>
<!-- maven擴展 提供ssh遠程服務 是wagon-maven-plugin插件所依賴 -->
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh</artifactId>
<version>2.8</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>wagon-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<!-- 拷貝目錄下(執行目錄) target目錄下的jar包 -->
<fromFile>target/${project.artifactId}${activeName}-${project.version}.jar</fromFile>
<!-- 使用scp傳輸文件 指定服務端 用戶名密碼 ip 並指定目標文件夾-->
<url>scp://${user}:${password}@${ip}/home/app/server</url>
<!-- 命令列表 可以在傳輸完成后執行 -->
<commands>
<command>systemctl restart boot-server</command>
</commands>
<!-- 顯示運行命令的輸出結果 -->
<displayCommandOutputs>true</displayCommandOutputs>
</configuration>
</plugin>
</plugins>
</build>
更多用法 請查看官網
-
將 profile中配置的變量 作為 連接的參數 這樣指定profile后 就可以動態的拷貝的相應的文件夾
-
執行命令:
mvn clean package -Ptest wagon:upload-single wagon:sshexec
這個命令分成三個部分
-
mvn clean package -Ptest
指定profile 並打包 -
wagon:upload-single
上傳命令 -
wagon:sshexec
執行配置的命令列表
可以酌情選擇
-
使用maven profile 替換 spring 的profile
上面是將項目按照指定的profile打包並上傳到服務器 , 但是如果你的springboot項目 也配置了多個 環境,那么在執行時也需要 指定對應的 spring profile ,
可以 使用maven的profile 代替 spring的profile 那么當打包完成后 就無須在啟動時重新指定
在application.yml 中配置:
spring:
profiles:
active: @active@
使用@@ 取maven profile中配置的變量, 如上例配置了maven的profile為test 等價於spring.profiles.active=test
這樣就會讀取到 application-test.yml里面的配置了
這里我一直都犯了一個錯誤: 我一直以為 當執行jar包指定了profile后 則會讀取 相應的 application-{profile}.yml文件,不會讀取 默認的application.yml了,其實不然 正確的讀取順序是,無論如果都會先讀取默認文件夾下面 application.yml 再根據有沒有指定profile ,去找對應的 yaml,並覆蓋之前相同的配置(互補) 這也能解釋為什么 profile配置在 application.yml 中可以生效的原因