简体   繁体   English

MavenCli output 到 eclipse/maven 控制台

[英]MavenCli output to eclipse/maven console

I have an elipse RCP application which uses Maven Surefire for special tests.我有一个椭圆 RCP 应用程序,它使用 Maven Surefire 进行特殊测试。 Maven is integrated via m2e into the application. Maven 通过 m2e 集成到应用程序中。

The user builds his project in my application and wants to test parts of his project.用户在我的应用程序中构建他的项目并想要测试他的项目的一部分。

Now it's possible to start the maven test command from within my code and it works perfectly but the logging output is not printed to my application's console but to my IDE's console.现在可以从我的代码中启动 maven 测试命令,它运行良好,但日志记录 output 没有打印到我的应用程序控制台,而是打印到我的 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);
}

How can I get the mavenCli to print to my application's console?如何让 mavenCli 打印到我的应用程序控制台?

Ok, I've found a way to print to the application's console from MavenCLI.好的,我找到了一种从 MavenCLI 打印到应用程序控制台的方法。 In the action that is triggered by the user I am starting a Job:在用户触发的操作中,我正在启动一个作业:

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();
}

And in the Job class I'm starting the MavenCLI:在作业 class 中,我正在启动 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM