首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过maven插件设置Java/Scala测试的系统属性?

如何通过maven插件设置Java/Scala测试的系统属性?
EN

Stack Overflow用户
提问于 2015-10-09 21:53:55
回答 1查看 1.6K关注 0票数 1

我想用两个不同的配置文件运行我的测试,每个配置文件设置一个Java属性,导致我的scala测试代码以不同的方式执行。

我尝试配置maven-surefire和maven-scalatest插件:

代码语言:javascript
复制
        <plugin>
            <groupId>org.scalatest</groupId>
            <artifactId>scalatest-maven-plugin</artifactId>
            <version>1.0</version>
            <configuration>
                <systemProperties>
                    <spark.master>local</spark.master>
                </systemProperties>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
            <configuration>
                <systemPropertyVariables>
                    <spark.master>local</spark.master>
                </systemPropertyVariables>
            </configuration>
        </plugin>

但是,当执行System.getProperty("spark.master")时,结果仍然是空的。我应该怎么做才能启用这个设置?

//

对第一个答复的答复:

我已经将我的surefire配置更改为以下内容:

代码语言:javascript
复制
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
            <executions>
                <execution>
                    <id>test</id>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <systemPropertyVariables>
                        <spark.master>${spark.master}</spark.master>
                    </systemPropertyVariables>
                </execution>
            </executions>
            <configuration>
                <forkCount>1</forkCount>
            </configuration>
        </plugin>

但显然是在错误的地方。Maven给了我这个错误:

代码语言:javascript
复制
[ERROR]     Malformed POM /home/peng/git/datapassport/pom.xml: Unrecognised tag: 'systemPropertyVariables' (position: START_TAG seen ...</goals>\n                        <systemPropertyVariables>... @170:50)  @ /home/peng/git/datapassport/pom.xml, line 170, column 50 -> [Help 2]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-09 22:55:18

(a)如果使用JUnit -将其版本升级到4.7或更高版本,并指定显式提供程序:

代码语言:javascript
复制
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit47</artifactId>
            <version>2.18.1</version>
        </dependency>
    </dependencies>

(b)在万无一失的插件配置中指明

代码语言:javascript
复制
<forkCount>1</forkCount>

(c)使用plugun的执行maven机制运行两个不同的配置文件

代码语言:javascript
复制
<executions>
    <execution>
        <id>tests-1</id>
        <goals><goal>test</goal></goals>
        <configuration>
            <systemProperyVariables ... />
        </configuration>
    </execution>
    <execution>
        <id>tests-2</id>
        <goals><goal>test</goal></goals>
        <configuration>
            <systemProperyVariables ... />
        </configuration>
    </execution>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33047695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档