12
12
public class RealTest {
13
13
14
14
@ Test
15
- void helloWorldMaven () throws Exception {
15
+ void helloWorldMaven () throws Exception {
16
16
publishPluginLocally ();
17
17
// PACKAGE MAVEN HELLO WORLD WITH CURRENT JAVA PACKAGER
18
18
InvocationRequest request = new DefaultInvocationRequest ();
@@ -25,55 +25,45 @@ void helloWorldMaven() throws Exception {
25
25
request .addArg ("-e" );
26
26
Invoker invoker = new DefaultInvoker ();
27
27
InvocationResult result = invoker .execute (request );
28
- if (result .getExitCode () != 0 || result .getExecutionException () != null )
28
+ if (result .getExitCode () != 0 || result .getExecutionException () != null )
29
29
throw new RuntimeException ("Maven exit code != 0, see the cause below for details." , result .getExecutionException ());
30
30
}
31
31
32
32
@ Test
33
- void helloWorldGradle () throws Exception {
33
+ void helloWorldGradle () throws Exception {
34
34
publishPluginLocally ();
35
35
// PACKAGE GRADLE HELLO WORLD WITH CURRENT JAVA PACKAGER
36
- if (getBuilder (getGradlew ().getAbsolutePath (),
37
- "clean" , "package" , "-x" , "test" , "-x" , "javadoc" , "--stacktrace" )
38
- .directory (new File (System .getProperty ("user.dir" ) + "/test/hello-world-gradle" ))
39
- .inheritIO ().start ().waitFor ()
40
- != 0 ) throw new Exception ("Failed! Exit code is not 0, see details further below:" );
36
+ MyProcess p = new MyProcess (getGradlew ().getAbsolutePath (),
37
+ "clean" , "package" , "-x" , "test" , "-x" , "javadoc" , "--stacktrace" );
38
+ p .builder .directory (new File (System .getProperty ("user.dir" ) + "/test/hello-world-gradle" ));
39
+ p .execNow ();
41
40
}
42
41
43
42
@ Test
44
43
void publishPluginLocally () throws Exception {
45
44
System .out .println ("Building and publishing plugin locally..." );
46
45
// PUBLISH CURRENT JAVA PACKAGER TO LOCAL MAVEN REPO TO BE USED BY THE HELLO WORLD PROJECTS
47
- if (getBuilder (getGradlew ().getAbsolutePath (), "build" , "publishToMavenLocal" , "-x" , "validatePlugins" , "-x" , "test" , "-x" , "javadoc" , "--stacktrace" )
48
- .start ().waitFor ()
49
- != 0 ) throw new Exception ("Failed! Exit code is not 0, see details further below:" );
46
+ MyProcess p = new MyProcess (getGradlew ().getAbsolutePath (), "build" , "publishToMavenLocal" , "-x" , "validatePlugins" , "-x" , "test" , "-x" , "javadoc" , "--stacktrace" );
47
+ p .execNow ();
50
48
System .out .println ("Successfully built and published plugin locally." );
51
49
}
52
50
53
- private File getGradlew (){
51
+ private File getGradlew () {
54
52
return new File (System .getProperty ("user.dir" ) +
55
53
"/gradlew" + (Platform .getCurrentPlatform () == Platform .windows ? ".bat" : ".sh" ));
56
54
}
57
55
58
56
private File findMavenHome () {
59
57
File startDir ;
60
- if (Platform .getCurrentPlatform () == Platform .windows ){
58
+ if (Platform .getCurrentPlatform () == Platform .windows ) {
61
59
startDir = new File (System .getProperty ("user.home" ) + "\\ .m2\\ wrapper\\ dists" );
62
60
return startDir .listFiles ()[0 ].listFiles ()[0 ].listFiles ()[0 ];
63
- } else { // LINUX OR MAC
61
+ } else { // LINUX OR MAC
64
62
// TODO
65
63
throw new RuntimeException ("Failed to determine maven home folder! Linux is currently not supported." );
66
64
}
67
65
}
68
66
69
- private ProcessBuilder getBuilder (String ... arguments ) throws IOException {
70
- ProcessBuilder builder = new ProcessBuilder ().command (arguments )
71
- .inheritIO ();
72
- Map <String , String > environment = builder .environment ();
73
- setValueIgnoreCase (environment , "JAVA_HOME" , System .getProperty ("java.home" ));
74
- return builder ;
75
- }
76
-
77
67
private String getValueIgnoreCase (Map <String , String > map , String key ) {
78
68
for (String _key : map .keySet ()) {
79
69
if (_key != null && _key .equalsIgnoreCase (key ))
@@ -91,4 +81,42 @@ private void setValueIgnoreCase(Map<String, String> map, String key, String valu
91
81
}
92
82
}
93
83
84
+ class MyProcess {
85
+ public ProcessBuilder builder ;
86
+
87
+ public MyProcess (String ... arguments ) {
88
+ this (new ProcessBuilder ().command (arguments ));
89
+ }
90
+
91
+ public MyProcess (ProcessBuilder builder ) {
92
+ this .builder = builder ;
93
+ builder .inheritIO ();
94
+ Map <String , String > environment = builder .environment ();
95
+ setValueIgnoreCase (environment , "JAVA_HOME" , System .getProperty ("java.home" ));
96
+ }
97
+
98
+ public String commandToString () {
99
+ StringBuilder sb = new StringBuilder ();
100
+ for (String s : builder .command ()) {
101
+ sb .append (s ).append (" " );
102
+ }
103
+ return sb .toString ();
104
+ }
105
+
106
+ public void execNow () throws IOException , InterruptedException {
107
+ try {
108
+ StackTraceElement [] stackTrace = new Exception ().getStackTrace ();
109
+ StackTraceElement stackEl = stackTrace [1 ];
110
+ System .out .println (stackEl .toString ()+" -> " +commandToString ());
111
+ Process p = builder .start ();
112
+ int result = p .waitFor ();
113
+ if (result != 0 )
114
+ throw new IOException ("Process exited with " + result + " instead of 0! Something probably went wrong." );
115
+ } catch (Throwable e ) {
116
+ System .err .println ("Error during execution of: " + commandToString ());
117
+ throw e ;
118
+ }
119
+ }
120
+ }
121
+
94
122
}
0 commit comments