]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.maven/src/main/java/org/argeo/slc/maven/MavenCall.java
Clean up directories
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.maven / src / main / java / org / argeo / slc / maven / MavenCall.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.maven;
17
18 import java.io.File;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.argeo.slc.SlcException;
27 import org.codehaus.plexus.PlexusContainer;
28
29 /** A Maven execution. */
30 public class MavenCall implements Runnable {
31 private final static Log log = LogFactory.getLog(MavenCall.class);
32 private String basedir;
33 private String settings;
34 /** Raw command lines arguments */
35 private String cl;
36 private List<String> goals;
37 private List<String> profiles;
38 private Map<String, String> properties;
39
40 private Boolean success = null;
41
42 public void run() {
43 Thread.currentThread().setContextClassLoader(
44 getClass().getClassLoader());
45 List<String> args = new ArrayList<String>();
46 args.add("-e");
47 if (settings != null && !settings.trim().equals("")) {
48 args.add("--settings");
49 args.add(settings);
50 }
51 args.add("-f");
52 args.add(getBasedirFile().getPath() + "/pom.xml");
53 // FIXME manages \" \". Use Commons CLI?
54 if (cl != null) {
55 String[] clArgs = cl.split(" ");
56 args.addAll(Arrays.asList(clArgs));
57 }
58
59 if (goals != null)
60 args.addAll(goals);
61 if (profiles != null)
62 for (String profile : profiles)
63 args.add("-P" + profile);
64 if (properties != null)
65 for (String key : properties.keySet())
66 args.add("-D" + key + "=\"" + properties.get(key) + "\"");
67
68 // String[] goals = { "-o", "-e", "-f", basedir + "/pom.xml", "clean",
69 // "install" };
70
71 // String m2Home = "/opt/apache-maven-3.0.1";
72 // System.setProperty("classworlds.conf", m2Home + "/bin/m2.conf");
73 // System.setProperty("maven.home", m2Home);
74 //
75 // Launcher.main(goals);
76 log.info("Maven call: " + args);
77
78 CustomCli mavenCli = new CustomCli();
79 int exitCode = mavenCli.doMain(args.toArray(new String[args.size()]),
80 getBasedirFile().getPath(), System.out, System.err);
81 if (log.isDebugEnabled())
82 log.debug("Maven exit code: " + exitCode);
83 if (exitCode == 0)
84 success = true;
85 else
86 success = false;
87
88 PlexusContainer plexusContainer = mavenCli.getContainer();
89 if (log.isDebugEnabled())
90 log.debug(plexusContainer.getContext().getContextData());
91 plexusContainer.dispose();
92 }
93
94 /** Removes 'file:' prefix if present */
95 protected File getBasedirFile() {
96 if (basedir == null)
97 throw new SlcException("basedir not set");
98 File dir;
99 if (basedir.startsWith("file:"))
100 dir = new File(basedir.substring("file:".length()));
101 else
102 dir = new File(basedir);
103 return dir;
104 }
105
106 public void setBasedir(String basedir) {
107 this.basedir = basedir;
108 }
109
110 public void setSettings(String settings) {
111 this.settings = settings;
112 }
113
114 public void setGoals(List<String> goals) {
115 this.goals = goals;
116 }
117
118 public void setProfiles(List<String> profiles) {
119 this.profiles = profiles;
120 }
121
122 public void setProperties(Map<String, String> properties) {
123 this.properties = properties;
124 }
125
126 public void setCl(String cl) {
127 this.cl = cl;
128 }
129
130 public Boolean getSuccess() {
131 return success == null ? false : success;
132 }
133
134 }