IDEADEV-41380: Artifacts: results of manual editing of "Build artifacts" setting...
[fedora-idea.git] / plugins / maven / src / main / java / org / jetbrains / idea / maven / tasks / MavenBeforeRunTasksProvider.java
blobbffedcdf4ea40769465792ed78a10f167fc93894
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.jetbrains.idea.maven.tasks;
18 import com.intellij.execution.BeforeRunTaskProvider;
19 import com.intellij.execution.configurations.RunConfiguration;
20 import com.intellij.ide.DataAccessors;
21 import com.intellij.openapi.actionSystem.DataContext;
22 import com.intellij.openapi.application.ApplicationManager;
23 import com.intellij.openapi.application.ModalityState;
24 import com.intellij.openapi.progress.ProgressIndicator;
25 import com.intellij.openapi.progress.Task;
26 import com.intellij.openapi.project.Project;
27 import com.intellij.openapi.util.Key;
28 import com.intellij.openapi.util.Pair;
29 import com.intellij.openapi.vfs.LocalFileSystem;
30 import com.intellij.openapi.vfs.VirtualFile;
31 import com.intellij.util.concurrency.Semaphore;
32 import org.jetbrains.annotations.NotNull;
33 import org.jetbrains.idea.maven.navigator.SelectMavenGoalDialog;
34 import org.jetbrains.idea.maven.project.MavenProject;
35 import org.jetbrains.idea.maven.project.MavenProjectsManager;
36 import org.jetbrains.idea.maven.execution.MavenRunner;
37 import org.jetbrains.idea.maven.execution.MavenRunnerParameters;
38 import org.jetbrains.idea.maven.utils.MavenLog;
40 import java.util.Collections;
42 public class MavenBeforeRunTasksProvider extends BeforeRunTaskProvider<MavenBeforeRunTask> {
43 public static final Key<MavenBeforeRunTask> TASK_ID = Key.create("Maven.BeforeRunTask");
44 private final Project myProject;
46 public MavenBeforeRunTasksProvider(Project project) {
47 myProject = project;
50 public Key getId() {
51 return TASK_ID;
54 public String getDescription(RunConfiguration runConfiguration, MavenBeforeRunTask task) {
55 String desc = null;
56 if (task.isEnabled()) {
57 Pair<MavenProject, String> projectAndGoal = getProjectAndGoalChecked(task);
58 if (projectAndGoal != null) desc = projectAndGoal.first.getDisplayName() + ":" + projectAndGoal.second;
60 return desc == null
61 ? TasksBundle.message("maven.tasks.before.run.empty")
62 : TasksBundle.message("maven.tasks.before.run", desc);
65 private Pair<MavenProject, String> getProjectAndGoalChecked(MavenBeforeRunTask task) {
66 String path = task.getProjectPath();
67 String goal = task.getGoal();
68 if (path == null || goal == null) return null;
70 VirtualFile file = LocalFileSystem.getInstance().findFileByPath(path);
71 if (file == null) return null;
73 MavenProject project = MavenProjectsManager.getInstance(myProject).findProject(file);
74 if (project == null) return null;
76 return Pair.create(project, goal);
79 public boolean hasConfigurationButton() {
80 return true;
83 public MavenBeforeRunTask createTask(RunConfiguration runConfiguration) {
84 return new MavenBeforeRunTask();
87 public boolean configureTask(RunConfiguration runConfiguration, MavenBeforeRunTask task) {
88 SelectMavenGoalDialog dialog = new SelectMavenGoalDialog(myProject,
89 task.getProjectPath(),
90 task.getGoal(),
91 TasksBundle.message("maven.tasks.select.goal.title"));
92 dialog.show();
93 if (!dialog.isOK()) return false;
95 task.setProjectPath(dialog.getSelectedProjectPath());
96 task.setGoal(dialog.getSelectedGoal());
98 MavenTasksManager.getInstance(myProject).fireTasksChanged();
99 return true;
102 public boolean executeTask(final DataContext context, RunConfiguration configuration, final MavenBeforeRunTask task) {
103 final Semaphore targetDone = new Semaphore();
104 final boolean[] result = new boolean[1];
105 try {
106 ApplicationManager.getApplication().invokeAndWait(new Runnable() {
107 public void run() {
108 final Project project = DataAccessors.PROJECT.from(context);
109 final Pair<MavenProject, String> projectAndGoal = getProjectAndGoalChecked(task);
111 if (project == null || project.isDisposed() || projectAndGoal == null) return;
113 targetDone.down();
114 new Task.Backgroundable(project, TasksBundle.message("maven.tasks.executing"), true) {
115 public void run(@NotNull ProgressIndicator indicator) {
116 try {
117 MavenRunnerParameters params = new MavenRunnerParameters(
118 true,
119 projectAndGoal.first.getDirectory(),
120 Collections.singletonList(projectAndGoal.second),
121 MavenProjectsManager.getInstance(project).getActiveProfiles());
123 result[0] = MavenRunner.getInstance(project).runBatch(Collections.singletonList(params),
124 null,
125 null,
126 TasksBundle.message("maven.tasks.executing"),
127 indicator);
129 finally {
130 targetDone.up();
134 @Override
135 public boolean shouldStartInBackground() {
136 return MavenRunner.getInstance(project).getSettings().isRunMavenInBackground();
139 @Override
140 public void processSentToBackground() {
141 MavenRunner.getInstance(project).getSettings().setRunMavenInBackground(true);
143 }.queue();
145 }, ModalityState.NON_MODAL);
147 catch (Exception e) {
148 MavenLog.LOG.error(e);
149 return false;
151 targetDone.waitFor();
152 return result[0];