Maven attribute and Profile

Posted by Riparian on Fri, 28 Jan 2022 12:40:38 +0100

When the project is developed based on the spring framework, a large number of spring modules need to be introduced, such as core, beans, context, jdbc, etc. when the above modules are introduced, the modules of the same version are used. At this time, we will define a maven attribute to deal with this application scenario. However, there are more application scenarios for Maven attributes.

Maven attribute classification

Built in attribute, pom attribute, custom attribute, Settings attribute, Java system attribute, environment variable attribute

Built in properties

There are two built-in attributes. One is basedir, which indicates the root directory of the project, and the other is version, which indicates the project version

POM properties

Users can use this type of attribute to reference the value of the corresponding element in the pom file, such as project Version is to take the value under the tag in the pom file.

Common pom attributes include:

Attribute name

explain

project.build.sourceDirectory

Source directory of the project

project.build.testSourceDirectory

Test source directory of the project

project.build.directory

The project construction output directory is target by default

project.outputDirectory

The directory of the project main code compilation output. The default is target/classes

project.testOutputDirectory

The directory of compiled output of project test code. The default is target / test class

Custom properties

Attributes defined by tags

<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 properties

The user's attribute starting with settings refers to settings The value of the xml element in the xml configuration file. Such as settings Localrepository points to the address of the user's local repository

Java system properties

All Java system properties can be referenced using Maven properties. Such as user Home points to the user directory. You can use mvn help:system to view all Java system properties

Environment variable properties

Environment variable attribute references start with env. For example, env JAVA_HOME means Java_ The value of the home environment variable.

Like Java system properties, you can also view them through mvn help:system

Maven Profile usage

The POMS we define in the project are actually inherited from the POM root file

Root file storage location:

Unzip 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 usage scenario

Multi environment support (development, testing, pre production, production)

profile usage

  1. Define a configuration file and apply the attributes defined in the profile
driver=${driver}
url=${url}
user=${user}
password=${password}

The property name defined in the profile should be the same as * 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. Define the profile and activate the specified profile
<activation>
	<activeByDefault>false</activeByDefault>
</activation>
  1. Turn on resource filtering
<resource>
	<directory>${basedir}/src/main/resources</directory>
	<filtering>true</filtering>
</resource>
  1. Compile test
  2. View the compiled resource file

Activation method of profile

  • Command line activation
mvn install -Pdev,test
  • Activate in settings file
<activeProfiles>
	<activeProfile>dev</activeProfile>
</activeProfiles>
  • System attribute activation
<activation>
	<property>
		<name>test</name>
		<value>123</value>
	</property>
</activation>
  • Operating system environment activation
<activation>
	<os>
		<name>Windows 10</name>
	</os>
</activation>
  • File presence activation
<activation>
	<file>
		<exists>aaa.properties</exists>
	</file>
</activation>
  • Default activation
<activation>
    <activeByDefault>true</activeByDefault>
</activation>

Type of profile

  1. pom.xml
  2. User settings xml
  3. Global settings xml

Complete example of profile usage

Scenario Description: for example, we have two sets of database configurations (dev and prod) in the environment. The connection string accounts are different, so we need to establish multiple configuration files to distinguish. If we use profile, we only need to establish one configuration file

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>
            <!-- Set the compiled version to 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>
            <!-- Skip test -->
            <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>

Test code

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

View compiled files

Topics: Java Maven Spring