001    package org.kuali.common.util.service;
002    
003    import java.io.File;
004    import java.util.ArrayList;
005    import java.util.Collections;
006    import java.util.List;
007    import java.util.Properties;
008    
009    import org.apache.commons.lang3.StringUtils;
010    import org.kuali.common.util.CollectionUtils;
011    import org.kuali.common.util.LocationUtils;
012    import org.kuali.common.util.PropertyUtils;
013    
014    public class DefaultMavenService extends DefaultExecService implements MavenService {
015    
016            @Override
017            public void execute(MavenContext context) {
018    
019                    // Update options with MavenContext attributes
020                    handleOptions(context);
021    
022                    // Convert options/goals/phases into an args list
023                    List<String> args = getArgs(context);
024    
025                    // Create an execution context
026                    DefaultExecContext dec = new DefaultExecContext();
027                    dec.setExecutable(context.getExecutable());
028                    dec.setWorkingDirectory(context.getWorkingDir());
029                    dec.setArgs(args);
030    
031                    // TODO Re-factor things so only MAVEN_OPTS gets inherited instead of everything
032                    if (context.isInheritMavenOpts()) {
033                            dec.setAddSystemEnvironment(true);
034                    }
035    
036                    // Execute Maven making sure we get 0 as a return value
037                    executeAndValidate(dec);
038            }
039    
040            @Override
041            public void execute(File workingDir, List<String> options, List<String> goals, List<String> phases) {
042                    MavenContext context = new MavenContext();
043                    context.setWorkingDir(workingDir);
044                    context.setOptions(options);
045                    context.setGoals(goals);
046                    context.setPhases(phases);
047                    execute(context);
048            }
049    
050            protected List<String> getOptions(MavenContext context) {
051                    List<String> options = new ArrayList<String>();
052                    if (context.isBatchMode()) {
053                            options.add("--batch-mode");
054                    }
055                    if (context.isDebug()) {
056                            options.add("--debug");
057                    }
058                    if (context.isErrors()) {
059                            options.add("--errors");
060                    }
061                    if (context.isOffline()) {
062                            options.add("--offline");
063                    }
064                    if (context.isQuiet()) {
065                            options.add("--quiet");
066                    }
067                    if (context.getPom() != null) {
068                            String cpath = LocationUtils.getCanonicalPath(context.getPom());
069                            options.add("--file");
070                            options.add(cpath);
071                    }
072                    if (!CollectionUtils.isEmpty(context.getProfiles())) {
073                            String csv = CollectionUtils.getCSV(context.getProfiles());
074                            options.add("--activate-profiles");
075                            options.add(csv);
076                    }
077                    return options;
078            }
079    
080            protected void handleOptions(MavenContext context) {
081                    List<String> options = getOptions(context);
082                    if (context.getOptions() == null) {
083                            context.setOptions(options);
084                    } else {
085                            context.getOptions().addAll(options);
086                    }
087            }
088    
089            protected List<String> getArgs(MavenContext context) {
090                    List<String> args = new ArrayList<String>();
091                    if (!CollectionUtils.isEmpty(context.getOptions())) {
092                            args.addAll(context.getOptions());
093                    }
094                    if (!CollectionUtils.isEmpty(context.getGoals())) {
095                            args.addAll(context.getGoals());
096                    }
097                    if (!CollectionUtils.isEmpty(context.getPhases())) {
098                            args.addAll(context.getPhases());
099                    }
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    }