1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.cling;
20
21 import java.io.IOException;
22
23 import org.apache.maven.api.cli.Invoker;
24 import org.apache.maven.api.cli.InvokerException;
25 import org.apache.maven.api.cli.InvokerRequest;
26 import org.apache.maven.api.cli.ParserException;
27 import org.codehaus.plexus.classworlds.ClassWorld;
28
29 import static java.util.Objects.requireNonNull;
30
31
32
33
34 public abstract class ClingSupport {
35 static final String CORE_CLASS_REALM_ID = "plexus.core";
36
37 protected final ClassWorld classWorld;
38 protected final boolean classWorldManaged;
39
40
41
42
43 public ClingSupport() {
44 this(new ClassWorld(CORE_CLASS_REALM_ID, Thread.currentThread().getContextClassLoader()), true);
45 }
46
47
48
49
50 public ClingSupport(ClassWorld classWorld) {
51 this(classWorld, false);
52 }
53
54 private ClingSupport(ClassWorld classWorld, boolean classWorldManaged) {
55 this.classWorld = requireNonNull(classWorld);
56 this.classWorldManaged = classWorldManaged;
57 }
58
59
60
61
62 public int run(String[] args) throws IOException {
63 try (Invoker invoker = createInvoker()) {
64 return invoker.invoke(parseArguments(args));
65 } catch (ParserException e) {
66 System.err.println(e.getMessage());
67 return 1;
68 } catch (InvokerException e) {
69 return 1;
70 } finally {
71 if (classWorldManaged) {
72 classWorld.close();
73 }
74 }
75 }
76
77 protected abstract Invoker createInvoker();
78
79 protected abstract InvokerRequest parseArguments(String[] args) throws ParserException, IOException;
80 }