【问题标题】:MavenCli output to eclipse/maven consoleMavenCli 输出到 eclipse/maven 控制台
【发布时间】:2020-02-21 14:16:21
【问题描述】:

我有一个使用 Maven Surefire 进行特殊测试的 elipse RCP 应用程序。 Maven 通过 m2e 集成到应用程序中。

用户在我的应用程序中构建他的项目并想要测试他的项目的一部分。

现在可以从我的代码中启动 maven 测试命令,它运行良好,但日志输出不会打印到我的应用程序控制台,而是打印到我的 IDE 控制台。

public class MyAction implements IObjectActionDelegate {

private IFolder selectedFolder;
private IPath path;

@Override
public void run(IAction action) {
    String flagValue = foo();
    String projectLocation = bar();

    PrintStream out = System.out; // <- here

    MavenCli cli = new MavenCli();
    cli.doMain(new String[] {"test", "-DmyFlag=" + flagValue}, 
            projectLocation, out, out);
}

如何让 mavenCli 打印到应用程序的控制台?

【问题讨论】:

  • 你为什么要调用 Maven 命令行,而不是像你已经提到的那样使用与 surefire 相关的常规单元/集成测试设置?
  • 我希望用户能够测试他在我的应用程序中创建的项目。他在项目的 pom 文件中指定依赖项和属性,并且可以运行 maven surefire 测试。

标签: java eclipse maven eclipse-rcp m2eclipse


【解决方案1】:

好的,我找到了一种从 MavenCLI 打印到应用程序控制台的方法。在用户触发的操作中,我正在启动一个作业:

public class MyAction implements IObjectActionDelegate {

private IFolder selectedFolder;
private IPath path;

@Override
public void run(IAction action) {
    String flagValue = foo();
    String projectLocation = bar();

    MyJob runTestCaseJob = new MyJob(flagValue , projectLocation);
        runTestCaseJob.schedule();
}

在 Job 类中,我正在启动 MavenCLI:

public class MyJob extends Job {

private static final String TASK_NAME = "Starting the Maven Command";

private String myFlag;
private String projectLocation;

private final PrintStream out = getConsole();
private final MavenCli cli = new MavenCli();

public MyJob(String myFlag, String projectLocation) {
    super(TASK_NAME);
    this.myFlag = myFlag;
    this.projectLocation = projectLocation;
}

@Override
protected IStatus run(IProgressMonitor monitor) {
    cli.doMain(new String[] {"test", "-DmyFlag=" + myFlag}, 
            projectLocation, out, out);
    return Status.OK_STATUS;
}

private PrintStream getConsole() {
    MessageConsole console = findMessageConsole("Console");
    console.activate();
    return new PrintStream(console.newOutputStream());
}

private MessageConsole findMessageConsole(String title) {
    IConsole[] existingConsoles = ConsolePlugin.getDefault().getConsoleManager().getConsoles();
    for (IConsole existingConsole : existingConsoles) {
        if (existingConsole.getName().equals(title)) {
            return (MessageConsole) existingConsole;
        }
    }
    MessageConsole console = new MessageConsole(title, null);
    ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] {console});
    return console;
}

【讨论】:

    猜你喜欢
    • 2020-05-16
    • 2015-02-22
    • 2019-11-12
    • 2018-05-15
    • 2013-05-29
    • 1970-01-01
    • 2015-10-27
    • 2013-08-12
    • 2012-06-01
    相关资源
    最近更新 更多