001 /**
002 * Copyright 2010-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.common.util.service;
017
018 import java.io.File;
019 import java.util.ArrayList;
020 import java.util.Collections;
021 import java.util.List;
022 import java.util.Properties;
023
024 import org.apache.commons.lang3.StringUtils;
025 import org.kuali.common.util.CollectionUtils;
026 import org.kuali.common.util.LocationUtils;
027 import org.kuali.common.util.PropertyUtils;
028
029 public class DefaultMavenService extends DefaultExecService implements MavenService {
030
031 @Override
032 public void execute(MavenContext context) {
033
034 // Update options with MavenContext attributes
035 handleOptions(context);
036
037 // Convert options/goals/phases into an args list
038 List<String> args = getArgs(context);
039
040 // Create an execution context
041 DefaultExecContext dec = new DefaultExecContext();
042 dec.setExecutable(context.getExecutable());
043 dec.setWorkingDirectory(context.getWorkingDir());
044 dec.setArgs(args);
045
046 // TODO Re-factor things so only MAVEN_OPTS gets inherited instead of everything
047 if (context.isInheritMavenOpts()) {
048 dec.setAddSystemEnvironment(true);
049 }
050
051 // Execute Maven making sure we get 0 as a return value
052 executeAndValidate(dec);
053 }
054
055 @Override
056 public void execute(File workingDir, List<String> options, List<String> goals, List<String> phases) {
057 MavenContext context = new MavenContext();
058 context.setWorkingDir(workingDir);
059 context.setOptions(options);
060 context.setGoals(goals);
061 context.setPhases(phases);
062 execute(context);
063 }
064
065 protected List<String> getOptions(MavenContext context) {
066 List<String> options = new ArrayList<String>();
067 if (context.isBatchMode()) {
068 options.add("--batch-mode");
069 }
070 if (context.isDebug()) {
071 options.add("--debug");
072 }
073 if (context.isErrors()) {
074 options.add("--errors");
075 }
076 if (context.isOffline()) {
077 options.add("--offline");
078 }
079 if (context.isQuiet()) {
080 options.add("--quiet");
081 }
082 if (context.getPom() != null) {
083 String cpath = LocationUtils.getCanonicalPath(context.getPom());
084 options.add("--file");
085 options.add(cpath);
086 }
087 if (!CollectionUtils.isEmpty(context.getProfiles())) {
088 String csv = CollectionUtils.getCSV(context.getProfiles());
089 options.add("--activate-profiles");
090 options.add(csv);
091 }
092 return options;
093 }
094
095 protected void handleOptions(MavenContext context) {
096 List<String> options = getOptions(context);
097 if (context.getOptions() == null) {
098 context.setOptions(options);
099 } else {
100 context.getOptions().addAll(options);
101 }
102 }
103
104 protected List<String> getArgs(MavenContext context) {
105 List<String> args = new ArrayList<String>();
106 if (!CollectionUtils.isEmpty(context.getOptions())) {
107 args.addAll(context.getOptions());
108 }
109 if (!CollectionUtils.isEmpty(context.getGoals())) {
110 args.addAll(context.getGoals());
111 }
112 if (!CollectionUtils.isEmpty(context.getPhases())) {
113 args.addAll(context.getPhases());
114 }
115 if (!CollectionUtils.isEmpty(context.getPassThroughPropertyKeys())) {
116 Properties p = getPassThroughProperties(context);
117 List<String> keys = PropertyUtils.getSortedKeys(p);
118 for (String key : keys) {
119 String value = p.getProperty(key);
120 String arg = "-D" + key + "=" + value;
121 args.add(arg);
122 }
123 }
124 return args;
125 }
126
127 protected Properties getPassThroughProperties(MavenContext context) {
128 List<String> keys = context.getPassThroughPropertyKeys();
129 Properties properties = new Properties();
130 Collections.sort(keys);
131 Properties global = PropertyUtils.getGlobalProperties(context.getProperties());
132 for (String key : keys) {
133 String value = global.getProperty(key);
134 if (!StringUtils.isBlank(value)) {
135 properties.setProperty(key, value);
136 }
137 }
138 return properties;
139 }
140
141 }