]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.maven/src/main/java/org/argeo/slc/maven/MavenCall.java
d85067ebbfbc2d4d1220e7692cad78809fc3a2f0
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.maven / src / main / java / org / argeo / slc / maven / MavenCall.java
1 package org.argeo.slc.maven;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.List;
7 import java.util.Map;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.argeo.slc.SlcException;
12 import org.codehaus.plexus.PlexusContainer;
13
14 /** A Maven execution. */
15 public class MavenCall implements Runnable {
16 private final static Log log = LogFactory.getLog(MavenCall.class);
17 private String basedir;
18 private String settings;
19 /** Raw command lines arguments */
20 private String cl;
21 private List<String> goals;
22 private List<String> profiles;
23 private Map<String, String> properties;
24
25 public void run() {
26 Thread.currentThread().setContextClassLoader(
27 getClass().getClassLoader());
28 List<String> args = new ArrayList<String>();
29 args.add("-e");
30 if (settings != null) {
31 args.add("--settings");
32 args.add(settings);
33 }
34 args.add("-f");
35 args.add(getBasedirFile().getPath() + "/pom.xml");
36 // FIXME manages \" \". Use Commons CLI?
37 if (cl != null) {
38 String[] clArgs = cl.split(" ");
39 args.addAll(Arrays.asList(clArgs));
40 }
41
42 if (goals != null)
43 args.addAll(goals);
44 if (profiles != null)
45 for (String profile : profiles)
46 args.add("-P" + profile);
47 if (properties != null)
48 for (String key : properties.keySet())
49 args.add("-D" + key + "=\"" + properties.get(key) + "\"");
50
51 // String[] goals = { "-o", "-e", "-f", basedir + "/pom.xml", "clean",
52 // "install" };
53
54 // String m2Home = "/opt/apache-maven-3.0.1";
55 // System.setProperty("classworlds.conf", m2Home + "/bin/m2.conf");
56 // System.setProperty("maven.home", m2Home);
57 //
58 // Launcher.main(goals);
59
60 CustomCli mavenCli = new CustomCli();
61 int exitCode = mavenCli.doMain(args.toArray(new String[args.size()]),
62 getBasedirFile().getPath(), System.out, System.err);
63 if (log.isDebugEnabled())
64 log.debug("Maven exit code: " + exitCode);
65
66 PlexusContainer plexusContainer = mavenCli.getContainer();
67 if (log.isDebugEnabled())
68 log.debug(plexusContainer.getContext().getContextData());
69 plexusContainer.dispose();
70 }
71
72 /** Removes 'file:' prefix if present */
73 protected File getBasedirFile() {
74 if (basedir == null)
75 throw new SlcException("basedir not set");
76 File dir;
77 if (basedir.startsWith("file:"))
78 dir = new File(basedir.substring("file:".length()));
79 else
80 dir = new File(basedir);
81 return dir;
82 }
83
84 public void setBasedir(String basedir) {
85 this.basedir = basedir;
86 }
87
88 public void setSettings(String settings) {
89 this.settings = settings;
90 }
91
92 public void setGoals(List<String> goals) {
93 this.goals = goals;
94 }
95
96 public void setProfiles(List<String> profiles) {
97 this.profiles = profiles;
98 }
99
100 public void setProperties(Map<String, String> properties) {
101 this.properties = properties;
102 }
103
104 public void setCl(String cl) {
105 this.cl = cl;
106 }
107
108 }