Last active
September 21, 2015 06:06
-
-
Save atanasenko/ddcc08403488835fdcc7 to your computer and use it in GitHub Desktop.
PomExecutor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SuppressWarnings("restriction") | |
public class PomExecutor { | |
static { | |
DebugPlugin.getDefault().getLaunchManager().addLaunchListener(new ILaunchesListener2() { | |
public void launchesRemoved(ILaunch[] launches) { | |
for(ILaunch l: launches) { | |
ILaunchConfiguration conf = l.getLaunchConfiguration(); | |
synchronized(conf) { | |
conf.notifyAll(); | |
} | |
} | |
} | |
public void launchesChanged(ILaunch[] launches) { | |
} | |
public void launchesAdded(ILaunch[] launches) { | |
} | |
public void launchesTerminated(ILaunch[] launches) { | |
for(ILaunch l: launches) { | |
ILaunchConfiguration conf = l.getLaunchConfiguration(); | |
synchronized(conf) { | |
conf.notifyAll(); | |
} | |
} | |
} | |
}); | |
} | |
private IProject project; | |
public PomExecutor(IProject project) { | |
this.project = project; | |
} | |
public void exec(String goals, IProgressMonitor monitor) | |
throws CoreException { | |
if (project == null) { | |
return; | |
} | |
ILaunchConfiguration launchConfiguration = createLaunchConfiguration(project, goals); | |
if (launchConfiguration == null) { | |
return; | |
} | |
ILaunch launch = launchConfiguration.launch("run", monitor, false, true); | |
synchronized(launchConfiguration) { | |
while(!launch.isTerminated()) { | |
try{ launchConfiguration.wait(5000L); } catch(InterruptedException e){} | |
} | |
} | |
} | |
private ILaunchConfiguration createLaunchConfiguration(IContainer basedir, String goals) { | |
try { | |
ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager(); | |
ILaunchConfigurationType launchConfigurationType = launchManager.getLaunchConfigurationType(MavenLaunchConstants.LAUNCH_CONFIGURATION_TYPE_ID); | |
ILaunchConfigurationWorkingCopy workingCopy = launchConfigurationType.newInstance(null, "Executing POM"); | |
workingCopy.setAttribute(MavenLaunchConstants.ATTR_POM_DIR, basedir.getLocation().toOSString()); | |
workingCopy.setAttribute(MavenLaunchConstants.ATTR_GOALS, goals); | |
workingCopy.setAttribute(MavenLaunchConstants.ATTR_UPDATE_SNAPSHOTS, true); | |
workingCopy.setAttribute(IDebugUIConstants.ATTR_PRIVATE, true); | |
workingCopy.setAttribute(RefreshTab.ATTR_REFRESH_SCOPE, "${project}"); | |
workingCopy.setAttribute(RefreshTab.ATTR_REFRESH_RECURSIVE, true); | |
setProjectConfiguration(workingCopy, basedir); | |
IPath path = getJREContainerPath(basedir); | |
if (path != null) { | |
workingCopy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_JRE_CONTAINER_PATH, path.toPortableString()); | |
} | |
return workingCopy; | |
} catch (CoreException ex) { | |
// TODO: report error somewhere | |
} | |
return null; | |
} | |
@SuppressWarnings("deprecation") | |
private void setProjectConfiguration(ILaunchConfigurationWorkingCopy workingCopy, IContainer basedir) { | |
IMavenProjectRegistry projectManager = MavenPlugin.getMavenProjectRegistry(); | |
IFile pomFile = basedir.getFile(new Path(IMavenConstants.POM_FILE_NAME)); | |
IMavenProjectFacade projectFacade = projectManager.create(pomFile, false, new NullProgressMonitor()); | |
if (projectFacade != null) { | |
ResolverConfiguration configuration = projectFacade.getResolverConfiguration(); | |
String activeProfiles = configuration.getActiveProfiles(); | |
if (activeProfiles != null && activeProfiles.length() > 0) { | |
workingCopy.setAttribute(MavenLaunchConstants.ATTR_PROFILES, | |
activeProfiles); | |
} | |
} | |
} | |
// TODO ideally it should use MavenProject, but it is faster to scan | |
// IJavaProjects | |
private IPath getJREContainerPath(IContainer basedir) throws CoreException { | |
IProject project = basedir.getProject(); | |
if (project != null && project.hasNature(JavaCore.NATURE_ID)) { | |
IJavaProject javaProject = JavaCore.create(project); | |
IClasspathEntry[] entries = javaProject.getRawClasspath(); | |
for (int i = 0; i < entries.length; i++) { | |
IClasspathEntry entry = entries[i]; | |
if (JavaRuntime.JRE_CONTAINER.equals(entry.getPath().segment(0))) { | |
return entry.getPath(); | |
} | |
} | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment