项目基于spring框架开发时需要引入大量的spring模块,例如core、beans、context、jdbc等,在引入上述模块时所用的都是同一个版本的模块,此时我们会定义一个maven属性来处理这种应用场景。但maven的属性还有更多应用场景。

Maven属性分类

内置属性、pom属性、自定义属性、Settings属性、Java系统属性、环境变量属性

内置属性

内置属性主要就两个,一个是basedir表示项目的根目录,另一个是version表示项目版本

POM属性

用户可以使用该类属性引用pom文件中对应的元素的值,比如project.version就是取pom文件中标签下的值。

常用的pom属性包括:

属性名称

说明

project.build.sourceDirectory

项目的源码目录

project.build.testSourceDirectory

项目的测试源码目录

project.build.directory

项目构建输出目录,默认为 target

project.outputDirectory

项目主代码编译输出的目录,默认为 target/classes

project.testOutputDirectory

项目测试代码编译输出的目录,默认为 target/test-class

自定义属性

通过标签定义的属性

<properties>
	<main.version>2.0</main.version>
	<second.main.version>2021.12.29</second.main.version>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	<java.version>1.8</java.version>
	<spring-boot.version>2.3.2.RELEASE</spring-boot.version>
	<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
	<spring-cloud-alibaba.version>2.2.6.RELEASE</spring-cloud-alibaba.version>
	<validation-api.version>2.0.1.Final</validation-api.version>
	<hibernate-validator.version>6.2.0.Final</hibernate-validator.version>
	<jjwt.version>0.9.1</jjwt.version>
	<mybatis-plus.version>3.4.3.1</mybatis-plus.version>
	<fastjson.version>1.2.78</fastjson.version>
	<hutool.version>5.7.7</hutool.version>
	<commons-codec.version>1.10</commons-codec.version>
	<spring-boot-admin.version>2.2.0</spring-boot-admin.version>
	<httpclient.version>4.5.13</httpclient.version>
</properties>

Settings属性

用户以settings开头的属性引用settings.xml配置文件中xml元素的值。如settings.localRepository指向用户本地仓库的地址

Java系统属性

所有的Java系统属性都可以使用Maven属性引用。如user.home指向用户目录,可以使用mvn help:system查看所有的Java系统属性

环境变量属性

环境变量属性引用使用env开头。例如env.JAVA_HOME表示JAVA_HOME环境变量的值。

和Java系统属性一样,也可以通过mvn help:system查看

Maven Profile使用

我们在项目中定义的Pom实际都继承自pom根文件

根文件存放位置:

解压 apache-maven-3.8.1/lib/maven-model-builder-3.8.1.jar

maven-model-builder-3.8.1/org/apache/maven/model/pom-4.0.0.xml

<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!-- START SNIPPET: superpom -->
<project>
  <modelVersion>4.0.0</modelVersion>

  <repositories>
    <repository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
  </pluginRepositories>

  <build>
    <directory>${project.basedir}/target</directory>
    <outputDirectory>${project.build.directory}/classes</outputDirectory>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
    <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
    <scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
    <resources>
      <resource>
        <directory>${project.basedir}/src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>${project.basedir}/src/test/resources</directory>
      </testResource>
    </testResources>
    <pluginManagement>
      <!-- NOTE: These plugins will be removed from future versions of the super POM -->
      <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.8</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.5.3</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <reporting>
    <outputDirectory>${project.build.directory}/site</outputDirectory>
  </reporting>

  <profiles>
    <!-- NOTE: The release profile will be removed from future versions of the super POM -->
    <profile>
      <id>release-profile</id>

      <activation>
        <property>
          <name>performRelease</name>
          <value>true</value>
        </property>
      </activation>

      <build>
        <plugins>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-sources</id>
                <goals>
                  <goal>jar-no-fork</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-javadoc-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-javadocs</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-deploy-plugin</artifactId>
            <configuration>
              <updateReleaseInfo>true</updateReleaseInfo>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

</project>
<!-- END SNIPPET: superpom -->

profile使用场景

多环境支持(开发、测试、预生产、生产)

profile使用方法

  1. 定义一个配置文件,应用profile中定义的属性
driver=${driver}
url=${url}
user=${user}
password=${password}

profile中定义的属性properties名要与 *.properties 中定义的一致

<profile>
	<id>dev</id>
	<activation>
		<activeByDefault>false</activeByDefault>
	</activation>
	<properties>
		<driver>com.mysql.jdbc.Driver</driver>
		<url>jdbc:mysql://localhost:3306/dev</url>
		<user>root</user>
		<password>123456</password>
	</properties>
</profile>
  1. 定义profile,并激活指定的profile
<activation>
	<activeByDefault>false</activeByDefault>
</activation>
  1. 开启资源过滤
<resource>
	<directory>${basedir}/src/main/resources</directory>
	<filtering>true</filtering>
</resource>
  1. 编译测试
  2. 查看编译后的资源文件

profile的激活方式

  • 命令行方式激活
mvn install -Pdev,test
  • 在settings文件中激活
<activeProfiles>
	<activeProfile>dev</activeProfile>
</activeProfiles>
  • 系统属性激活
<activation>
	<property>
		<name>test</name>
		<value>123</value>
	</property>
</activation>
  • 操作系统环境激活
<activation>
	<os>
		<name>Windows 10</name>
	</os>
</activation>
  • 文件存在激活
<activation>
	<file>
		<exists>aaa.properties</exists>
	</file>
</activation>
  • 默认激活
<activation>
    <activeByDefault>true</activeByDefault>
</activation>

profile的种类

  1. pom.xml
  2. 用户settings.xml
  3. 全局settings.xml

profile使用完整示例

场景描述:例如我们有两套环境的数据库配置(dev、prod),里面的连接串账户不一样,那么我们需要建立多个配置文件进行区分,假如我们使用了profile仅需建立一个配置文件即可

db.properties

driver=${driver}
url=${url}
user=${user}
password=${password}

pom.xml

<?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.little</groupId>
    <artifactId>LearnMavenMaster</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <driver>com.mysql.jdbc.Driver</driver>
                <url>jdbc:mysql://localhost:3306/dev</url>
                <user>root</user>
                <password>123456</password>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <driver>com.mysql.jdbc.Driver</driver>
                <url>jdbc:mysql://localhost:3306/prod</url>
                <user>root</user>
                <password>root</password>
            </properties>
        </profile>
    </profiles>
    <build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <!-- 设置编译版本为1.8 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- 跳过测试 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

测试代码

Properties properties = new Properties();
properties.load(HelloWorld.class.getClassLoader().getResourceAsStream("db.properties"));
for (Map.Entry<Object, Object> item : properties.entrySet()) {
	System.out.printf("%s = %s%n", item.getKey(), item.getValue());
}


// output
// user = root
// password = root
// url = jdbc:mysql://localhost:3306/prod
// driver = com.mysql.jdbc.Driver

查看编译后的文件

maven中的profile的使用 maven的profile属性_maven中的profile的使用