Maven 中的 profile

2021-07-07 15:05 更新
  • Maven 中有一個(gè)概念叫做:?profile?,它的誕生主要是為了解決不同環(huán)境所需的不同變量、配置等問題。
  • 有了 ?profile?,可以根據(jù)激活的條件,啟動(dòng)不同條件下的配置信息。
  • ?profile ?是可以有多個(gè)的,也可以同時(shí)激活多個(gè) ?profile?,方便自由組合。
  • ?profile? 一般可以在三個(gè)地方:?settings.xml?,?pom.xml?,?profiles.xml?(這個(gè)不常用)
  • 在 ?settings.xml? 上,一般大家用來做倉庫的選擇,比如以下? settings.xml ?代碼:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>D:\maven\my_local_repository</localRepository>
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus</id>
<url>http://192.168.1.73:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://192.168.1.73:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>aliyun</id>
<repositories>
<repository>
<id>aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
  • 以上代碼中 ?profile? 就做一件事:設(shè)置全局的 ?profile?,一個(gè)是 ?nexus ?倉庫,一個(gè)是 ?aliyun ?倉庫,默認(rèn)激活的是 nexus 倉庫。(?activeProfiles?)
  • 在 ?pom.xml ?中,一般用來激活環(huán)境配置,比如以下代碼:
<profiles>
<profile>
<id>dev</id>
<properties>
<package.environment>dev</package.environment>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/env/${package.environment}</directory>
<includes>
<include>**/*</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<finalName>${project.artifactId}</finalName>
</build>
</profile>
<profile>
<id>product</id>
<properties>
<package.environment>product</package.environment>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/env/${package.environment}</directory>
<includes>
<include>**/*</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<finalName>${project.artifactId}</finalName>
</build>
</profile>
</profiles>
  • 以上代碼中 ?profile ?就做一件事:打包的時(shí)候,默認(rèn)是 ?dev ?模式,打包 ?src/main/env/dev? 下的配置文件,如果選擇 ?product? 則打包 ?src/main/env/product? 下的配置文件。


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)