IntelliJ:运行Junit测试时激活Maven配置文件

Jon*_*age 14 java junit intellij-idea maven

我已经声明了一些特定于Maven配置文件的属性.我的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>
Run Code Online (Sandbox Code Playgroud)

当我通过IntelliJ IDEA 2016启动Junit测试时,我遇到一些使用"ci"Maven配置文件的问题.
我通过"Maven Projects"面板激活我的配置文件,然后我开始测试.问题是"my.properties.file"属性值等于"foo.xml",而不是"bar.xml".

我没有命令行的问题(我可以使用"-Pci"标志).如何告诉IntelliJ使用"ci"配置文件?谢谢.

erv*_*dio 2

您应该将配置文件添加到 Maven setting.xml 文件(您应该在路径 ${YOUR_MAVEN_HOME}\apache-maven-3.1.1\conf\setting.xml 中找到它)。然后,您必须打开intellij,单击“View”>“Tool Windows”>“Maven Projects”。在那里,您应该看到您的配置文件(ci 和release)并选择正确的配置文件。

希望这可以帮到你。

  • 是的,它有效,但我失去了一些 IntelliJ 集成。根据示例,我无法轻松运行单个测试类/方法、重新运行失败的测试、启动测试调试器等。 (7认同)
  • 不幸的是它不起作用。我将活动配置文件添加到settings.xml,它们在 Maven 项目面板中显示自动激活,但它仍然不起作用。 (2认同)