spring bootはmaven filterを通じてproperties属性を置換します(多環境配置)
9119 ワード
この二日間のプロジェクトはもう終わりました。いろいろな環境構成をmaven filterによってmaven包装の時にpropertiesの配置を変えたいです。以前はずっとドルの方式を使っていましたが、包装してからはプロモーションの属性を変えられません。これは私のmaven filterの属性です。
13.2 Maven Maven Maven users can inhers from the spring-boot-starter-parent project to outin sensible defaults.The parent project provides the following feature:
Java 1.6 as the default compler level.UTF-8 source encoding.A Dependency Management section,allowing you to omit tags for common dependencies,inhersed from the spring-dependencies POM.Sensible firebleSensible resource filtering forapaplication.properties and aplication.yml including profile-specifiles(e.g.aplication-foo.properties and aplication-foo.yml)Othe last point:since the the dededefafafaulconconconconconconconfifigficonconconconconconconconconconconconconconconconconfifififififififirererereininininininininingggggggggggggfificonconconconconconconconconconconconconconconconfifififififififififirereres(you can override that with a Maven property resource.delimiter)
大体の意味は、私のmavenはspring-boot-starter-parentを引き継いでいます。また、springのデフォルト設定ファイルが受け入れるプレースホルダも…です。だから、mavenfilter{}プレースホルダはspringのmaven pomに置き換えられて@になりました。@は、resource.delimiterによってカバーできます。spring-startmを見てください。
filter.spring.freemarker.template-loader-path=file:/mnt/web/ftl/
filter.spring.resources.static-locations=file:/mnt/web/static/
filter.server.port=80
#mysql connection info
filter.jdbc.url=jdbc:mysql://192.168.1.111:3306/ubip?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&allowMultiQueries=true
filter.jdbc.username=root
filter.jdbc.password=banger
#redis config
filter.redis.database=0
filter.redis.host=192.168.1.111
filter.redis.port=6379
filter.redis.password=1234
appication.propertiesファイルの中で差し替えられている属性が必要です。jdbc.url=${filter.jdbc.url}
...
mavenの配置 <profiles>
<profile>
<id>productid>
<properties>
<env>productenv>
properties>
profile>
profiles>
<build>
<filters>
<filter>src/main/filters/${env}.propertiesfilter>
filters>
<resources>
<resource>
<directory>src/main/resourcesdirectory>
<excludes>
<exclude>static/**exclude>
<exclude>ftl/**exclude>
excludes>
<filtering>truefiltering>
resource>
resources>
build>
以前はこのように配置してもいいです。spring bootではだめです。後に公式文書を見ましたが、中にはこのような言葉があります。13.2 Maven Maven Maven users can inhers from the spring-boot-starter-parent project to outin sensible defaults.The parent project provides the following feature:
Java 1.6 as the default compler level.UTF-8 source encoding.A Dependency Management section,allowing you to omit tags for common dependencies,inhersed from the spring-dependencies POM.Sensible firebleSensible resource filtering forapaplication.properties and aplication.yml including profile-specifiles(e.g.aplication-foo.properties and aplication-foo.yml)Othe last point:since the the dededefafafaulconconconconconconconfifigficonconconconconconconconconconconconconconconconconfifififififififirererereininininininininingggggggggggggfificonconconconconconconconconconconconconconconconfifififififififififirereres(you can override that with a Maven property resource.delimiter)
大体の意味は、私のmavenはspring-boot-starter-parentを引き継いでいます。また、springのデフォルト設定ファイルが受け入れるプレースホルダも…です。だから、mavenfilter{}プレースホルダはspringのmaven pomに置き換えられて@になりました。@は、resource.delimiterによってカバーできます。spring-startmを見てください。
<properties>
<java.version>1.6java.version>
<resource.delimiter>@resource.delimiter>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<maven.compiler.source>${java.version}maven.compiler.source>
<maven.compiler.target>${java.version}maven.compiler.target>
properties>
分かりました。私はpropertiesの中のプレースホルダを変えます。#mysql connection info
jdbc.url=@filter.jdbc.url@
jdbc.username=@filter.jdbc.username@
jdbc.password=@filter.jdbc.password@
#redis config
# Redis ( 0)
redis.database=@filter.redis.database@
# Redis
redis.host=@filter.redis.host@
# Redis
redis.port=@filter.redis.port@
# Redis ( )
redis.password=@filter.redis.password@
これで、mavenは属性を換えることができます。