|
| 1 | +package org.nodeclipse.enide.maven.launchexec; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +import org.eclipse.core.resources.IProject; |
| 10 | +import org.eclipse.core.resources.IResource; |
| 11 | +import org.eclipse.core.resources.ResourcesPlugin; |
| 12 | +import org.eclipse.core.runtime.CoreException; |
| 13 | +import org.eclipse.core.runtime.IPath; |
| 14 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 15 | +import org.eclipse.debug.core.DebugPlugin; |
| 16 | +import org.eclipse.debug.core.ILaunch; |
| 17 | +import org.eclipse.debug.core.ILaunchConfiguration; |
| 18 | +//import org.eclipse.debug.core.ILaunchManager; |
| 19 | +import org.eclipse.debug.core.model.ILaunchConfigurationDelegate; |
| 20 | +import org.eclipse.debug.core.model.RuntimeProcess; |
| 21 | +import org.eclipse.jface.preference.IPreferenceStore; |
| 22 | +import org.nodeclipse.enide.maven.Activator; |
| 23 | +import org.nodeclipse.enide.maven.preferences.Dialogs; |
| 24 | +//import org.nodeclipse.debug.util.Constants; |
| 25 | +//import org.nodeclipse.debug.util.VariablesUtil; |
| 26 | +//import org.nodeclipse.ui.Activator; |
| 27 | +//import org.nodeclipse.ui.preferences.Dialogs; |
| 28 | +//import org.nodeclipse.ui.preferences.PreferenceConstants; |
| 29 | +//import org.nodeclipse.ui.util.NodeclipseConsole; |
| 30 | +import org.nodeclipse.enide.maven.preferences.MavenConstants; |
| 31 | +import org.nodeclipse.enide.maven.util.NodeclipseLogger; |
| 32 | +import org.nodeclipse.enide.maven.util.VariablesUtil; |
| 33 | + |
| 34 | +/** |
| 35 | + * mvn compile exec:java -Dexec.mainClass=example.Example |
| 36 | + * *.java mvn exec<br> |
| 37 | + * see LaunchConfigurationDelegate in .debug and .phantomjs, .jjs module for comparison. |
| 38 | + * |
| 39 | + * @since 0.10 |
| 40 | + * @author Paul Verest |
| 41 | + */ |
| 42 | +public class LaunchConfigurationDelegate implements ILaunchConfigurationDelegate { |
| 43 | + |
| 44 | + @Override |
| 45 | + public void launch(ILaunchConfiguration configuration, String mode, |
| 46 | + ILaunch launch, IProgressMonitor monitor) throws CoreException { |
| 47 | + |
| 48 | + IPreferenceStore preferenceStore = Activator.getDefault().getPreferenceStore(); |
| 49 | + |
| 50 | + // Using configuration to build command line |
| 51 | + List<String> cmdLine = new ArrayList<String>(); |
| 52 | + |
| 53 | + // Maven installation path should be stored in preference. |
| 54 | + String mavenPath= preferenceStore.getString(MavenConstants.MAVEN_PATH); |
| 55 | + // Check if the maven location is correctly configured |
| 56 | + File mavenFile = new File(mavenPath); |
| 57 | + if(!mavenFile.exists()){ |
| 58 | + // If the location is not valid than show a dialog which prompts the user to goto the preferences page |
| 59 | + Dialogs.showPreferencesDialog(MavenConstants.PREFERENCES_PAGE, |
| 60 | + "Maven installation is not correctly configured.\n\n" |
| 61 | + + "Please goto Window -> Preferences -> "+MavenConstants.PREFERENCE_PAGE_NAME |
| 62 | + +" and configure the correct location"); |
| 63 | + return; |
| 64 | + } |
| 65 | + cmdLine.add(mavenPath); |
| 66 | + |
| 67 | + String mavenOptions= preferenceStore.getString(MavenConstants.MAVEN_OPTIONS); |
| 68 | + if(!mavenOptions.equals("")) { |
| 69 | + String[] sa = mavenOptions.split(" "); |
| 70 | + for(String s : sa) { |
| 71 | + cmdLine.add(s); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | +// String nodeArgs = configuration.getAttribute(GradleConstants.ATTR_GRADLE_ARGUMENTS, ""); |
| 76 | +// if(!nodeArgs.equals("")) { |
| 77 | +// String[] sa = nodeArgs.split(" "); |
| 78 | +// for(String s : sa) { |
| 79 | +// cmdLine.add(s); |
| 80 | +// } |
| 81 | +// } |
| 82 | + |
| 83 | + //mvn compile exec:java -Dexec.mainClass=example.Example |
| 84 | + cmdLine.add("compile"); |
| 85 | + cmdLine.add("exec:java"); |
| 86 | + |
| 87 | + String file = configuration.getAttribute("KEY_FILE_PATH", ""); |
| 88 | + IResource res = ResourcesPlugin.getWorkspace().getRoot().findMember(file); |
| 89 | + IProject project = res.getProject(); |
| 90 | + IPath relPath = res.getProjectRelativePath(); // =src/main/java/maven/MainClass.java |
| 91 | + String packageClass = ""+relPath; |
| 92 | + // remove maven source folder |
| 93 | + if (packageClass.startsWith("src/main/java/")){ |
| 94 | + packageClass = packageClass.substring("src/main/java/".length()); |
| 95 | + } else if (packageClass.startsWith("src/")){ |
| 96 | + packageClass = packageClass.substring("src/".length()); |
| 97 | + } |
| 98 | + // cut .java |
| 99 | + packageClass = packageClass.substring(0, packageClass.lastIndexOf('.')); |
| 100 | + packageClass = packageClass.replace('/', '.'); |
| 101 | + cmdLine.add("-Dexec.mainClass="+packageClass ); |
| 102 | + |
| 103 | +// String workingDirectory = configuration.getAttribute(MavenConstants.ATTR_WORKING_DIRECTORY, ""); |
| 104 | +// File workingPath = null; |
| 105 | +// String sProjectPath = project.getLocation().toOSString(); |
| 106 | +// if(workingDirectory.length() == 0) { |
| 107 | +// workingPath = new File(sProjectPath); |
| 108 | +// } else { |
| 109 | +// workingDirectory = VariablesUtil.resolveValue(workingDirectory); |
| 110 | +// if(workingDirectory == null) { |
| 111 | +// workingPath = new File(sProjectPath); |
| 112 | +// } else { |
| 113 | +// workingPath = new File(workingDirectory); |
| 114 | +// } |
| 115 | +// } |
| 116 | + String workingDirectory = configuration.getAttribute(MavenConstants.ATTR_WORKING_DIRECTORY, ""); |
| 117 | + File workingPath = null; |
| 118 | + if(workingDirectory.length() > 0) { |
| 119 | + workingDirectory = VariablesUtil.resolveValue(workingDirectory); |
| 120 | + if(workingDirectory != null) { |
| 121 | + workingPath = new File(workingDirectory); |
| 122 | + } |
| 123 | + } |
| 124 | + if (workingPath == null){ |
| 125 | + workingPath = new File(project.getLocation().toOSString()); |
| 126 | + } |
| 127 | + |
| 128 | + Map<String, String> envm = new HashMap<String, String>(); |
| 129 | + envm = configuration.getAttribute(MavenConstants.ATTR_ENVIRONMENT_VARIABLES, envm); |
| 130 | + String[] envp = new String[envm.size() + 2]; |
| 131 | + int idx = 0; |
| 132 | + for(String key : envm.keySet()) { |
| 133 | + String value = envm.get(key); |
| 134 | + envp[idx++] = key + "=" + value; |
| 135 | + } |
| 136 | + envp[idx++] = "JAVA_HOME=" + System.getProperty("java.home"); //System.getenv("JAVA_HOME"); |
| 137 | +//ERROR: M2_HOME not found in your environment. |
| 138 | +//Please set the M2_HOME variable in your environment to match the |
| 139 | +//location of the Maven installation |
| 140 | + envp[idx++] = "M2_HOME=" + System.getenv("MAVEN_HOME"); |
| 141 | + |
| 142 | + |
| 143 | +// for(String s : cmdLine) NodeclipseConsole.write(s+" "); |
| 144 | +// NodeclipseConsole.write("\n"); |
| 145 | + |
| 146 | + StringBuilder sb = new StringBuilder(100); |
| 147 | + for(String s : cmdLine) sb.append(s).append(' '); |
| 148 | + NodeclipseLogger.log(sb.append('\n').toString()); |
| 149 | + |
| 150 | + String[] cmds = {}; |
| 151 | + cmds = cmdLine.toArray(cmds); |
| 152 | + // Launch a process to debug.eg, |
| 153 | + Process p = DebugPlugin.exec(cmds, workingPath, envp); |
| 154 | + RuntimeProcess process = (RuntimeProcess)DebugPlugin.newProcess(launch, p, MavenConstants.PROCESS_MESSAGE); |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments