22
33import fi .helsinki .cs .tmc .langs .java .exception .MavenExecutorException ;
44
5+ import com .google .common .base .Preconditions ;
6+
7+ import org .apache .commons .lang3 .SystemUtils ;
58import org .apache .maven .shared .invoker .DefaultInvocationRequest ;
69import org .apache .maven .shared .invoker .DefaultInvoker ;
710import org .apache .maven .shared .invoker .InvocationOutputHandler ;
811import org .apache .maven .shared .invoker .InvocationRequest ;
912import org .apache .maven .shared .invoker .InvocationResult ;
1013import org .apache .maven .shared .invoker .MavenInvocationException ;
11-
1214import org .codehaus .plexus .util .cli .CommandLineException ;
13-
15+ import org .rauschig .jarchivelib .ArchiveFormat ;
16+ import org .rauschig .jarchivelib .Archiver ;
17+ import org .rauschig .jarchivelib .ArchiverFactory ;
1418import org .slf4j .Logger ;
1519import org .slf4j .LoggerFactory ;
1620
1721import java .io .ByteArrayOutputStream ;
1822import java .io .File ;
23+ import java .io .IOException ;
24+ import java .io .InputStream ;
1925import java .io .PrintStream ;
26+ import java .nio .file .Files ;
2027import java .nio .file .Path ;
28+ import java .nio .file .Paths ;
29+ import java .nio .file .StandardCopyOption ;
2130import java .util .Arrays ;
2231
32+
2333public class MavenInvokatorMavenTaskRunner implements MavenTaskRunner {
2434
2535 private static final Logger log = LoggerFactory .getLogger (MavenInvokatorMavenTaskRunner .class );
@@ -33,14 +43,23 @@ public MavenExecutionResult exec(Path projectPath, String[] mavenArgs) {
3343 InvocationRequest request = new DefaultInvocationRequest ();
3444 request .setMavenOpts (MAVEN_OPTS );
3545
36- DefaultInvoker invoker = new DefaultInvoker ();
3746 String mavenHome = System .getenv ("M3_HOME" );
3847 if (mavenHome == null ) {
3948 mavenHome = System .getenv ("M2_HOME" );
4049 }
4150 if (mavenHome == null ) {
4251 mavenHome = System .getenv ("MAVEN_HOME" );
4352 }
53+ if (mavenHome == null ) {
54+ mavenHome = System .getProperty ("maven.home" );
55+ }
56+ if (mavenHome == null ) {
57+ mavenHome = useBundledMaven ().toString ();
58+ }
59+
60+ log .info ("Using maven at: {}" , mavenHome );
61+
62+ DefaultInvoker invoker = new DefaultInvoker ();
4463 invoker .setMavenHome (new File (mavenHome ));
4564
4665 final ByteArrayOutputStream outBuf = new ByteArrayOutputStream ();
@@ -75,4 +94,57 @@ public void consumeLine(String line) {
7594 throw new MavenExecutorException (e );
7695 }
7796 }
97+
98+ private Path useBundledMaven () {
99+ Path mavenHome = getConfigDirectory ();
100+ if (Files .exists (mavenHome )) {
101+ log .info ("Maven already extracted" );
102+
103+ // Add the name of the extracted folder to the path
104+ return mavenHome .resolve ("apache-maven-3.3.9" );
105+ }
106+ log .info ("Maven bundle not previously extracted, extracting..." );
107+ try {
108+ InputStream data = getClass ().getResourceAsStream ("apache-maven-3.3.9.zip" );
109+ Preconditions .checkNotNull (
110+ data , "Couldn't load bundled maven from tmc-langs-java.jar." );
111+ Path tmpFile = Files .createTempFile ("tmc-maven" , "zip" );
112+ Files .copy (data , tmpFile , StandardCopyOption .REPLACE_EXISTING );
113+ Archiver archiver = ArchiverFactory .createArchiver (ArchiveFormat .ZIP );
114+ archiver .extract (tmpFile .toFile (), mavenHome .toFile ());
115+ try {
116+ Files .deleteIfExists (tmpFile );
117+ } catch (IOException e ) {
118+ log .warn ("Deleting tmp apache-maven.zip failed" , e );
119+ }
120+
121+ // Add the name of the extracted folder to the path
122+ return mavenHome .resolve ("apache-maven-3.3.9" );
123+ } catch (IOException e ) {
124+ throw new RuntimeException (e );
125+ }
126+ }
127+
128+ static Path getConfigDirectory () {
129+ Path configPath ;
130+
131+ if (SystemUtils .IS_OS_WINDOWS ) {
132+ String appdata = System .getenv ("APPDATA" );
133+ if (appdata == null ) {
134+ configPath = Paths .get (System .getProperty ("user.home" ));
135+ } else {
136+ configPath = Paths .get (appdata );
137+ }
138+ } else {
139+ //Assume we're using Unix (Linux, Mac OS X or *BSD)
140+ String configEnv = System .getenv ("XDG_CONFIG_HOME" );
141+
142+ if (configEnv != null && configEnv .length () > 0 ) {
143+ configPath = Paths .get (configEnv );
144+ } else {
145+ configPath = Paths .get (System .getProperty ("user.home" )).resolve (".config" );
146+ }
147+ }
148+ return configPath .resolve ("tmc" );
149+ }
78150}
0 commit comments