Application of Maven Profile

maven's profile app will appear more or less in mid-sized and large companies.

If you're new to the maven profile, you probably don't care what it does, just like me.

Even though I've seen it many times, I don't need to configure it myself, just be active.

But once the problem occurs, you probably won't find any problems.

Basic concepts

Profile, which has the same meaning as the basic translation, means configuration in technical translation.Depending on the profile, the runtime replaces the defined variable values.

If you've used spring's profile, you probably know what's going on.

Different types of profile s

  • Project-level profile

Define POM file

  • User level

Defined in (%USER_HOME%/.m2/settings.xml)

  • The global definition is in (${maven.home}/conf/settings.xml).

How do I activate profile s?

  • Explicitly
  • Through Maven settings
  • Based on environment variables
  • OS settings
  • Present or missing files
In the setting s configuration file, start explicitly

In the configuration file, you can explicitly specify the profile to start within the <activeProfiles>tag

The following:

<settings>
  ...
  <activeProfiles>
    <activeProfile>profile-1</activeProfile>
  </activeProfiles>
  ...
</settings>
Start with build-based environment variables

The following configuration represents that profile s are activated when the jdk version number is between 1.3 and 1.5

<profiles>
  <profile>
    <activation>
      <jdk>[1.3,1.6)</jdk>
    </activation>
    ...
  </profile>
</profiles>

The following is based on the runtime variable, and if there is an "environment" in the system variable and the value is "test", the following profile will be activated.

mvn groupId:artifactId:goal -Denvironment=test


    <profiles>
  <profile>
    <activation>
      <property>
        <name>environment</name>
        <value>test</value>
      </property>
    </activation>
    ...
  </profile>
</profiles>

Specific application

So what can you actually do with profile s?

Profiles are often much more flexible at the pom level because they are only for the project level and modifying the configuration of the profile will not affect other projects.

The specific content items that can be modified in the pom are as follows:

<repositories>
<pluginRepositories>
<dependencies>
<plugins>
<properties> (not actually available in the main POM, but used behind the scenes)
<modules>
<reporting>
<dependencyManagement>
<distributionManagement>
a subset of the <build> element, which consists of:
<defaultGoal>
<resources>
<testResources>
<finalName>

These are all properties that can be defined in the profile to replace the values in the pom.

Specific examples

The following configuration specifies an appserver.home

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.myco.plugins</groupId>
        <artifactId>spiffy-integrationTest-plugin</artifactId>
        <version>1.0</version>
        <configuration>
          <appserverHome>${appserver.home}</appserverHome>
        </configuration>
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>

We add the following profile configuration to the POM

<project>
  ...
  <profiles>
    <profile>
      <id>appserverConfig-dev</id>
      <activation>
        <property>
          <name>env</name>
          <value>dev</value>
        </property>
      </activation>
      <properties>
        <appserver.home>/path/to/dev/appserver</appserver.home>
      </properties>
    </profile>
 
    <profile>
      <id>appserverConfig-dev-2</id>
      <activation>
        <property>
          <name>env</name>
          <value>dev-2</value>
        </property>
      </activation>
      <properties>
        <appserver.home>/path/to/another/dev/appserver2</appserver.home>
      </properties>
    </profile>
  </profiles>
  ..
</project>

When the profile's configuration is activated, the defined variable values can be replaced.

In this way, other configurations can be changed accordingly.

Scan the QR code and learn to grow with the technology group:

Keywords: Java Maven JDK xml less

Added by Bunyip on Fri, 08 Nov 2019 04:21:49 +0200