MAVEN attribute definition and use

From: http://www.tmser.com/post-178.html

Maven has three built-in features: Properties, Profiles, and Resource Filtering to support build flexibility.

MAVEN property

There are actually six types of Maven properties:

  • Built-in properties: There are two commonly used built-in properties - ${basedir} represents the project root directory, that is, the directory containing the pom.xml file; ${version} represents the project version.
  • POM attribute: The value of the corresponding element in the pom. For example, ${project.artifactId} corresponds to the value of the <project><artifactId> element. What specific POM properties can be used, you can check the attachment at the end of this page - Super POM
  • Custom Properties: Custom Maven properties under the <properties> element in the pom. E.g

 

<project>  
        <properties>  
            <my.prop>hello</my.prop>  
        </properties>  
    </project>
  • Settings property: the same as the POM property. For example, ${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, for example ${user.home} points to the user directory. All Java system properties can be viewed through the command line mvn help:system
  • Environment variable properties: All environment variables can be referenced using Maven properties starting with env. For example ${env.JAVA_HOME} refers to the value of the JAVA_HOME environment variable. You can also view all environment variables through the command line mvn help:system.

 

Resource filtering

Maven's properties filter function can help you automatically replace variables wrapped in ${} in the configuration file.

In order to facilitate the construction of different environments, we usually configure different configurations in the pom in the form of properties.

By default, Maven properties are only resolved in the POM. Resource filtering means that Maven properties can also be parsed in resource files (src/main/resources, src/test/resources).

Add the following configuration to the POM to enable resource filtering

 

<build>  
        <resources>  
            <resource>  
                <directory>${project.basedir}/src/main/resources</directory>  
                <filtering>true</filtering>  
            </resource>  
        </resources>  
        <testResources>  
            <testResource>  
                <directory>${project.basedir}/src/test/resources</directory>  
                <filtering>true</filtering>  
            </testResource>  
        </testResources>  
    </build>

 

In addition to filtering the main resource directory and test resource directory, Maven can also filter the resource directories (such as css and js directories) of Web projects. At this time, you need to configure the maven-war-plugin plugin

 

<plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-war-plugin</artifactId>  
    <version>2.1-beta-1</version>  
    <configuration>  
        <webResources>  
            <resource>  
                <filtering>true</filtering>  
                <directory>src/main/webapp</directory>  
                <includes>  
                    <include>**/*.css</include>  
                    <include>**/*.js</include>  
                </includes>  
            </resource>  
        </webResources>  
    </configuration>  
</plugin>

MAVEN PROFILE

Each Profile can be regarded as a part of the configuration of POM. We can apply different Profiles according to different environments, so as to achieve the purpose of using different POM configurations in different environments.

 

A profile can be declared in the following three files:

  • pom.xml: Obviously, the profile declared here is only valid for the current project
  • User settings.xml: The profile in .m2/settings.xml is valid for the user's Maven project
  • Global settings.xml: conf/settings.xml, valid for all Maven projects on this machine

It's worth noting that the elements that can be declared in profile pom.xml are not the same elements that can be declared in settings.xml:

  • Profile declarable elements in pom.xml:
<project>  
        <repositories></repositories>  
        <pluginRepositories></pluginRepositories>  
        <distributionManagement></distributionManagement>  
        <dependencies></dependencies>  
        <dependencyManagement></dependencyManagement>  
        <modules></modules>  
        <properties></properties>  
        <reporting></reporting>  
        <build>  
            <plugins></plugins>  
            <defaultGoal></defaultGoal>  
            <resources></resources>  
            <testResources></testResources>  
            <finalName></finalName>  
        </build>  
    </project>

Profile declarable elements in settings.xml:

<project>  
        <repositories></repositories>  
        <pluginRepositories></pluginRepositories>  
        <properties></properties>  
    </project>

profile configuration example:

 

    <project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>cc.mzone</groupId>
    <artifactId>myjar</artifactId>
    <version>0.1</version>
    <packaging>jar</packaging>
    <build>
    <resources>
    <resource>
    <directory>src/main/resources</directory>
    <includes>
    <include>*.*</include>
    </includes>
    <filtering>true</filtering>
    </resource>
    </resources>
    </build>
     
    <properties>
    <jdbc.url>jdbc:mysql://localhost:3306/abc</jdbc.url>
    <jdbc.username>root</jdbc.username>
    <jdbc.password>root</jdbc.password>
    </properties>
     
    <profiles>
    <profile>
    <id>product</id>
    <properties>
    <jdbc.url>jdbc:mysql://localhost:3306/abc123</jdbc.url>
    <jdbc.username>rootuser</jdbc.username>
    <jdbc.password>rootpwd</jdbc.password>
    </properties>
    </profile>
    </profiles>
    </project>

 Here we define the relevant configuration of the database in the pom file, and define a profile whose id is product, and also define the relevant configuration of the database in this profile. In this way, when we use the mvn package command, we can use the default jdbc settings. When we use mvn package -P product , maven will automatically use the database configuration in the profile with the id of product. This is an overlay for maven to read the property configuration file.

 

Activate PROFILE

There are several ways to activate a Profile:

  1. Command line activation, if there are two profiles with profile id devx and devy:

    mvn clean install  -Pdevx,devy  

  2. settings file is explicitly activated
    <settings>  
            ...  
            <activeProfiles>  
                <activeProfile>devx</activeProfile>  
                <activeProfile>devy</activeProfile>  
            </activeProfiles>  
            ...  
        </settings>
  3. System property activation, the user can configure to activate the profile when a system property exists or its value is equal to the expected value, such as:
    <profiles>  
            <profile>  
                <activation>  
                    <property>  
                        <name>actProp</name>  
                        <value>x</value>  
                    </property>  
                </activation>  
            </profile>  
        </profiles>
    Don't forget that system properties can be declared on the command line. Such as:
    1. mvn clean install -DactProp=x  
    This is actually a way to activate profiles from the command line, and multiple profiles can be activated using the same system property. Don't forget, the system properties can be viewed through mvn help:system to view the activation of the operating system environment,

 

<profiles>  
        <profile>  
            <activation>  
                <os>  
                    <name>Windows XP</name>  
                    <family>Windows</family>  
                    <arch>x86</arch>  
                    <version>5.1.2600</version>  
                </os>  
            </activation>  
        </profile>  
    </profiles>

The family values ​​here include Window, UNIX, Mac, etc., while the other items correspond to os.name, os.arch, os.version of the system properties

 

5. Whether the file exists or not is activated

Maven can root 5. Determine whether to activate the profile according to the existence of a file in the project

 

<profiles>  
        <profile>  
            <activation>  
                <file>  
                    <missing>x.properties</missing>  
                    <exists>y.properties</exists>  
                </file>  
            </activation>  
        </profile>  
    </profiles>

Notice: The plugin maven-help-plugin provides a goal to help users understand the currently active profile:

  1. mvn help:active-profiles  

There is also a target to list all current profiles:

  1. mvn help:all-profiles  

 

A simple reference can also be seen: The use of Maven properties (properties) tags

                                maven built-in properties (${} properties)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325295224&siteId=291194637