简体   繁体   中英

Activating Maven profile using system properties

I'm working on a project that uses Maven profiles to differentiate between environment-specific configurations. I'm running version 3.0.5 . The goal is to be able to specify the environment by passing a system property to the initial run command from the shell that corresponds to an activation property, like so:

pom.xml

<profiles>
   ...
    <profile><!-- Local Dev Environment -->
        <id>local</id>
        <activation>
            <property>
                <name>environment</name>
                <value>local</value>
            </property>
        </activation>
        <properties>
            ...
        </properties>
    </profile><!-- End Local Dev Environment -->
    ...
</profiles>

Startup Command

mvn exec:java -Denvironment=local

However, the profile isn't activated (confirmed using help:active-profiles , which produces no output). I'm pretty noobish with Maven, but shouldn't this work?

It's my understanding that running mvn with the -D option allows me to set system properties (in this case: environment should equal local ) which are then cross-referenced with the POM by Maven when running the application to determine which profile's activation criteria are met (per the required activation properties ), and then running the application with those profiles active. Is there something that I'm not grokking here?

I've got a POM here for a small test app with a test suite. I've included your profile and set a property to skip the tests.

I can activate the profile (and skip the tests) using 2 different methods:

mvn clean package -Plocal

or

mvn clean package -Denvironment=local

I haven't used the exec plugin before, I'm just looking at it now, but I don't think it should change the profile behaviour.

    <?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.test</groupId>
    <artifactId>test-app</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <profiles>
        <profile><!-- Local Dev Environment -->
            <id>local</id>
            <activation>
                <property>
                    <name>environment</name>
                    <value>local</value>
                </property>
            </activation>
            <properties>
                <maven.test.skip>true</maven.test.skip>
            </properties>
        </profile><!-- End Local Dev Environment -->
    </profiles>
</project>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM