Skip to content

Commit 8a4d79d

Browse files
committed
#117 add maven exec:java
1 parent 0233e64 commit 8a4d79d

File tree

9 files changed

+493
-4
lines changed

9 files changed

+493
-4
lines changed

org.nodeclipse.enide.maven/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
3-
Bundle-Name: pom.xml Run As Maven build
3+
Bundle-Name: pom.xml Run As Maven build; *.java Run as compile exec:java
44
Bundle-SymbolicName: org.nodeclipse.enide.maven;singleton:=true
55
Bundle-Version: 0.9.0.qualifier
66
Bundle-Activator: org.nodeclipse.enide.maven.Activator
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,128 @@
11

22

3+
# Enide Maven
4+
5+
This is alternative and completion to m2e (Maven Eclipse Integration).
6+
7+
Launch build or execute Java class.
8+
Project does not need to be Maven project. Just `pom.xml` would be enough.
9+
(The project may be General, old classic Java, broken Maven or Gradle project.)
10+
11+
No miracles, this plugins just launches `mvn` with options, as you can do from command line.
12+
Full path to maven `mvn.bat` or `mvn` is used, so you can try different versions.
13+
Specify options to `mvn` that will be Workspace-wide.
14+
Executed string is shown is Console, so you can check and learn.
15+
316
## org.nodeclipse.enide.maven
417

518
pom.xml Run As `mvn package` Maven build
19+
*.java Run As `mvn compile exec:java -Dexec.mainClass=package.Class`
620

721
Minisite <http://www.nodeclipse.org/enide/maven/>
822
GitHub <https://github.com/Nodeclipse/nodeclipse-1/tree/master/org.nodeclipse.enide.maven>
923

24+
### TODO
25+
26+
1)
27+
28+
What is gradle analog for maven compile and run
29+
`mvn compile exec:java -Dexec.mainClass=example.Example`
30+
31+
2)
32+
33+
`-Dexec.mainClass=maven.MainClass` becomes `maven.MainClass`
34+
35+
package maven;
36+
37+
public class MainClass {
38+
public static void main(String[] args) {
39+
System.out.println("app is running!");
40+
}
41+
}
42+
43+
from Eclipse using
44+
45+
Process p = DebugPlugin.exec(cmds, workingPath, envp);
46+
RuntimeProcess process = (RuntimeProcess)DebugPlugin.newProcess(launch, p, MavenConstants.PROCESS_MESSAGE);
47+
48+
for process
49+
50+
D:\Code\springsource\3.0\apache-maven-3.0.3\bin\mvn.bat compile exec:java -Dexec.mainClass=maven.MainClass
51+
52+
[INFO] Scanning for projects...
53+
[INFO]
54+
[INFO] ------------------------------------------------------------------------
55+
[INFO] Building org.example.maven 0.0.1-SNAPSHOT
56+
[INFO] ------------------------------------------------------------------------
57+
[INFO] ------------------------------------------------------------------------
58+
[INFO] BUILD FAILURE
59+
[INFO] ------------------------------------------------------------------------
60+
[INFO] Total time: 0.672s
61+
[INFO] Finished at: Fri Jan 24 23:59:50 CST 2014
62+
[INFO] Final Memory: 4M/15M
63+
[INFO] ------------------------------------------------------------------------
64+
[ERROR] Unknown lifecycle phase "maven.MainClass". You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-site, site, post-site, site-deploy, pre-clean, clean, post-clean. -> [Help 1]
65+
[ERROR]
66+
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
67+
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
68+
[ERROR]
69+
[ERROR] For more information about the errors and possible solutions, please read the following articles:
70+
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/LifecyclePhaseNotFoundException
71+
72+
but from cmd shell using `mvn` or full filename is OK
73+
74+
D:\Progs\Enide-Monster-08-kepler-win32\runtime-EclipseApplication\org.example.maven>mvn compile exec:java -Dexec.mainClass=maven.MainClass
75+
[INFO] Scanning for projects...
76+
[INFO]
77+
[INFO] ------------------------------------------------------------------------
78+
[INFO] Building org.example.maven 0.0.1-SNAPSHOT
79+
[INFO] ------------------------------------------------------------------------
80+
[INFO]
81+
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ org.example.maven ---
82+
[WARNING] Using platform encoding (Cp1251 actually) to copy filtered resources, i.e. build is platform dependent!
83+
[INFO] Copying 0 resource
84+
[INFO]
85+
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ org.example.maven ---
86+
[INFO] Nothing to compile - all classes are up to date
87+
[INFO]
88+
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ org.example.maven >>>
89+
[INFO]
90+
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ org.example.maven <<<
91+
[INFO]
92+
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ org.example.maven ---
93+
app is running!
94+
[INFO] ------------------------------------------------------------------------
95+
[INFO] BUILD SUCCESS
96+
[INFO] ------------------------------------------------------------------------
97+
[INFO] Total time: 1.342s
98+
[INFO] Finished at: Fri Jan 24 23:56:41 CST 2014
99+
[INFO] Final Memory: 5M/15M
100+
[INFO] ------------------------------------------------------------------------
101+
D:\Progs\Enide-Monster-08-kepler-win32\runtime-EclipseApplication\org.example.maven>D:\Code\springsource\3.0\apache-maven-3.0.3\bin\mvn.bat compile exec:java -Dexec.mainClass=maven
102+
.MainClass
103+
[INFO] Scanning for projects...
104+
[INFO]
105+
[INFO] ------------------------------------------------------------------------
106+
[INFO] Building org.example.maven 0.0.1-SNAPSHOT
107+
[INFO] ------------------------------------------------------------------------
108+
[INFO]
109+
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ org.example.maven ---
110+
[WARNING] Using platform encoding (Cp1251 actually) to copy filtered resources, i.e. build is platform dependent!
111+
[INFO] Copying 0 resource
112+
[INFO]
113+
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ org.example.maven ---
114+
[INFO] Nothing to compile - all classes are up to date
115+
[INFO]
116+
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ org.example.maven >>>
117+
[INFO]
118+
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ org.example.maven <<<
119+
[INFO]
120+
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ org.example.maven ---
121+
app is running!
122+
[INFO] ------------------------------------------------------------------------
123+
[INFO] BUILD SUCCESS
124+
[INFO] ------------------------------------------------------------------------
125+
[INFO] Total time: 1.435s
126+
[INFO] Finished at: Fri Jan 24 23:57:46 CST 2014
127+
[INFO] Final Memory: 5M/15M
128+
[INFO] ------------------------------------------------------------------------

org.nodeclipse.enide.maven/plugin.xml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,65 @@
6161
</shortcut>
6262
</extension>
6363

64+
<extension
65+
point="org.eclipse.debug.core.launchConfigurationTypes">
66+
<launchConfigurationType
67+
delegate="org.nodeclipse.enide.maven.launchexec.LaunchConfigurationDelegate"
68+
id="org.nodeclipse.enide.maven.launchexec.LaunchConfigurationType"
69+
modes="run"
70+
name="Maven exec">
71+
</launchConfigurationType>
72+
</extension>
73+
<extension
74+
point= "org.eclipse.debug.ui.launchConfigurationTypeImages" >
75+
<launchConfigurationTypeImage
76+
configTypeID= "org.nodeclipse.enide.maven.launchexec.LaunchConfigurationType"
77+
icon= "icons/feather-ma16_bottom_right_corner_run_half_size.png"
78+
id= "org.nodeclipse.enide.maven.launcheexec.LaunchConfigurationTypeImage" >
79+
</launchConfigurationTypeImage>
80+
</extension>
81+
<extension
82+
point="org.eclipse.debug.ui.launchConfigurationTabGroups">
83+
<launchConfigurationTabGroup
84+
class= "org.nodeclipse.enide.maven.launchexec.LaunchConfigurationTabGroup"
85+
id= "org.nodeclipse.enide.maven.launchexec.LaunchConfigurationTabGroup"
86+
type= "org.nodeclipse.enide.maven.launchexec.LaunchConfigurationType" >
87+
</launchConfigurationTabGroup>
88+
</extension>
89+
90+
<!-- *.java run as maven compile exec:java -->
91+
<extension
92+
point= "org.eclipse.debug.ui.launchShortcuts" >
93+
<shortcut
94+
class= "org.nodeclipse.enide.maven.launchexec.LaunchShortcut"
95+
icon= "icons/feather-ma16_bottom_right_corner_run_half_size.png"
96+
id= "org.nodeclipse.enide.maven.launchexec.LaunchShortcut"
97+
label= "mvn compile exec:java Maven execution"
98+
modes= "run" >
99+
<configurationType
100+
id= "org.nodeclipse.enide.maven.launchexec.LaunchConfigurationType" >
101+
</configurationType>
102+
<contextualLaunch>
103+
<enablement>
104+
<with
105+
variable= "selection" >
106+
<count
107+
value= "1" >
108+
</count>
109+
<iterate>
110+
<or>
111+
<test
112+
property= "org.eclipse.debug.ui.matchesPattern"
113+
value= "*.java" >
114+
</test>
115+
</or>
116+
</iterate>
117+
</with>
118+
</enablement>
119+
</contextualLaunch>
120+
</shortcut>
121+
</extension>
122+
64123
<extension
65124
point="org.eclipse.help.toc">
66125
<toc

org.nodeclipse.enide.maven/src/org/nodeclipse/enide/maven/launch/LaunchConfigurationDelegate.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ public void launch(ILaunchConfiguration configuration, String mode,
5959
}
6060
cmdLine.add(mavenPath);
6161

62-
String nodeOptions= preferenceStore.getString(MavenConstants.MAVEN_OPTIONS);
63-
if(!nodeOptions.equals("")) {
64-
String[] sa = nodeOptions.split(" ");
62+
String mavenOptions= preferenceStore.getString(MavenConstants.MAVEN_OPTIONS);
63+
if(!mavenOptions.equals("")) {
64+
String[] sa = mavenOptions.split(" ");
6565
for(String s : sa) {
6666
cmdLine.add(s);
6767
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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

Comments
 (0)