配置文件目录:
项目有dev、dev2、dev3、test等环境下的多套配置文件。
在pom文件中添加如下配置切换:
<build>
<resources>
<resource>
<!-- directory加载resources下所有配置文件 -->
<directory>src/main/resources/</directory>
<!-- excludes不包括的配置文件 -->
<excludes>
<exclude>env/dev/*</exclude>
<exclude>env/dev2/*</exclude>
<exclude>env/dev3/*</exclude>
<exclude>env/test/*</exclude>
<exclude>env/test2/*</exclude>
<exclude>env/uat/*</exclude>
<exclude>env/prod/*</exclude>
</excludes>
<!-- includes包括的配置文件 -->
<includes>
<include>messages/*</include>
<include>**/*.xml</include>
<include>security/*</include>
</includes>
</resource>
<!-- 选择active的profile的配置文件直接放在classpath根目录下 -->
<resource>
<!-- 上面resource把env下的所有配置都剔除,这里env下选择激活的配置文件目录 -->
<directory>src/main/resources/env/${profiles.active}</directory>
</resource>
</resources>
</build>
<!-- 设置激活哪个profiles -->
<profiles>
<profile>
<id>dev</id>
<properties>
<profiles.active>dev</profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
<profile>
<id>dev2</id>
<properties>
<profiles.active>dev2</profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
</properties>
</profile>
<profile>
<id>test2</id>
<properties>
<profiles.active>test2</profiles.active>
</properties>
</profile>
<profile>
<id>uat</id>
<properties>
<profiles.active>uat</profiles.active>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profiles.active>prod</profiles.active>
</properties>
</profile>
</profiles>
maven项目打包命令:clean deploy -e -Dmaven.test.skip=true
maven配置文件setting.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:\apache-maven-3.5.2\repository</localRepository>
<pluginGroups></pluginGroups>
<proxies></proxies>
<!-- 配置远程库所在的服务器信息 -->
<servers>
<server>
<!-- id匹配在项目pom文件中配置的远程仓库id -->
<id>nexus-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
<!-- 配置镜像列表 -->
<mirrors>
<mirror>
<!-- 私服公共仓库 -->
<id>nexus-releases</id>
<!-- 被镜像的服务器id,多个可以用逗号隔开 -->
<mirrorOf>*</mirrorOf>
<url>http://192.168.0.237:8081/nexus/content/groups/public</url>
</mirror>
<mirror>
<!-- 私服开发版本仓库 -->
<id>nexus-snapshots</id>
<mirrorOf>*</mirrorOf>
<url>http://192.168.0.237:8081/nexus/content/repositories/snapshots</url>
</mirror>
<mirror>
<!-- 中央仓库的阿里镜像 -->
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
<mirror>
<!-- 中央仓库的阿里镜像 -->
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>central</id>
<name>Maven Repository Switchboard</name>
<url>http://repo1.maven.org/maven2/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo2.maven.org/maven2/</url>
</mirror>
<mirror>
<id>ibiblio</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>
</mirror>
<mirror>
<id>jboss-public-repository-group</id>
<mirrorOf>central</mirrorOf>
<name>JBoss Public Repository Group</name>
<url>http://repository.jboss.org/nexus/content/groups/public</url>
</mirror>
<mirror>
<id>google-maven-central</id>
<name>Google Maven Central</name>
<url>https://maven-central.storage.googleapis.com
</url>
<mirrorOf>central</mirrorOf>
</mirror>
<!-- 中央仓库在中国的镜像 -->
<mirror>
<id>maven.net.cn</id>
<name>oneof the central mirrors in china</name>
<url>http://maven.net.cn/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<!--如果一个settings中的profile被激活,它的值会覆盖任何其它定义在POM中或者profile.xml中的带有相同id的profile -->
<profiles>
<profile>
<id>jdk-1.8</id>
<!-- Activation是profile的开启钥匙 -->
<activation>
<!--profile默认是否激活的标识 -->
<activeByDefault>true</activeByDefault>
<!--activation有一个内建的java版本检测,如果检测到jdk版本与期待的一样,profile被激活 -->
<jdk>1.8</jdk>
</activation>
<!--如果Maven检测到某一个属性(其值可以在POM中通过${名称}引用),其拥有对应的名称和值,Profile就会被激活 -->
<!--如果值字段是空的,那么存在属性名称字段就会激活profile,否则按区分大小写方式匹配属性值字段 -->
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
<profile>
<id>nexus</id>
<!-- 远程仓库列表 -->
<repositories>
<repository>
<id>nexus-releases</id>
<url>http://192.168.0.237:8081/nexus/content/groups/public</url>
<!-- 开启该仓库的发布版本的下载 -->
<releases><enabled>true</enabled></releases>
<!-- 开启该仓库的快照版本的下载 -->
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>nexus-snapshots</id>
<url>http://192.168.0.237:8081/nexus/content/repositories/snapshots/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<!-- 插件的远程仓库列表 -->
<pluginRepositories>
<pluginRepository>
<id>nexus-releases</id>
<url>http://192.168.0.237:8081/nexus/content/groups/public</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
<pluginRepository>
<id>nexus-snapshots</id>
<url>http://192.168.0.237:8081/nexus/content/repositories/snapshots</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<!-- 手动激活profiles列表 -->
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
pom文件中配置私服地址:
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<distributionManagement>
<!-- 配置私服地址 -->
<repository>
<id>nexus-releases</id>
<url>http://192.168.0.237:8081/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<url>http://192.168.0.237:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>