如何从cmd(maven项目)将String值传递给java?

时间:2013-09-17 08:12:33

标签: java maven

我需要将String值(jbehave故事文件名)从cmd传递给.java文件。像这样:

mvn verify -PMyTests -DstoryName=purchasing.story

以这种方式在java中使用:

System.out.println(storyName);

我的设置:

现在由

启动测试
mvn verify -PMyTests

pom.xml 文件结构:

...
<dependencies>
    ...
</dependencies>
<build>
    ...
<build>

<profiles>
    <profile>
        <id>MyTests</id>
        <build>
            <plugins>
                ...
            </plugins>
        </build>
    </profile>

MyStories.java

public class MyStories extends JUnitStories {
    ...
    @Override
    protected List<String> storyPaths() {
        return new StoryFinder()
            .findPaths(codeLocationFromClass(embeddableClass).getFile(), asList(
                    "**/purchasing.story"
                    ), null);
    }
}

尝试了以下主题:Is there a way to use maven property in java class during compilationHow to pass java code a parameter from maven for testing,但没有结果。 简明扼要的教程,搜索技巧或文档以及实例表示赞赏。

P.S。找到的解决方案很简单,对我有用:http://syntx.co/languages-frameworks/how-to-pass-parameters-to-the-junit-tests-from-the-maven-surefire-plugin/

注释中提供的解决方案(我“复制”的主题)比这个更复杂,因此新手更容易实现错误

1 个答案:

答案 0 :(得分:0)

您需要像这样阅读System属性:

public class MyStories extends JUnitStories {
    ...
    @Override
    protected List<String> storyPaths() {
        String storyName = System.getProperty("storyName");
        return new StoryFinder()
            .findPaths(codeLocationFromClass(embeddableClass).getFile(),
                       asList("**/" + storyName),
                       null);
    }
}