public static void setBuildArtifactBeforeRun(
      @NotNull Project project,
      @NotNull RunConfiguration configuration,
      @NotNull Artifact artifact) {
    RunManagerEx runManager = RunManagerEx.getInstanceEx(project);
    final List<BuildArtifactsBeforeRunTask> buildArtifactsTasks =
        runManager.getBeforeRunTasks(configuration, ID);
    if (buildArtifactsTasks.isEmpty()) { // Add new task if absent
      BuildArtifactsBeforeRunTask task = new BuildArtifactsBeforeRunTask(project);
      buildArtifactsTasks.add(task);
      List<BeforeRunTask> tasks = runManager.getBeforeRunTasks(configuration);
      tasks.add(task);
      runManager.setBeforeRunTasks(configuration, tasks, false);
    }

    for (AbstractArtifactsBeforeRunTask task : buildArtifactsTasks) {
      task.setEnabled(true);
      task.addArtifact(artifact);
    }
  }
 public static void setBuildArtifactBeforeRunOption(
     @NotNull JComponent runConfigurationEditorComponent,
     Project project,
     @NotNull Artifact artifact,
     final boolean enable) {
   final DataContext dataContext =
       DataManager.getInstance().getDataContext(runConfigurationEditorComponent);
   final ConfigurationSettingsEditorWrapper editor =
       ConfigurationSettingsEditorWrapper.CONFIGURATION_EDITOR_KEY.getData(dataContext);
   if (editor != null) {
     List<BeforeRunTask> tasks = editor.getStepsBeforeLaunch();
     List<AbstractArtifactsBeforeRunTask> myTasks =
         new ArrayList<AbstractArtifactsBeforeRunTask>();
     for (BeforeRunTask task : tasks) {
       if (task instanceof AbstractArtifactsBeforeRunTask) {
         myTasks.add((AbstractArtifactsBeforeRunTask) task);
       }
     }
     if (enable && myTasks.isEmpty()) {
       AbstractArtifactsBeforeRunTask task = new BuildArtifactsBeforeRunTask(project);
       task.addArtifact(artifact);
       task.setEnabled(true);
       editor.addBeforeLaunchStep(task);
     } else {
       for (AbstractArtifactsBeforeRunTask task : myTasks) {
         if (enable) {
           task.addArtifact(artifact);
           task.setEnabled(true);
         } else {
           task.removeArtifact(artifact);
           if (task.getArtifactPointers().isEmpty()) {
             task.setEnabled(false);
           }
         }
       }
     }
   }
 }