]> git.argeo.org Git - gpl/argeo-slc.git/blob - cms/org.argeo.slc.support.maven/src/org/argeo/slc/maven/MavenCall.java
8e8499db93ae96782a599af579c80876c73f7c9a
[gpl/argeo-slc.git] / cms / org.argeo.slc.support.maven / src / 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 private Boolean success = null;
26
27 public void run() {
28 Thread.currentThread().setContextClassLoader(
29 getClass().getClassLoader());
30 List<String> args = new ArrayList<String>();
31 args.add("-e");
32 if (settings != null && !settings.trim().equals("")) {
33 args.add("--settings");
34 args.add(settings);
35 }
36 args.add("-f");
37 args.add(getBasedirFile().getPath() + "/pom.xml");
38 // FIXME manages \" \". Use Commons CLI?
39 if (cl != null) {
40 String[] clArgs = cl.split(" ");
41 args.addAll(Arrays.asList(clArgs));
42 }
43
44 if (goals != null)
45 args.addAll(goals);
46 if (profiles != null)
47 for (String profile : profiles)
48 args.add("-P" + profile);
49 if (properties != null)
50 for (String key : properties.keySet())
51 args.add("-D" + key + "=\"" + properties.get(key) + "\"");
52
53 // String[] goals = { "-o", "-e", "-f", basedir + "/pom.xml", "clean",
54 // "install" };
55
56 // String m2Home = "/opt/apache-maven-3.0.1";
57 // System.setProperty("classworlds.conf", m2Home + "/bin/m2.conf");
58 // System.setProperty("maven.home", m2Home);
59 //
60 // Launcher.main(goals);
61 log.info("Maven call: " + args);
62
63 CustomCli mavenCli = new CustomCli();
64 int exitCode = mavenCli.doMain(args.toArray(new String[args.size()]),
65 getBasedirFile().getPath(), System.out, System.err);
66 if (log.isDebugEnabled())
67 log.debug("Maven exit code: " + exitCode);
68 if (exitCode == 0)
69 success = true;
70 else
71 success = false;
72
73 PlexusContainer plexusContainer = mavenCli.getContainer();
74 if (log.isDebugEnabled())
75 log.debug(plexusContainer.getContext().getContextData());
76 plexusContainer.dispose();
77 }
78
79 /** Removes 'file:' prefix if present */
80 protected File getBasedirFile() {
81 if (basedir == null)
82 throw new SlcException("basedir not set");
83 File dir;
84 if (basedir.startsWith("file:"))
85 dir = new File(basedir.substring("file:".length()));
86 else
87 dir = new File(basedir);
88 return dir;
89 }
90
91 public void setBasedir(String basedir) {
92 this.basedir = basedir;
93 }
94
95 public void setSettings(String settings) {
96 this.settings = settings;
97 }
98
99 public void setGoals(List<String> goals) {
100 this.goals = goals;
101 }
102
103 public void setProfiles(List<String> profiles) {
104 this.profiles = profiles;
105 }
106
107 public void setProperties(Map<String, String> properties) {
108 this.properties = properties;
109 }
110
111 public void setCl(String cl) {
112 this.cl = cl;
113 }
114
115 public Boolean getSuccess() {
116 return success == null ? false : success;
117 }
118
119 }