Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Maven Build Profiles

Maven Build Profiles

A build profile is a set of configuration values that can be used to set or override Maven build defaults.

Using build profiles, you can customize the build for different environments, such as production (Production) and development (Development) environments.

Profiles are specified in the pom.xml file using the activeProfiles or profiles elements and can be triggered in various ways. Profiles modify the POM at build time and are used to set parameters for different target environments (for example, the database server address in development (Development), testing (Testing), and production (Production) environments).


Types of Build Profiles

There are generally three types of build profiles:

Type Where Defined
Per Project Defined in the project's POM file pom.xml
Per User Defined in Maven's settings xml file (%USER_HOME%/.m2/settings.xml)
Global Defined in Maven's global settings xml file (%M2_HOME%/conf/settings.xml)

Profile Activation

Maven build profiles can be activated in various ways.

Profile Activation Examples

Assume the project structure is as follows:

There are three files under the src/main/resources folder for testing:

File Name Description
env.properties The configuration used by default when no profile is specified.
env.test.properties The test configuration used when the test profile is applied.
env.prod.properties The production configuration used when the production profile is applied.

**Note:** These three configuration files do not represent the functionality of build profiles; they are used for this test's purpose; for example, when I specify the build profile as prod, the project uses the env.prod.properties file.

**Note:** The following example still uses the AntRun plugin, because this plugin can bind to Maven lifecycle phases and, through Ant tags, output information, copy files, etc., without writing a single line of code, and nothing more. The rest is unrelated to this build profile topic.

1. Profile Activation

A profile lets us define a set of configuration information and then specify its activation conditions. This way we can define multiple profiles, each with different activation conditions and configuration information, so that different environments use different configuration information.

In the following example, we add the maven-antrun-plugin:run goal to the test phase. This lets us output text information in different profiles. We will use pom.xml to define the different profiles and use maven commands in the command console to activate the profiles.

The pom.xml file 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: ****build profileused is node.

Explanation:Three were created above , where distinguishing different Execute different AntRun tasks; the AntRun task can be understood as follows: AntRun listens to the test phase of the Maven lifecycle; when Maven executes test, the AntRun task is triggered, and the task outputs text and copies files to the specified location; as for which AntRun task to execute, at this pointbuild profileplays the role of transmitting the specification, for example, entering the specified through command-line parameters 。

Execute a command:

mvn test -Ptest

Tip: the first test is the Maven lifecycle phase, the second test is thebuild profilespecified parameter, which is transmitted via -P; of course, it can be prod or normal, defined by you****。

The results of the run are as follows:

It can be seen that the AntRun task was successfully triggered, and it corresponds to thebuild profileunder task for test.

Test the other two commands; the results are as follows:

2. Activating Profiles via Maven Settings

Open %USER_HOME%/.m2 under the directory settings.xml file, where %USER_HOME% represents the user's home directory. If the setting.xml file does not exist, simply copy %M2_HOME%/conf/settings.xml to the .m2 directory, where %M2_HOME% represents the Maven installation directory.

Configure the setting.xml file, adding 属性:

<

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 a command:

mvn test

**Tip 1:** At this point there is no need to use -Ptest to input parameters; the setting.xml file above has already specified the test parameter instead.

**Tip 2:** It can also be used in the %M2_HOME%/conf/settings.xml file for configuration, with the same effect.

Execution result:

3. Activating Profiles via Environment Variables

First remove all the setting.xml values tested in the previous step.

Then in pom.xml, inside the for test node, add nodes in the document:

<

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 a command:

mvn test -Denv=test

**Tip 1:** Above, -D is used to pass environment variables, where env corresponds to the just-set value, and test corresponds to。

**Tip 2:** System environment variables were tested on Windows 10 but did not work, so they can only be passed via -D.

Execution result:

4. Activating Profiles via Operating System

The activation element contains the following operating system information. When the system is windows XP, the test Profile will be triggered.

<

profile

<

id

test

id

<

activation

<

os

<

name

Windows XP

name

<

family

Windows

family

<

arch

x86

arch

<

version

5.1.2600

version

os

activation

profile

Now open the command console, navigate to the directory containing pom.xml, and execute the following mvn command. Do not use the -P option to specify the Profile name. Maven will display the results of the activated test Profile.

mvn test

5. Activating Profiles via File Presence or Absence

Now use the activation element containing the following operating system information. When target/generated-sources/axistools/wsdl2java/com/companyname/group is missing, the test Profile will be triggered.

<

profile

<

id

test

id

<

activation

<

file

<

missing

target/generated-sources/axistools/wsdl2java/ com/companyname/group

missing

file

activation

profile

Now open the command console, navigate to the directory containing pom.xml, and execute the following mvn command. Do not use the -P option to specify the Profile name. Maven will display the results of the activated test Profile.

mvn test


Reference: https://www.cnblogs.com/EasonJim/p/6828743.html