|
|
@@ -0,0 +1,37 @@
|
|
|
+package com.moekr.moocoder.util;
|
|
|
+
|
|
|
+
|
|
|
+import lombok.extern.apachecommons.CommonsLog;
|
|
|
+import org.apache.maven.shared.invoker.*;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.util.Collections;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author miaomuzhi
|
|
|
+ * @since 2020/5/30
|
|
|
+ */
|
|
|
+@CommonsLog
|
|
|
+public class MavenUtil {
|
|
|
+ //TODO automatically find maven home
|
|
|
+ private static final String MAVEN_HOME = "/usr/local/Cellar/maven/3.5.3/libexec";
|
|
|
+
|
|
|
+ private MavenUtil() {}
|
|
|
+
|
|
|
+ public static boolean buildMavenProject(String srcDir){
|
|
|
+ try {
|
|
|
+ InvocationRequest request = new DefaultInvocationRequest();
|
|
|
+ request.setPomFile( new File( srcDir + File.separator + "pom.xml" ) );
|
|
|
+ request.setGoals( Collections.singletonList("compile"));
|
|
|
+
|
|
|
+ Invoker invoker = new DefaultInvoker();
|
|
|
+ invoker.setMavenHome(new File(MAVEN_HOME));
|
|
|
+ invoker.setWorkingDirectory(new File(srcDir));
|
|
|
+ InvocationResult result = invoker.execute(request);
|
|
|
+ return result.getExitCode() == 0; //non-zero exit code indicates compile or test failure
|
|
|
+ } catch (MavenInvocationException e) {
|
|
|
+ log.info(e);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|