简体   繁体   中英

IntelliJ: activate Maven profile when running Junit tests

I have declared some properties that are specific to Maven profiles. A part of my pom.xml:

<profiles>
            <profile>
                <id>release</id>
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>
                <properties>
                    <my.properties.file>foo.xml</my.properties.file>
                </properties>
            </profile>
            <profile>
              <id>ci</id>
              <properties>
                <my.properties.file>bar.xml</my.properties.file>
              </properties>
        </profile>
</profiles>

I encounter some problem to use the "ci" Maven profile when I start Junit tests via IntelliJ IDEA 2016.
I activate my profile via the "Maven Projects" panel, then I start tests. The problem is the "my.properties.file" property value is equal to "foo.xml", not "bar.xml".

I have no problem with command-line (I can use the "-Pci" flag). How can I tell IntelliJ to use the "ci" profile? Thx.

You should add the profiles to the Maven setting.xml file (you should find it in the path ${YOUR_MAVEN_HOME}\\apache-maven-3.1.1\\conf\\setting.xml). Then, you have to open intellij, click on View > Tool Windows > Maven Projects. There, you should see your profiles (ci and release) and select the correct one.

Hope this can help you.

Just finally solved it.

    <profile>
        <id>profile-to-be-activated-on-build</id>
        <activation>
            <activeByDefault>false</activeByDefault><!-- on your flavor -->
            <property>
                <name>mvn-profile-env-var-trigger</name>
            </property>
        </activation>
    </profile>

Goto JUnit default profile (aka configuration template). Add into JVM args:

-Dmvn-profile-env-var-trigger

You may need to manually reload maven profiles in IDE.

Also make sure on [Settings > Build Tools > Maven > Running tests] envVars is checked (or better everything).

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