View Javadoc

1   package org.kuali.common.util.service;
2   
3   import java.io.File;
4   import java.util.ArrayList;
5   import java.util.Collections;
6   import java.util.List;
7   import java.util.Properties;
8   
9   import org.apache.commons.lang3.StringUtils;
10  import org.kuali.common.util.CollectionUtils;
11  import org.kuali.common.util.LocationUtils;
12  import org.kuali.common.util.PropertyUtils;
13  
14  public class DefaultMavenService extends DefaultExecService implements MavenService {
15  
16  	@Override
17  	public void execute(MavenContext context) {
18  
19  		// Update options with MavenContext attributes
20  		handleOptions(context);
21  
22  		// Convert options/goals/phases into an args list
23  		List<String> args = getArgs(context);
24  
25  		// Create an execution context
26  		DefaultExecContext dec = new DefaultExecContext();
27  		dec.setExecutable(context.getExecutable());
28  		dec.setWorkingDirectory(context.getWorkingDir());
29  		dec.setArgs(args);
30  
31  		// TODO Re-factor things so only MAVEN_OPTS gets inherited instead of everything
32  		if (context.isInheritMavenOpts()) {
33  			dec.setAddSystemEnvironment(true);
34  		}
35  
36  		// Execute Maven making sure we get 0 as a return value
37  		executeAndValidate(dec);
38  	}
39  
40  	@Override
41  	public void execute(File workingDir, List<String> options, List<String> goals, List<String> phases) {
42  		MavenContext context = new MavenContext();
43  		context.setWorkingDir(workingDir);
44  		context.setOptions(options);
45  		context.setGoals(goals);
46  		context.setPhases(phases);
47  		execute(context);
48  	}
49  
50  	protected List<String> getOptions(MavenContext context) {
51  		List<String> options = new ArrayList<String>();
52  		if (context.isBatchMode()) {
53  			options.add("--batch-mode");
54  		}
55  		if (context.isDebug()) {
56  			options.add("--debug");
57  		}
58  		if (context.isErrors()) {
59  			options.add("--errors");
60  		}
61  		if (context.isOffline()) {
62  			options.add("--offline");
63  		}
64  		if (context.isQuiet()) {
65  			options.add("--quiet");
66  		}
67  		if (context.getPom() != null) {
68  			String cpath = LocationUtils.getCanonicalPath(context.getPom());
69  			options.add("--file");
70  			options.add(cpath);
71  		}
72  		if (!CollectionUtils.isEmpty(context.getProfiles())) {
73  			String csv = CollectionUtils.getCSV(context.getProfiles());
74  			options.add("--activate-profiles");
75  			options.add(csv);
76  		}
77  		return options;
78  	}
79  
80  	protected void handleOptions(MavenContext context) {
81  		List<String> options = getOptions(context);
82  		if (context.getOptions() == null) {
83  			context.setOptions(options);
84  		} else {
85  			context.getOptions().addAll(options);
86  		}
87  	}
88  
89  	protected List<String> getArgs(MavenContext context) {
90  		List<String> args = new ArrayList<String>();
91  		if (!CollectionUtils.isEmpty(context.getOptions())) {
92  			args.addAll(context.getOptions());
93  		}
94  		if (!CollectionUtils.isEmpty(context.getGoals())) {
95  			args.addAll(context.getGoals());
96  		}
97  		if (!CollectionUtils.isEmpty(context.getPhases())) {
98  			args.addAll(context.getPhases());
99  		}
100 		if (!CollectionUtils.isEmpty(context.getPassThroughPropertyKeys())) {
101 			Properties p = getPassThroughProperties(context);
102 			List<String> keys = PropertyUtils.getSortedKeys(p);
103 			for (String key : keys) {
104 				String value = p.getProperty(key);
105 				String arg = "-D" + key + "=" + value;
106 				args.add(arg);
107 			}
108 		}
109 		return args;
110 	}
111 
112 	protected Properties getPassThroughProperties(MavenContext context) {
113 		List<String> keys = context.getPassThroughPropertyKeys();
114 		Properties properties = new Properties();
115 		Collections.sort(keys);
116 		Properties internal = context.getProperties();
117 		internal.putAll(PropertyUtils.getEnvAsProperties());
118 		internal.putAll(System.getProperties());
119 		for (String key : keys) {
120 			String value = internal.getProperty(key);
121 			if (!StringUtils.isBlank(value)) {
122 				properties.setProperty(key, value);
123 			}
124 		}
125 		return properties;
126 	}
127 
128 }