Maven's Build Profiles

Posted by clem_c_rock on Wed, 02 Feb 2022 10:31:24 +0100

Maven's Build Profiles

When using build profiles on the command line, yes-P,For example: mvn -Pinput

Note: the build configuration file here is not a real file, but does specific things by specifying parameters.

The following is quoted from https://ayayui.gitbooks.io/tutorialspoint-maven/content/book/maven_build_profiles.html:

At that time, the example of this tutorial was in version 2.0, while the new version 3.0 only added a little. For details, please refer to the official website http://maven.apache.org/guides/introduction/introduction-to-profiles.html

What is a build profile?

A Build profile is the value of a series of configuration items, which can be used to set or override Maven build default values. Using the build configuration file, you can customize the build method for different environments, such as production environment and Development environment.

The configuration file is in POM The active profiles or profiles element is used to specify in the XML file, and can be triggered in various ways. The configuration file modifies the POM during construction and is used to set different target environments for parameters (for example, the address of the database server in the Development, Testing and production environments).

Type of build profile

There are generally three types of build profiles

typeWhere is it defined
Per ProjectDefined in the project POM file POM In XML
User level (Per User)Defined in Maven's settings xml file (% USER_HOME%/.m2/settings.xml)
GlobalDefined in Maven global settings xml file (% M2_HOME%/conf/settings.xml)

Profile activation

Maven's build profile can be activated in a number of ways.

  • Explicit activation using command console input.
  • Set via maven.
  • Based on environment variables (user or system variables).
  • Operating system settings (for example, Windows series).
  • The existence or absence of the file.

Official profile activation example

http://maven.apache.org/guides/introduction/introduction-to-profiles.html

http://maven.apache.org/ref/2.2.1/maven-profile/profiles.html

Practice profile activation example

The new project structure is as follows:

There are three test files in src/main/resources folder:

file namedescribe
env.propertiesIf no configuration file is specified, the default configuration is used.
env.test.propertiesThe test configuration when the test configuration file is used.
env.prod.propertiesThe production configuration when the production profile is used.

Note: these three configuration files do not represent the function of building configuration files, but are used for the purpose of this test; For example, when I specify that the build configuration file is prod, the project uses envprod Properties file.

Note: the following example still uses the AntRun plug-in, because this plug-in can bind the Maven life cycle stage and output information and copy files through the Ant tag without writing any code. The rest have nothing to do with this build configuration file.

1. Display profile activation

pom. The XML configuration is as follows:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jsoft.test</groupId>
  <artifactId>testproject</artifactId>
  <packaging>jar</packaging>
  <version>0.1-SNAPSHOT</version>
  <name>testproject</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <profiles>
      <profile>
          <id>test</id>
          <build>
              <plugins>
                 <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>
                    <executions>
                       <execution>
                          <phase>test</phase>
                          <goals>
                             <goal>run</goal>
                          </goals>
                          <configuration>
                          <tasks>
                             <echo>Using env.test.properties</echo>
                             <copy file="src/main/resources/env.test.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>
                          </tasks>
                          </configuration>
                       </execution>
                    </executions>
                 </plugin>
              </plugins>
          </build>
      </profile>
      <profile>
          <id>normal</id>
          <build>
              <plugins>
                 <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>
                    <executions>
                       <execution>
                          <phase>test</phase>
                          <goals>
                             <goal>run</goal>
                          </goals>
                          <configuration>
                          <tasks>
                             <echo>Using env.properties</echo>
                             <copy file="src/main/resources/env.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>
                          </tasks>
                          </configuration>
                       </execution>
                    </executions>
                 </plugin>
              </plugins>
          </build>
      </profile>
      <profile>
          <id>prod</id>
          <build>
              <plugins>
                 <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>
                    <executions>
                       <execution>
                          <phase>test</phase>
                          <goals>
                             <goal>run</goal>
                          </goals>
                          <configuration>
                          <tasks>
                             <echo>Using env.prod.properties</echo>
                             <copy file="src/main/resources/env.prod.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>
                          </tasks>
                          </configuration>
                       </execution>
                    </executions>
                 </plugin>
              </plugins>
          </build>
      </profile>
   </profiles>
</project>

Note: the < profiles > node is used to build the configuration file.

Note: three new < profiles > are created above. Among them, < ID > distinguishes different < profiles > and executes different AntRun tasks; AntRun's task can be understood in this way. AntRun monitors the Maven life cycle stage of test. When Maven executes test, in addition to sending AntRun's task, the task outputs text and copies files to the specified location; As for which AntRun task to execute, the build configuration file plays the role of transferring the specified. For example, enter the specified < ID > through the command line parameters.

Execute command:

mvn test -Ptest

Tip: the first test is the Maven life cycle stage, and the second test is the < ID > parameter specified for the build configuration file. This parameter is transmitted through - P. of course, it can be prod or normal, which are defined by you.

The results of the operation are as follows:

It can be seen that the AntRun task was successfully triggered. And it is a task whose < ID > is test under the corresponding build configuration file.

Then test the other two commands, and the results are as follows:

2. Activation profile via Maven settings

Open% USER_HOME%/. Settings in m2 directory XML file where% USER_HOME% represents the user's home directory. If setting Copy% m2 directly if the XML file does not exist_ HOME%/conf/settings.xml to m2 directory, where% M2_HOME% represents Maven's installation directory. For why you can do this, refer to: http://www.cnblogs.com/EasonJim/p/6827058.html

Configure setting XML file, add < activeprofiles > attribute:

<settings 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/settings-1.0.0.xsd">
   ...
   <activeProfiles>
      <activeProfile>test</activeProfile>
   </activeProfiles>
</settings>

Execute command:

mvn test

Tip: at this time, you don't need to use - Ptest to enter parameters. The above setting The < activeprofile > of the XML file has specified the test parameter instead.

Tip 2: it can also be used in% M2_HOME%/conf/settings.xml file, and the effect is consistent.

Execution result:

3. Activate the profile through the environment variable

First set the setting in the previous step Remove all XML values.

Then in POM The < ID > in XML is the < profile > node of test. Add the < activation > node:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jsoft.test</groupId>
  <artifactId>testproject</artifactId>
  <packaging>jar</packaging>
  <version>0.1-SNAPSHOT</version>
  <name>testproject</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <profiles>
      <profile>
          <id>test</id>
          <activation>
            <property>
               <name>env</name>
               <value>test</value>
            </property>
          </activation>
          <build>
              <plugins>
                 <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>
                    <executions>
                       <execution>
                          <phase>test</phase>
                          <goals>
                             <goal>run</goal>
                          </goals>
                          <configuration>
                          <tasks>
                             <echo>Using env.test.properties</echo>
                             <copy file="src/main/resources/env.test.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>
                          </tasks>
                          </configuration>
                       </execution>
                    </executions>
                 </plugin>
              </plugins>
          </build>
      </profile>
      <profile>
          <id>normal</id>
          <build>
              <plugins>
                 <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>
                    <executions>
                       <execution>
                          <phase>test</phase>
                          <goals>
                             <goal>run</goal>
                          </goals>
                          <configuration>
                          <tasks>
                             <echo>Using env.properties</echo>
                             <copy file="src/main/resources/env.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>
                          </tasks>
                          </configuration>
                       </execution>
                    </executions>
                 </plugin>
              </plugins>
          </build>
      </profile>
      <profile>
          <id>prod</id>
          <build>
              <plugins>
                 <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>
                    <executions>
                       <execution>
                          <phase>test</phase>
                          <goals>
                             <goal>run</goal>
                          </goals>
                          <configuration>
                          <tasks>
                             <echo>Using env.prod.properties</echo>
                             <copy file="src/main/resources/env.prod.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>
                          </tasks>
                          </configuration>
                       </execution>
                    </executions>
                 </plugin>
              </plugins>
          </build>
      </profile>
   </profiles>
</project>

Execute command:

mvn test -Denv=test

Tip: the above uses - D to pass environment variables, where evn corresponds to the < name > value just set, and test corresponds to < value >.

Tip 2: the environment variable of the system is tested on Windows 10, but it does not take effect. Therefore, it can only be passed through - D.

Execution result:

4. Activate profile via operating system

5. Activate the configuration file through the existence or absence of the file

6. Activate the configuration file through the version of JDK

...

For more activation configurations, please refer to the official example: http://maven.apache.org/guides/introduction/introduction-to-profiles.html

 

Test Engineering: https://github.com/easonjim/5_java_example/tree/master/maventest/test4/test4/testproject

Classification: Programming language - [Java]

Label: javamavenTutorial point Maven practice notes

    

2

«  Previous: After Jenkins is built, upload files to the specified SVN through SVN Publisher Plugin (tutorial Collection) 
»  Next: - D (Properties property) and - P (Profiles profile) in Maven

posted @ 2017-05-09 04:29  EasonJim Read (13937) comments (0) edit Collection and reporting

Topics: Maven