总结:Maven之<profiles>

本文介绍了如何使用Maven配置不同环境(如开发、测试和生产环境)的方法,并提供了具体的配置案例,包括如何通过-P参数或-D参数来激活特定环境的配置。

一、介绍

我们项目打包的时候需要选择具体引用哪个配置文件,product?test?dev?

而<profiles>就是指定具体的配置文件的。

二、配置案例

<!-- profile begin -->
  <profiles>
    <profile>
      <id>dev</id>
      <activation>
        <property>
          <name>APP_ENV</name>
          <value>dev</value>
        </property>
      </activation>
      <properties>
        <profile.env.name>env.dev.properties</profile.env.name>
      </properties>
    </profile>
    <profile>
      <id>test</id>
      <activation>
        <property>
          <name>APP_ENV</name>
          <value>test</value>
        </property>
      </activation>
      <properties>
        <profile.env.name>env.test.properties</profile.env.name>
      </properties>
    </profile>
    <profile>
      <id>product</id>
      <activation>
        <property>
          <name>APP_ENV</name>
          <value>product</value>
        </property>
      </activation>
      <properties>
        <profile.env.name>env.product.properties</profile.env.name>
      </properties>
    </profile>
    <profile>
      <id>product_cron</id>
      <activation>
        <property>
          <name>APP_ENV</name>
          <value>product_cron</value>
        </property>
      </activation>
      <properties>
        <profile.env.name>env.product_cron.properties</profile.env.name>
      </properties>
    </profile>
  </profiles>
  <!-- profile end -->

三、引用

方式一:通过-P引用

打包时执行mvn clean package -P test将触发test环境的profile配置

相对应的:

mvn clean package -P dev  触发env.dev.properties

mvn clean package -P product  触发 env.product.properties

方式二:通过-D引用

-D代表(Properties属性)

如上对应的property的name为APP_ENV,因此可以使用如下命令

mvn clean package -DAPP_ENV=test  将触发test环境的profile配置

mvn clean package -DAPP_ENV=dev  触发env.dev.properties

mvn clean package -DAPP_ENV=product  触发 env.product.properties

pom报错: Element encoding is not allowed here:427 Element encoding is not allowed here:427 Plugin' org. apache. maven. plugins: maven-failsafe插in:3.1.2' not found:634 Plugin' org. apache. maven. plugins: maven-failsafe插in:3.1.2' not found:634 Plugin' org. apache. maven. plugins: maven-failsafe插in:3.1.2' not found:635 Plugin' org. apache. maven. plugins: maven-failsafe插in:3.1.2' not found:635 Cannot resolve symbol ' integration-test':639 Cannot resolve symbol ' verify':640 Plugin' org. apache. maven. plugins: maven-surefire报port插in:3.1.2' not found:655 Plugin' org. apache. maven. plugins: maven-surefire报port插in:3.1.2' not found:655 Plugin' org. apache. maven. plugins: maven-surefire报port插in:3.1.2' not found:656 Plugin' org. apache. maven. plugins: maven-surefire报port插in:3.1.2' not found:656 Plugin' org. apache. maven. plugins: maven-project-info-reports插in:3.4.5' not found:662 Plugin' org. apache. maven. plugins: maven-project-info-reports插in:3.4.5' not found:662 Plugin' org. apache. maven. plugins: maven-project-info-reports插in:3.4.5' not found:663 Plugin' org. apache. maven. plugins: maven-project-info-reports插in:3.4.5' not found:663 Plugin' org. owasp: dependency-check-maven:9.0.7' not found:668 Plugin' org. owasp: dependency-check-maven:9.0.7' not found:668 Plugin' org. owasp: dependency-check-maven:9.0.7' not found:669 Plugin' org. owasp: dependency-check-maven:9.0.7' not found:669 Plugin' org. owasp: dependency-check-maven:9.0.7' not found:670 Plugin' org. owasp: dependency-check-maven:9.0.7' not found:670 <?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> <groupId>com.drivingschool</groupId> <artifactId>driving-school-system</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>driving-school-system</name> <description>驾校学员管理系统 - Spring Boot项目</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.18</version> <!-- 使用LTS版本 --> <relativePath/> </parent> <properties> <java.version>11</java.version> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- 依赖版本管理 --> <mysql.version>8.0.33</mysql.version> <lombok.version>1.18.30</lombok.version> <mapstruct.version>1.5.5.Final</mapstruct.version> <jjwt.version>0.11.5</jjwt.version> <caffeine.version>3.1.8</caffeine.version> <hibernate.validator.version>6.2.5.Final</hibernate.validator.version> <swagger.version>3.0.0</swagger.version> <flyway.version>9.22.3</flyway.version> <springdoc.version>1.7.0</springdoc.version> </properties> <dependencies> <!-- Spring Boot Starters --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- Thymeleaf Security Integration --> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> </dependency> <!-- 数据库 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <scope>compile</scope> </dependency> <!-- 数据库迁移 --> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> <version>${flyway.version}</version> </dependency> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-mysql</artifactId> <version>${flyway.version}</version> </dependency> <!-- 缓存 --> <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> <version>${caffeine.version}</version> </dependency> <!-- JWT --> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-api</artifactId> <version>${jjwt.version}</version> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-impl</artifactId> <version>${jjwt.version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-jackson</artifactId> <version>${jjwt.version}</version> <scope>runtime</scope> </dependency> <!-- 工具库 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct</artifactId> <version>${mapstruct.version}</version> </dependency> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>${mapstruct.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.4</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>32.1.2-jre</version> </dependency> <!-- 验证 --> <dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> <version>${hibernate.validator.version}</version> </dependency> <!-- API文档 --> <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-ui</artifactId> <version>${springdoc.version}</version> </dependency> <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-data-rest</artifactId> <version>${springdoc.version}</version> </dependency> <!-- Excel处理 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.2.4</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.4</version> </dependency> <!-- 文件上传处理 --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.13.0</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.5</version> </dependency> <!-- 二维码生成 --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.5.2</version> </dependency> <!-- 邮件发送 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <!-- 测试依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.testcontainers</groupId> <artifactId>mysql</artifactId> <version>1.19.3</version> <scope>test</scope> </dependency> <dependency> <groupId>org.awaitility</groupId> <artifactId>awaitility</artifactId> <version>4.2.0</version> <scope>test</scope> </dependency> <!-- 开发工具 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> </dependencies> <dependencyManagement> <dependencies> <!-- 确保所有Spring Boot依赖版本一致 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.7.18</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <finalName>driving-school-system</finalName> <plugins> <!-- Spring Boot Maven插件 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> <layers> <enabled>true</enabled> </layers> <mainClass>com.drivingschool.DrivingSchoolApplication</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <!-- 编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> <annotationProcessorPaths> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </path> <path> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>${mapstruct.version}</version> </path> <path> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <version>2.7.18</version> </path> </annotationProcessorPaths> <compilerArgs> <arg>-Amapstruct.defaultComponentModel=spring</arg> <arg>-Amapstruct.unmappedTargetPolicy=IGNORE</arg> </compilerArgs> </configuration> </plugin> <!-- 资源过滤 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.3.1</version> <configuration> <delimiters> <delimiter>@</delimiter> </delimiters> <useDefaultDelimiters>false</useDefaultDelimiters> </configuration> </plugin> <!-- 打包时包含依赖的源码 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.3.0</version> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <!-- 打包时包含JavaDoc --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>3.6.3</version> <executions> <execution> <id>attach-javadocs</id> <goals> <goal>jar</goal> </goals> <configuration> <doclint>none</doclint> </configuration> </execution> </executions> </plugin> <!-- 代码质量检查 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <version>3.3.0</version> <configuration> <configLocation>checkstyle.xml</configLocation> <encoding>UTF-8</encoding> <consoleOutput>true</consoleOutput> <failsOnError>true</failsOnError> <linkXRef>false</linkXRef> </configuration> <executions> <execution> <id>validate</id> <phase>validate</phase> <goals> <goal>check</goal> </goals> </execution> </executions> </plugin> <!-- 测试覆盖率 --> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.10</version> <executions> <execution> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>report</id> <phase>test</phase> <goals> <goal>report</goal> </goals> </execution> <execution> <id>check</id> <goals> <goal>check</goal> </goals> <configuration> <rules> <rule> <element>BUNDLE</element> <limits> <limit> <counter>INSTRUCTION</counter> <value>COVEREDRATIO</value> <minimum>0.80</minimum> </limit> </limits> </rule> </rules> </configuration> </execution> </executions> </plugin> <!-- Docker构建插件 --> <plugin> <groupId>com.google.cloud.tools</groupId> <artifactId>jib-maven-plugin</artifactId> <version>3.4.0</version> <configuration> <from> <image>eclipse-temurin:11-jre-focal</image> <platforms> <platform> <architecture>amd64</architecture> <os>linux</os> </platform> <platform> <architecture>arm64</architecture> <os>linux</os> </platform> </platforms> </from> <to> <image>driving-school-system:${project.version}</image> </to> <container> <entrypoint> <entry>java</entry> <entry>-cp</entry> <entry>/app/resources:/app/classes:/app/libs/*</entry> <entry>com.drivingschool.DrivingSchoolApplication</entry> </entrypoint> <ports> <port>8080</port> </ports> <environment> <TZ>Asia/Shanghai</TZ> </environment> <creationTime>USE_CURRENT_TIMESTAMP</creationTime> </container> </configuration> </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*.properties</include> <include>**/*.yml</include> <include>**/*.yaml</include> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <filtering>false</filtering> <excludes> <exclude>**/*.properties</exclude> <exclude>**/*.yml</exclude> <exclude>**/*.yaml</exclude> <exclude>**/*.xml</exclude> </excludes> </resource> </resources> </build> <profiles> <!-- 开发环境配置 --> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <spring.profiles.active>dev</spring.profiles.active> <maven.test.skip>true</maven.test.skip> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <profiles> <profile>dev</profile> </profiles> <jvmArguments>-Xmx512m -Xms256m</jvmArguments> </configuration> </plugin> </plugins> </build> </profile> <!-- 测试环境配置 --> <profile> <id>test</id> <properties> <spring.profiles.active>test</spring.profiles.active> </properties> </profile> <!-- 生产环境配置 --> <profile> <id>prod</id> <properties> <spring.profiles.active>prod</spring.profiles.active> <maven.test.skip>true</maven.test.skip> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <profiles> <profile>prod</profile> </profiles> <jvmArguments> -Xmx1024m -Xms512m -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/heapdump.hprof </jvmArguments> </configuration> </plugin> </plugins> </build> </profile> <!-- 打包跳过测试 --> <profile> <id>package-only</id> <properties> <maven.test.skip>true</maven.test.skip> <skipTests>true</skipTests> </properties> </profile> <!-- 运行集成测试 --> <profile> <id>integration-test</id> <properties> <maven.test.skip>false</maven.test.skip> <skipTests>false</skipTests> <skipITs>false</skipITs> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>3.1.2</version> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> <reporting> <plugins> <!-- 测试报告 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-report-plugin</artifactId> <version>3.1.2</version> </plugin> <!-- 依赖分析 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> <version>3.4.5</version> </plugin> <!-- 依赖检查 --> <plugin> <groupId>org.owasp</groupId> <artifactId>dependency-check-maven</artifactId> <version>9.0.7</version> <reportSets> <reportSet> <reports> <report>aggregate</report> </reports> </reportSet> </reportSets> </plugin> </plugins> </reporting> <!-- 仓库配置 --> <repositories> <repository> <id>aliyun</id> <name>Aliyun Maven</name> <url>https://maven.aliyun.com/repository/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>central</id> <name>Central Repository</name> <url>https://repo.maven.apache.org/maven2</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>aliyun</id> <name>Aliyun Plugin Repository</name> <url>https://maven.aliyun.com/repository/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project>
最新发布
12-17
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd"> <localRepository>D:\Program Files\apache-maven-3.8.5\download</localRepository> <pluginGroups> </pluginGroups> <proxies> </proxies> <servers> <!--科电数据外网maven仓库begin--> <server> <id>nexus-ieds</id> <username>ieds</username> <password>,ki89ol.</password> </server> </servers> <mirrors> <mirror> <id>ieds</id> <name>科电数据研发中心maven仓库</name> <url>http://10.10.63.99:8081/repository/maven-public/</url> <mirrorOf>*</mirrorOf> </mirror> <mirror> <id>geotools</id> <name>GeoTools Releases</name> <url>https://repo.osgeo.org/repository/release/</url> <mirrorOf>org.geotools</mirrorOf> <!-- 只替换 org.geotools 包的相关依赖 --> </mirror> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> <profiles> <profile> <id>geotools</id> <repositories> <repository> <id>geotools</id> <url>https://repo.osgeo.org/repository/release/</url> <releases> <enabled>false</enabled> </releases> </repository> </repositories> </profile> <profile> <id>ieds</id> <activation> <activeByDefault>false</activeByDefault> <!-- 默认激活 --> </activation> </profile> <profile> <id>alimaven</id> <activation> <activeByDefault>true</activeByDefault> <!-- 默认激活 --> </activation> </profile> </profiles> </settings>这个maven的配置工具有问题吗
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值