IDEADEV-3663 Allow Developer to pass JVM args while Running Ant build
[fedora-idea.git] / plugins / ant / src / com / intellij / lang / ant / config / execution / AntCommandLineBuilder.java
blob56c7d5ba7e40ec6ca250ce6da3d973b611e506d8
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.intellij.lang.ant.config.execution;
18 import com.intellij.execution.CantRunException;
19 import com.intellij.execution.configurations.JavaParameters;
20 import com.intellij.execution.configurations.ParametersList;
21 import com.intellij.ide.macro.Macro;
22 import com.intellij.ide.macro.MacroManager;
23 import com.intellij.lang.ant.AntBundle;
24 import com.intellij.lang.ant.config.impl.*;
25 import com.intellij.openapi.actionSystem.DataContext;
26 import com.intellij.openapi.projectRoots.JavaSdkType;
27 import com.intellij.openapi.projectRoots.Sdk;
28 import com.intellij.openapi.projectRoots.SdkType;
29 import com.intellij.openapi.roots.OrderRootType;
30 import com.intellij.openapi.vfs.VirtualFile;
31 import com.intellij.rt.ant.execution.AntMain2;
32 import com.intellij.rt.ant.execution.IdeaAntLogger2;
33 import com.intellij.rt.ant.execution.IdeaInputHandler;
34 import com.intellij.util.ArrayUtil;
35 import com.intellij.util.PathUtil;
36 import com.intellij.util.config.AbstractProperty;
37 import org.jetbrains.annotations.NonNls;
39 import java.io.File;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.List;
44 public class AntCommandLineBuilder {
45 private final List<String> myTargets = new ArrayList<String>();
46 private final JavaParameters myCommandLine = new JavaParameters();
47 private String myBuildFilePath;
48 private List<BuildFileProperty> myProperties;
49 private boolean myDone = false;
50 @NonNls private final List<String> myExpandedProperties = new ArrayList<String>();
51 @NonNls private static final String INPUT_HANDLER_PARAMETER = "-inputhandler";
52 @NonNls private static final String LOGFILE_PARAMETER = "-logfile";
53 @NonNls private static final String LOGFILE_SHORT_PARAMETER = "-l";
55 public void calculateProperties(final DataContext dataContext) throws Macro.ExecutionCancelledException {
56 for (BuildFileProperty property : myProperties) {
57 String value = property.getPropertyValue();
58 final MacroManager macroManager = GlobalAntConfiguration.getMacroManager();
59 value = macroManager.expandMacrosInString(value, true, dataContext);
60 value = macroManager.expandMacrosInString(value, false, dataContext);
61 myExpandedProperties.add("-D" + property.getPropertyName() + "=" + value);
65 public void addTarget(String targetName) {
66 myTargets.add(targetName);
69 public void setBuildFile(AbstractProperty.AbstractPropertyContainer container, File buildFile) throws CantRunException {
70 String jdkName = AntBuildFileImpl.CUSTOM_JDK_NAME.get(container);
71 Sdk jdk;
72 if (jdkName != null && jdkName.length() > 0) {
73 jdk = GlobalAntConfiguration.findJdk(jdkName);
75 else {
76 jdkName = AntConfigurationImpl.DEFAULT_JDK_NAME.get(container);
77 if (jdkName == null || jdkName.length() == 0) {
78 throw new CantRunException(AntBundle.message("project.jdk.not.specified.error.message"));
80 jdk = GlobalAntConfiguration.findJdk(jdkName);
82 if (jdk == null) {
83 throw new CantRunException(AntBundle.message("jdk.with.name.not.configured.error.message", jdkName));
85 VirtualFile homeDirectory = jdk.getHomeDirectory();
86 if (homeDirectory == null) {
87 throw new CantRunException(AntBundle.message("jdk.with.name.bad.configured.error.message", jdkName));
89 myCommandLine.setJdk(jdk);
91 final ParametersList vmParametersList = myCommandLine.getVMParametersList();
92 vmParametersList.add("-Xmx" + AntBuildFileImpl.MAX_HEAP_SIZE.get(container) + "m");
93 vmParametersList.add("-Xss" + AntBuildFileImpl.MAX_STACK_SIZE.get(container) + "m");
95 final AntInstallation antInstallation = AntBuildFileImpl.ANT_INSTALLATION.get(container);
96 if (antInstallation == null) {
97 throw new CantRunException(AntBundle.message("ant.installation.not.configured.error.message"));
100 final String antHome = AntInstallation.HOME_DIR.get(antInstallation.getProperties());
101 vmParametersList.add("-Dant.home=" + antHome);
103 String[] urls = jdk.getRootProvider().getUrls(OrderRootType.CLASSES);
104 final String jdkHome = homeDirectory.getPath().replace('/', File.separatorChar);
105 @NonNls final String pathToJre = jdkHome + File.separator + "jre" + File.separator;
106 for (String url : urls) {
107 final String path = PathUtil.toPresentableUrl(url);
108 if (!path.startsWith(pathToJre)) {
109 myCommandLine.getClassPath().add(path);
113 myCommandLine.getClassPath().addAllFiles(AntBuildFileImpl.ALL_CLASS_PATH.get(container));
115 myCommandLine.getClassPath().addAllFiles(AntBuildFileImpl.getUserHomeLibraries());
117 final SdkType sdkType = jdk.getSdkType();
118 if (sdkType instanceof JavaSdkType) {
119 final String toolsJar = ((JavaSdkType)sdkType).getToolsPath(jdk);
120 if (toolsJar != null) {
121 myCommandLine.getClassPath().add(toolsJar);
124 PathUtilEx.addRtJar(myCommandLine.getClassPath());
126 myCommandLine.setMainClass(AntMain2.class.getName());
127 final ParametersList programParameters = myCommandLine.getProgramParametersList();
129 final String additionalParams = AntBuildFileImpl.ANT_COMMAND_LINE_PARAMETERS.get(container);
130 if (additionalParams != null) {
131 for (String param : ParametersList.parse(additionalParams)) {
132 if (param.startsWith("-J")) {
133 final String cutParam = param.substring("-J".length());
134 if (cutParam.length() > 0) {
135 vmParametersList.add(cutParam);
138 else {
139 programParameters.add(param);
144 if (!(programParameters.getList().contains(LOGFILE_SHORT_PARAMETER) || programParameters.getList().contains(LOGFILE_PARAMETER)) ) {
145 programParameters.add("-logger", IdeaAntLogger2.class.getName());
147 if (!programParameters.getList().contains(INPUT_HANDLER_PARAMETER)) {
148 programParameters.add(INPUT_HANDLER_PARAMETER, IdeaInputHandler.class.getName());
151 myProperties = AntBuildFileImpl.ANT_PROPERTIES.get(container);
153 myBuildFilePath = buildFile.getAbsolutePath();
154 myCommandLine.setWorkingDirectory(buildFile.getParent());
157 public JavaParameters getCommandLine() {
158 if (myDone) return myCommandLine;
159 ParametersList programParameters = myCommandLine.getProgramParametersList();
160 for (final String property : myExpandedProperties) {
161 if (property != null) {
162 programParameters.add(property);
165 programParameters.add("-buildfile", myBuildFilePath);
166 for (final String target : myTargets) {
167 if (target != null) {
168 programParameters.add(target);
171 myDone = true;
172 return myCommandLine;
175 public void addTargets(String[] targets) {
176 myTargets.addAll(Arrays.asList(targets));
179 public String[] getTargets() {
180 return ArrayUtil.toStringArray(myTargets);