How to create and execute a CompoundRunConfiguration programmatically

Answered

Hi,

I am trying to create multiple MavenRunConfigurations programmatically and execute them.

Creating and executing one RunnerAndConfigurationSettings works fine with this lines of code:

RunnerAndConfigurationSettings mavenRunnerAndConfigurationSettings = MavenRunConfigurationType.createRunnerAndConfigurationSettings(null,new MavenRunnerSettings(),createMavenRunnerParameters(...), project);
        mavenRunnerAndConfigurationSettings.setName("myName");
ProgramRunnerUtil.executeConfiguration(mavenRunnerAndConfigurationSettings , DefaultRunExecutor.getRunExecutorInstance());

 

But this only works for one RunnerAndConfigurationSettings. If I try it with 2 RunnerAndConfigurationSettings only the first is executed.

 

Then I saw the CompoundRunConfiguration which do what I want. But I can´t figure out how to create and execute a CompoundRunConfiguration.

I tried it with the following code but no success. After calling the action in idea nothing happend. The code is executed because I see the log, but the CompoundRunConfiguration is not executed.

 CompoundRunConfigurationType type = ConfigurationTypeUtil.findConfigurationType(CompoundRunConfigurationType.class);

RunnerAndConfigurationSettings settings = RunManager.getInstance(project).createConfiguration(name, type);
CompoundRunConfiguration runConfiguration = (CompoundRunConfiguration)settings.getConfiguration();
runConfiguration.setAllowRunningInParallel(false);
Map<RunConfiguration, ExecutionTarget> map = new HashMap<>();
map.put(mavenRunConfig1, DefaultExecutionTarget.INSTANCE);
map.put(mavenRunConfig2, DefaultExecutionTarget.INSTANCE);
runConfiguration.setConfigurationsWithTargets(mapRunConfigurationAndDefaultTarget(runConfigs));

Executor runExecutorInstance = DefaultRunExecutor.getRunExecutorInstance();
ExecutionEnvironment env = ExecutionEnvironmentBuilder.create(runExecutorInstance, runnerAndConfigurationSettings).contentToReuse(null).dataContext(null).activeTarget().build();
log.info("is called...");
ProgramRunnerUtil.executeConfiguration(env, true, true);

 

Any ideas how I can execute programmatically created MavenRunConfigurations (more than one)?

1 comment
Comment actions Permalink

I have now found a solution. Use "executeConfigurationAsync" with Callback-Interface and start next RunConfiguration after the current terminated successfull.

 

Code-Example:

private static void executeNextRunConfiguration(List<RunnerAndConfigurationSettings> listOfRunnerAndConfigurationSettings, int nextRunnerConfigIndex) {
    Executor runExecutorInstance = DefaultRunExecutor.getRunExecutorInstance();
    RunnerAndConfigurationSettings runnerAndConfigurationSettings = listOfRunnerAndConfigurationSettings.get(nextRunnerConfigIndex);
    ExecutionEnvironment env = ExecutionEnvironmentBuilder.create(runExecutorInstance, runnerAndConfigurationSettings).contentToReuse(null).dataContext(null).activeTarget().build();
    ProgramRunnerUtil.executeConfigurationAsync(env, true, true, new ProgramRunner.Callback() {
        @Override
        public void processStarted(RunContentDescriptor descriptor) {
            descriptor.getProcessHandler().addProcessListener(new ProcessAdapter() {
                @Override
                public void processTerminated(@NotNull ProcessEvent event) {
                    if( event.getExitCode() == 0 ){
                        runNextConfigurationIfNeccessary();
                    }
                }

                private void runNextConfigurationIfNeccessary() {
                    if( (nextRunnerConfigIndex+1) < listOfRunnerAndConfigurationSettings.size() ){
                        int nextIndex = nextRunnerConfigIndex + 1;
                        executeNextRunConfiguration(listOfRunnerAndConfigurationSettings, nextIndex);
                    }
                }
            });
        }
    });
}
0

Please sign in to leave a comment.