Skip to content

Commit 53028de

Browse files
committed
# 117 run .java with mvn from PackageExploere, added dependency to JDT
1 parent 56264f6 commit 53028de

File tree

7 files changed

+243
-93
lines changed

7 files changed

+243
-93
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Require-Bundle: org.eclipse.ui,
1010
org.eclipse.ui.ide,
1111
org.eclipse.debug.ui,
1212
org.eclipse.core.variables,
13-
org.nodeclipse.common
13+
org.nodeclipse.common,
14+
org.eclipse.jdt.core
1415
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
1516
Bundle-ActivationPolicy: lazy

org.nodeclipse.enide.maven/README.md

Lines changed: 101 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,39 @@ Executed string is shown is Console, so you can check and learn.
1515

1616
## org.nodeclipse.enide.maven
1717

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

2121
Minisite <http://www.nodeclipse.org/enide/maven/>
2222
GitHub <https://github.com/Nodeclipse/nodeclipse-1/tree/master/org.nodeclipse.enide.maven>
2323

24+
[#117 Run Maven, Gradle or other batch-based script from Java (and then Eclipse)](https://github.com/Nodeclipse/nodeclipse-1/issues/117)
25+
26+
Depends on JDT (see link 1).
27+
2428
### Developing
2529

2630
`git clone`, import this project and `org.nodeclipse.coomon` (2) in Eclipse with PDE (e.g. Eclipse SDK/Classic)
2731
as "Existing Projects into Workspace".
2832

33+
from plugin.xml form click Run as Eclipse application
34+
35+
if memory is not enough, change in run configuration -> Argumnet -> VM options
36+
37+
-Dosgi.requiredJavaVersion=1.6 -Xms128m -Xmx784m -XX:MaxPermSize=128m -Xss2m
38+
39+
2940
maven build is optional for full build with other plugins.
3041

31-
### TODO
42+
#### Links
43+
44+
1. <http://stackoverflow.com/questions/775709/eclipse-pde-navigator-view-treeselection-obtaining-the-file-type-and-name>
45+
46+
### Idea
47+
48+
java-run-javac&java
49+
50+
### DONE
3251

3352
1)
3453

@@ -37,6 +56,7 @@ What is gradle analog for maven compile and run
3756

3857
2)
3958

59+
When executing Java class using maven from within Java ([Eclipse plugin actualy](https://github.com/Nodeclipse/nodeclipse-1/tree/master/org.nodeclipse.enide.maven))
4060
`-Dexec.mainClass=maven.MainClass` becomes `maven.MainClass`
4161

4262
package maven;
@@ -56,80 +76,84 @@ for process
5676

5777
D:\Code\springsource\3.0\apache-maven-3.0.3\bin\mvn.bat compile exec:java -Dexec.mainClass=maven.MainClass
5878

59-
[INFO] Scanning for projects...
60-
[INFO]
61-
[INFO] ------------------------------------------------------------------------
62-
[INFO] Building org.example.maven 0.0.1-SNAPSHOT
63-
[INFO] ------------------------------------------------------------------------
64-
[INFO] ------------------------------------------------------------------------
65-
[INFO] BUILD FAILURE
66-
[INFO] ------------------------------------------------------------------------
67-
[INFO] Total time: 0.672s
68-
[INFO] Finished at: Fri Jan 24 23:59:50 CST 2014
69-
[INFO] Final Memory: 4M/15M
70-
[INFO] ------------------------------------------------------------------------
71-
[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]
72-
[ERROR]
73-
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
74-
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
75-
[ERROR]
76-
[ERROR] For more information about the errors and possible solutions, please read the following articles:
77-
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/LifecyclePhaseNotFoundException
79+
[INFO] Scanning for projects...
80+
[INFO]
81+
[INFO] ------------------------------------------------------------------------
82+
[INFO] Building org.example.maven 0.0.1-SNAPSHOT
83+
[INFO] ------------------------------------------------------------------------
84+
[INFO] ------------------------------------------------------------------------
85+
[INFO] BUILD FAILURE
86+
[INFO] ------------------------------------------------------------------------
87+
[INFO] Total time: 0.672s
88+
[INFO] Finished at: Fri Jan 24 23:59:50 CST 2014
89+
[INFO] Final Memory: 4M/15M
90+
[INFO] ------------------------------------------------------------------------
91+
[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]
92+
[ERROR]
93+
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
94+
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
95+
[ERROR]
96+
[ERROR] For more information about the errors and possible solutions, please read the following articles:
97+
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/LifecyclePhaseNotFoundException
7898

7999
but from cmd shell using `mvn` or full filename is OK
80100

81-
D:\Progs\Enide-Monster-08-kepler-win32\runtime-EclipseApplication\org.example.maven>mvn compile exec:java -Dexec.mainClass=maven.MainClass
82-
[INFO] Scanning for projects...
83-
[INFO]
84-
[INFO] ------------------------------------------------------------------------
85-
[INFO] Building org.example.maven 0.0.1-SNAPSHOT
86-
[INFO] ------------------------------------------------------------------------
87-
[INFO]
88-
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ org.example.maven ---
89-
[WARNING] Using platform encoding (Cp1251 actually) to copy filtered resources, i.e. build is platform dependent!
90-
[INFO] Copying 0 resource
91-
[INFO]
92-
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ org.example.maven ---
93-
[INFO] Nothing to compile - all classes are up to date
94-
[INFO]
95-
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ org.example.maven >>>
96-
[INFO]
97-
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ org.example.maven <<<
98-
[INFO]
99-
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ org.example.maven ---
100-
app is running!
101-
[INFO] ------------------------------------------------------------------------
102-
[INFO] BUILD SUCCESS
103-
[INFO] ------------------------------------------------------------------------
104-
[INFO] Total time: 1.342s
105-
[INFO] Finished at: Fri Jan 24 23:56:41 CST 2014
106-
[INFO] Final Memory: 5M/15M
107-
[INFO] ------------------------------------------------------------------------
108-
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
109-
.MainClass
110-
[INFO] Scanning for projects...
111-
[INFO]
112-
[INFO] ------------------------------------------------------------------------
113-
[INFO] Building org.example.maven 0.0.1-SNAPSHOT
114-
[INFO] ------------------------------------------------------------------------
115-
[INFO]
116-
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ org.example.maven ---
117-
[WARNING] Using platform encoding (Cp1251 actually) to copy filtered resources, i.e. build is platform dependent!
118-
[INFO] Copying 0 resource
119-
[INFO]
120-
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ org.example.maven ---
121-
[INFO] Nothing to compile - all classes are up to date
122-
[INFO]
123-
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ org.example.maven >>>
124-
[INFO]
125-
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ org.example.maven <<<
126-
[INFO]
127-
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ org.example.maven ---
128-
app is running!
129-
[INFO] ------------------------------------------------------------------------
130-
[INFO] BUILD SUCCESS
131-
[INFO] ------------------------------------------------------------------------
132-
[INFO] Total time: 1.435s
133-
[INFO] Finished at: Fri Jan 24 23:57:46 CST 2014
134-
[INFO] Final Memory: 5M/15M
135-
[INFO] ------------------------------------------------------------------------
101+
D:\Progs\Enide-Monster-08-kepler-win32\runtime-EclipseApplication\org.example.maven>mvn compile exec:java -Dexec.mainClass=maven.MainClass
102+
[INFO] Scanning for projects...
103+
[INFO]
104+
[INFO] ------------------------------------------------------------------------
105+
[INFO] Building org.example.maven 0.0.1-SNAPSHOT
106+
[INFO] ------------------------------------------------------------------------
107+
[INFO]
108+
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ org.example.maven ---
109+
[WARNING] Using platform encoding (Cp1251 actually) to copy filtered resources, i.e. build is platform dependent!
110+
[INFO] Copying 0 resource
111+
[INFO]
112+
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ org.example.maven ---
113+
[INFO] Nothing to compile - all classes are up to date
114+
[INFO]
115+
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ org.example.maven >>>
116+
[INFO]
117+
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ org.example.maven <<<
118+
[INFO]
119+
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ org.example.maven ---
120+
app is running!
121+
[INFO] ------------------------------------------------------------------------
122+
[INFO] BUILD SUCCESS
123+
[INFO] ------------------------------------------------------------------------
124+
[INFO] Total time: 1.342s
125+
[INFO] Finished at: Fri Jan 24 23:56:41 CST 2014
126+
[INFO] Final Memory: 5M/15M
127+
[INFO] ------------------------------------------------------------------------
128+
129+
130+
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
131+
.MainClass
132+
[INFO] Scanning for projects...
133+
[INFO]
134+
[INFO] ------------------------------------------------------------------------
135+
[INFO] Building org.example.maven 0.0.1-SNAPSHOT
136+
[INFO] ------------------------------------------------------------------------
137+
[INFO]
138+
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ org.example.maven ---
139+
[WARNING] Using platform encoding (Cp1251 actually) to copy filtered resources, i.e. build is platform dependent!
140+
[INFO] Copying 0 resource
141+
[INFO]
142+
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ org.example.maven ---
143+
[INFO] Nothing to compile - all classes are up to date
144+
[INFO]
145+
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ org.example.maven >>>
146+
[INFO]
147+
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ org.example.maven <<<
148+
[INFO]
149+
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ org.example.maven ---
150+
app is running!
151+
[INFO] ------------------------------------------------------------------------
152+
[INFO] BUILD SUCCESS
153+
[INFO] ------------------------------------------------------------------------
154+
[INFO] Total time: 1.435s
155+
[INFO] Finished at: Fri Jan 24 23:57:46 CST 2014
156+
[INFO] Final Memory: 5M/15M
157+
[INFO] ------------------------------------------------------------------------
158+
159+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package runitself;
2+
import java.util.*;
3+
import java.io.*;
4+
5+
// http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html
6+
public class MediocreExecMaven {
7+
private static void log(String message) {
8+
System.out.println(message);
9+
}
10+
public static void main(String args[]) {
11+
try {
12+
13+
String mavenPath = "D:\\Progs\\springsource\\apache-maven-3.0.4\\bin\\mvn.bat";
14+
String mavenOptions = "-X compile exec:java -Dexec.mainClass=runclass.Runme";
15+
16+
String[] cmdLine = new String[2];
17+
cmdLine[0] = mavenPath; //cmdLine.add(mavenPath);
18+
cmdLine[1] = mavenOptions; //cmdLine.add(mavenOptions+" compile exec:java -Dexec.mainClass="+packageClass);
19+
20+
String[] envp = new String[2];
21+
//Map<String, String> envm = new HashMap<String, String>();
22+
envp[0] = "JAVA_HOME=" + System.getProperty("java.home"); //System.getenv("JAVA_HOME");
23+
envp[1] = "M2_HOME=" + System.getenv("MAVEN_HOME");
24+
25+
File workingDirectory = null;
26+
String currentDir = new File(".").getAbsolutePath();
27+
log(currentDir);
28+
String userDir = System.getProperty("user.dir"); //User working directory ; "user.home" User home directory
29+
workingDirectory = new File(userDir);
30+
log(workingDirectory.toString());
31+
32+
//
33+
Runtime rt = Runtime.getRuntime();
34+
Process proc = rt.exec(cmdLine, envp, workingDirectory);
35+
InputStream stdout = proc.getInputStream();
36+
InputStream stderr = proc.getErrorStream();
37+
InputStreamReader isr = new InputStreamReader(stdout);
38+
InputStreamReader isr2 = new InputStreamReader(stderr);
39+
BufferedReader br = new BufferedReader(isr);
40+
BufferedReader br2 = new BufferedReader(isr2);
41+
42+
String line = null;
43+
System.out.println("<OutputStream>");
44+
while ((line = br.readLine()) != null)
45+
System.out.println(line);
46+
System.out.println("</OutputStream>");
47+
48+
System.out.println("<ErrorStream>");
49+
while ((line = br2.readLine()) != null)
50+
System.out.println(line);
51+
System.out.println("</ErrorStream>");
52+
53+
int exitVal = proc.waitFor();
54+
System.out.println("Process exitValue: " + exitVal);
55+
} catch (Throwable t) {
56+
t.printStackTrace();
57+
}
58+
}
59+
}
60+
// [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoPluginFoundForPrefixException
61+
// </OutputStream>
62+
// <ErrorStream>
63+
// 'cmd' �����ڲ����ⲿ���Ҳ���ǿ����еij���
64+
// ���������
65+
// </ErrorStream>
66+
// Process exitValue: 1

org.nodeclipse.enide.maven/plugin.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</launchConfigurationTabGroup>
2929
</extension>
3030

31-
<!-- pom.xml run as maven -->
31+
<!-- pom.xml run as maven build -->
3232
<extension
3333
point= "org.eclipse.debug.ui.launchShortcuts" >
3434
<shortcut
@@ -60,6 +60,15 @@
6060
</contextualLaunch>
6161
</shortcut>
6262
</extension>
63+
64+
<!-- -->
65+
<extension point="org.eclipse.core.runtime.adapters">
66+
<factory
67+
adaptableType="org.eclipse.jdt.core.ICompilationUnit"
68+
class="org.nodeclipse.enide.jdt.CompilationUnitToFileAdapter">
69+
<adapter type="org.eclipse.core.resources.IFile" />
70+
</factory>
71+
</extension>
6372

6473
<extension
6574
point="org.eclipse.debug.core.launchConfigurationTypes">
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.nodeclipse.enide.jdt;
2+
3+
import org.eclipse.core.resources.IFile;
4+
import org.eclipse.core.runtime.IAdapterFactory;
5+
import org.eclipse.jdt.core.ICompilationUnit;
6+
7+
8+
/** The adapter factory class
9+
http://stackoverflow.com/questions/775709/eclipse-pde-navigator-view-treeselection-obtaining-the-file-type-and-name
10+
*/
11+
public class CompilationUnitToFileAdapter implements IAdapterFactory {
12+
@Override
13+
public Object getAdapter(Object adaptableObject, Class adapterType) {
14+
if (adaptableObject instanceof ICompilationUnit)
15+
// note: "adapting" it here just means returning the ref'd IFile
16+
return (IFile) ((ICompilationUnit)adaptableObject).getResource();
17+
return null;
18+
}
19+
@Override
20+
public Class[] getAdapterList() {
21+
return new Class[] {IFile.class};
22+
}
23+
}

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ public void launch(ILaunchConfiguration configuration, String mode,
6262
+" and configure the correct location");
6363
return;
6464
}
65-
cmdLine.add(mavenPath);
65+
//cmdLine.add(mavenPath);
6666

6767
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-
}
68+
// if(!mavenOptions.equals("")) {
69+
// String[] sa = mavenOptions.split(" ");
70+
// for(String s : sa) {
71+
// cmdLine.add(s);
72+
// }
73+
// }
7474

7575
// String nodeArgs = configuration.getAttribute(GradleConstants.ATTR_GRADLE_ARGUMENTS, "");
7676
// if(!nodeArgs.equals("")) {
@@ -80,9 +80,10 @@ public void launch(ILaunchConfiguration configuration, String mode,
8080
// }
8181
// }
8282

83-
//mvn compile exec:java -Dexec.mainClass=example.Example
84-
cmdLine.add("compile");
85-
cmdLine.add("exec:java");
83+
// //mvn compile exec:java -Dexec.mainClass=example.Example
84+
// cmdLine.add("compile");
85+
// cmdLine.add("exec:java");
86+
// cmdLine.add("-Dmaven.test.skip=true");
8687

8788
String file = configuration.getAttribute("KEY_FILE_PATH", "");
8889
IResource res = ResourcesPlugin.getWorkspace().getRoot().findMember(file);
@@ -98,7 +99,13 @@ public void launch(ILaunchConfiguration configuration, String mode,
9899
// cut .java
99100
packageClass = packageClass.substring(0, packageClass.lastIndexOf('.'));
100101
packageClass = packageClass.replace('/', '.');
101-
cmdLine.add("-Dexec.mainClass="+packageClass );
102+
// cmdLine.add("-Dexec.mainClass="+packageClass );
103+
104+
// as one string
105+
//cmdLine.add(mavenPath+" "+mavenOptions+" compile exec:java -Dexec.mainClass="+packageClass);
106+
107+
cmdLine.add(mavenPath);
108+
cmdLine.add(mavenOptions+" compile exec:java -Dexec.mainClass="+packageClass);
102109

103110
// String workingDirectory = configuration.getAttribute(MavenConstants.ATTR_WORKING_DIRECTORY, "");
104111
// File workingPath = null;

0 commit comments

Comments
 (0)