1212public class RealTest {
1313
1414 @ Test
15- void helloWorldMaven () throws Exception {
15+ void helloWorldMaven () throws Exception {
1616 publishPluginLocally ();
1717 // PACKAGE MAVEN HELLO WORLD WITH CURRENT JAVA PACKAGER
1818 InvocationRequest request = new DefaultInvocationRequest ();
@@ -25,55 +25,45 @@ void helloWorldMaven() throws Exception {
2525 request .addArg ("-e" );
2626 Invoker invoker = new DefaultInvoker ();
2727 InvocationResult result = invoker .execute (request );
28- if (result .getExitCode () != 0 || result .getExecutionException () != null )
28+ if (result .getExitCode () != 0 || result .getExecutionException () != null )
2929 throw new RuntimeException ("Maven exit code != 0, see the cause below for details." , result .getExecutionException ());
3030 }
3131
3232 @ Test
33- void helloWorldGradle () throws Exception {
33+ void helloWorldGradle () throws Exception {
3434 publishPluginLocally ();
3535 // 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 ();
4140 }
4241
4342 @ Test
4443 void publishPluginLocally () throws Exception {
4544 System .out .println ("Building and publishing plugin locally..." );
4645 // 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 ();
5048 System .out .println ("Successfully built and published plugin locally." );
5149 }
5250
53- private File getGradlew (){
51+ private File getGradlew () {
5452 return new File (System .getProperty ("user.dir" ) +
5553 "/gradlew" + (Platform .getCurrentPlatform () == Platform .windows ? ".bat" : ".sh" ));
5654 }
5755
5856 private File findMavenHome () {
5957 File startDir ;
60- if (Platform .getCurrentPlatform () == Platform .windows ){
58+ if (Platform .getCurrentPlatform () == Platform .windows ) {
6159 startDir = new File (System .getProperty ("user.home" ) + "\\ .m2\\ wrapper\\ dists" );
6260 return startDir .listFiles ()[0 ].listFiles ()[0 ].listFiles ()[0 ];
63- } else { // LINUX OR MAC
61+ } else { // LINUX OR MAC
6462 // TODO
6563 throw new RuntimeException ("Failed to determine maven home folder! Linux is currently not supported." );
6664 }
6765 }
6866
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-
7767 private String getValueIgnoreCase (Map <String , String > map , String key ) {
7868 for (String _key : map .keySet ()) {
7969 if (_key != null && _key .equalsIgnoreCase (key ))
@@ -91,4 +81,42 @@ private void setValueIgnoreCase(Map<String, String> map, String key, String valu
9181 }
9282 }
9383
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+
94122}
0 commit comments