Search in sources :

Example 11 with JavaParameters

use of com.intellij.execution.configurations.JavaParameters in project android by JetBrains.

the class AndroidJunitPatcherWithTestArtifactTest method testRemoveAndroidTestClasspath.

public void testRemoveAndroidTestClasspath() throws Exception {
    loadProject(SYNC_MULTIPROJECT);
    JUnitPatcher myPatcher = new AndroidJunitPatcher();
    Module module1 = ModuleManager.getInstance(myFixture.getProject()).findModuleByName("module1");
    JavaParameters parameters = new JavaParameters();
    parameters.configureByModule(module1, JavaParameters.CLASSES_AND_TESTS);
    String classpath = parameters.getClassPath().getPathsString();
    assertThat(classpath).contains("junit-4.12.jar");
    assertThat(classpath).contains("gson-2.2.4.jar");
    // JUnit is in test dependency, gson and guava are android test dependency
    myPatcher.patchJavaParameters(module1, parameters);
    classpath = parameters.getClassPath().getPathsString();
    assertThat(classpath).contains("junit-4.12.jar");
    assertThat(classpath).doesNotContain("gson-2.2.4.jar");
    assertThat(classpath).doesNotContain("guava-18.0.jar");
    assertThat(classpath).doesNotContain("module3");
}
Also used : JavaParameters(com.intellij.execution.configurations.JavaParameters) JUnitPatcher(com.intellij.execution.JUnitPatcher) Module(com.intellij.openapi.module.Module)

Example 12 with JavaParameters

use of com.intellij.execution.configurations.JavaParameters in project android by JetBrains.

the class AndroidMavenExecutor method generateResources.

public static Map<CompilerMessageCategory, List<String>> generateResources(final Module module) {
    MavenProjectsManager projectsManager = MavenProjectsManager.getInstance(module.getProject());
    final MavenRunnerParameters parameters = new MavenRunnerParameters(true, projectsManager.findProject(module).getDirectory(), Collections.singletonList("process-resources"), projectsManager.getExplicitProfiles());
    final Map<CompilerMessageCategory, List<String>> result = new HashMap<CompilerMessageCategory, List<String>>();
    result.put(CompilerMessageCategory.ERROR, new ArrayList<String>());
    try {
        JavaParameters javaParams = ApplicationManager.getApplication().runReadAction(new Computable<JavaParameters>() {

            @Nullable
            @Override
            public JavaParameters compute() {
                try {
                    return MavenExternalParameters.createJavaParameters(module.getProject(), parameters);
                } catch (ExecutionException e) {
                    LOG.info(e);
                    result.get(CompilerMessageCategory.ERROR).add(e.getMessage());
                    return null;
                }
            }
        });
        if (javaParams == null) {
            return result;
        }
        GeneralCommandLine commandLine = javaParams.toCommandLine();
        final StringBuildingOutputProcessor processor = new StringBuildingOutputProcessor();
        boolean success = AndroidUtils.executeCommand(commandLine, processor, WaitingStrategies.WaitForever.getInstance()) == ExecutionStatus.SUCCESS;
        String message = processor.getMessage();
        if (!success) {
            LOG.info(message);
            String lcmessage = message.toLowerCase();
            int buildErrorIndex = lcmessage.indexOf(BUILD_ERROR_INDICATOR);
            if (buildErrorIndex >= 0) {
                result.get(CompilerMessageCategory.ERROR).add(message.substring(buildErrorIndex));
            }
        }
    } catch (ExecutionException e) {
        LOG.info(e);
        result.get(CompilerMessageCategory.ERROR).add(e.getMessage());
    }
    return result;
}
Also used : CompilerMessageCategory(com.intellij.openapi.compiler.CompilerMessageCategory) MavenProjectsManager(org.jetbrains.idea.maven.project.MavenProjectsManager) HashMap(com.intellij.util.containers.HashMap) StringBuildingOutputProcessor(org.jetbrains.android.util.StringBuildingOutputProcessor) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) JavaParameters(com.intellij.execution.configurations.JavaParameters) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(com.intellij.execution.ExecutionException) Nullable(org.jetbrains.annotations.Nullable) MavenRunnerParameters(org.jetbrains.idea.maven.execution.MavenRunnerParameters)

Example 13 with JavaParameters

use of com.intellij.execution.configurations.JavaParameters in project intellij-community by JetBrains.

the class GradleScriptType method getRunner.

@Override
public GroovyScriptRunner getRunner() {
    return new GroovyScriptRunner() {

        @Override
        public boolean shouldRefreshAfterFinish() {
            return true;
        }

        @Override
        public boolean isValidModule(@NotNull Module module) {
            GradleInstallationManager libraryManager = ServiceManager.getService(GradleInstallationManager.class);
            return libraryManager.isGradleSdk(OrderEnumerator.orderEntries(module).getAllLibrariesAndSdkClassesRoots());
        }

        @Override
        public void ensureRunnerConfigured(@NotNull GroovyScriptRunConfiguration configuration) {
            String parameters = configuration.getProgramParameters();
            if (parameters != null) {
            // TODO den implement
            //            GradleTasksList list = GradleUtil.getToolWindowElement(GradleTasksList.class, project, ExternalSystemDataKeys.RECENT_TASKS_LIST);
            //            if (list != null) {
            //              ExternalSystemTaskDescriptor descriptor = new ExternalSystemTaskDescriptor(parameters, null);
            //              descriptor.setExecutorId(executor.getId());
            //              list.setFirst(descriptor);
            //              GradleLocalSettings.getInstance(project).setRecentTasks(list.getModel().getTasks());
            //            }
            }
            final GradleInstallationManager libraryManager = ServiceManager.getService(GradleInstallationManager.class);
        // TODO den implement
        //if (libraryManager.getGradleHome(module, project) == null) {
        //  int result = 0;
        //          int result = Messages.showOkCancelDialog(
        //            ExternalSystemBundle.message("gradle.run.no.sdk.text"),
        //            ExternalSystemBundle.message("gradle.run.no.sdk.title"),
        //            GradleIcons.Gradle
        //          );
        //          if (result == 0) {
        //            ShowSettingsUtil.getInstance().editConfigurable(project, new AbstractExternalProjectConfigurable(project));
        //          }
        //          if (libraryManager.getGradleHome(module, project) == null) {
        //            return false;
        //          }
        //        }
        }

        @Override
        public void configureCommandLine(JavaParameters params, @Nullable Module module, boolean tests, VirtualFile script, GroovyScriptRunConfiguration configuration) throws CantRunException {
            final Project project = configuration.getProject();
            final GradleInstallationManager libraryManager = ServiceManager.getService(GradleInstallationManager.class);
            if (module == null) {
                throw new CantRunException("Target module is undefined");
            }
            String rootProjectPath = module.getOptionValue(ExternalSystemConstants.ROOT_PROJECT_PATH_KEY);
            if (StringUtil.isEmpty(rootProjectPath)) {
                throw new CantRunException(String.format("Module '%s' is not backed by gradle", module.getName()));
            }
            final VirtualFile gradleHome = libraryManager.getGradleHome(module, project, rootProjectPath);
            if (gradleHome == null) {
                throw new CantRunException("Gradle home can not be found");
            }
            params.setMainClass(findMainClass(gradleHome, script, project));
            final File[] groovyJars = GroovyConfigUtils.getGroovyAllJars(gradleHome.getPath() + "/lib/");
            if (groovyJars.length > 0) {
                params.getClassPath().add(groovyJars[0].getAbsolutePath());
            } else {
                final VirtualFile groovyJar = findGroovyJar(module);
                if (groovyJar != null) {
                    params.getClassPath().add(groovyJar);
                }
            }
            final String userDefinedClasspath = System.getProperty("gradle.launcher.classpath");
            if (StringUtil.isNotEmpty(userDefinedClasspath)) {
                params.getClassPath().add(userDefinedClasspath);
            } else {
                final Collection<VirtualFile> roots = libraryManager.getClassRoots(project);
                if (roots != null) {
                    params.getClassPath().addVirtualFiles(roots);
                }
            }
            params.getVMParametersList().addParametersString(configuration.getVMParameters());
            params.getVMParametersList().add("-Dgradle.home=" + FileUtil.toSystemDependentName(gradleHome.getPath()));
            setToolsJar(params);
            final String scriptPath = configuration.getScriptPath();
            if (scriptPath == null) {
                throw new CantRunException("Target script or gradle project path is undefined");
            }
            if (new File(scriptPath).isFile()) {
                params.getProgramParametersList().add("--build-file");
            } else {
                params.getProgramParametersList().add("--project-dir");
            }
            params.getProgramParametersList().add(FileUtil.toSystemDependentName(scriptPath));
            params.getProgramParametersList().addParametersString(configuration.getProgramParameters());
        }
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GradleInstallationManager(org.jetbrains.plugins.gradle.service.GradleInstallationManager) NotNull(org.jetbrains.annotations.NotNull) GroovyScriptRunConfiguration(org.jetbrains.plugins.groovy.runner.GroovyScriptRunConfiguration) Project(com.intellij.openapi.project.Project) CantRunException(com.intellij.execution.CantRunException) GroovyScriptRunner(org.jetbrains.plugins.groovy.runner.GroovyScriptRunner) JavaParameters(com.intellij.execution.configurations.JavaParameters) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with JavaParameters

use of com.intellij.execution.configurations.JavaParameters in project intellij-plugins by JetBrains.

the class BndLaunchUtil method createJavaParameters.

@NotNull
public static JavaParameters createJavaParameters(@NotNull BndRunConfigurationBase configuration, @NotNull ProjectLauncher launcher) throws CantRunException {
    Project project = configuration.getProject();
    JavaParameters parameters = new JavaParameters();
    File launcherDir = launcher.getCwd();
    parameters.setWorkingDirectory(launcherDir != null ? launcherDir.getPath() : project.getBasePath());
    String jreHome = configuration.useAlternativeJre ? configuration.alternativeJrePath : null;
    JavaParametersUtil.configureProject(project, parameters, JavaParameters.JDK_ONLY, jreHome);
    parameters.getEnv().putAll(launcher.getRunEnv());
    parameters.getVMParametersList().addAll(asList(launcher.getRunVM()));
    parameters.getClassPath().addAll(asList(launcher.getClasspath()));
    parameters.setMainClass(launcher.getMainTypeName());
    parameters.getProgramParametersList().addAll(asList(launcher.getRunProgramArgs()));
    return parameters;
}
Also used : Project(com.intellij.openapi.project.Project) JavaParameters(com.intellij.execution.configurations.JavaParameters) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with JavaParameters

use of com.intellij.execution.configurations.JavaParameters in project intellij-community by JetBrains.

the class GradleApplicationEnvironmentProvider method createExecutionEnvironment.

@Nullable
public ExecutionEnvironment createExecutionEnvironment(@NotNull Project project, @NotNull ExecuteRunConfigurationTask executeRunConfigurationTask, @Nullable Executor executor) {
    if (!isApplicable(executeRunConfigurationTask))
        return null;
    ApplicationConfiguration applicationConfiguration = (ApplicationConfiguration) executeRunConfigurationTask.getRunProfile();
    PsiClass mainClass = applicationConfiguration.getMainClass();
    if (mainClass == null)
        return null;
    VirtualFile virtualFile = mainClass.getContainingFile().getVirtualFile();
    Module module = ProjectFileIndex.SERVICE.getInstance(project).getModuleForFile(virtualFile);
    if (module == null)
        return null;
    final JavaParameters params = new JavaParameters();
    JavaParametersUtil.configureConfiguration(params, applicationConfiguration);
    params.getVMParametersList().addParametersString(applicationConfiguration.getVMParameters());
    String javaExePath = null;
    try {
        final Sdk jdk = JavaParametersUtil.createProjectJdk(project, applicationConfiguration.getAlternativeJrePath());
        if (jdk == null)
            throw new RuntimeException(ExecutionBundle.message("run.configuration.error.no.jdk.specified"));
        final SdkTypeId type = jdk.getSdkType();
        if (!(type instanceof JavaSdkType))
            throw new RuntimeException(ExecutionBundle.message("run.configuration.error.no.jdk.specified"));
        javaExePath = ((JavaSdkType) type).getVMExecutablePath(jdk);
        if (javaExePath == null)
            throw new RuntimeException(ExecutionBundle.message("run.configuration.cannot.find.vm.executable"));
        javaExePath = FileUtil.toSystemIndependentName(javaExePath);
    } catch (CantRunException e) {
        ExecutionErrorDialog.show(e, "Cannot use specified JRE", project);
    }
    StringBuilder parametersString = new StringBuilder();
    for (String parameter : params.getProgramParametersList().getParameters()) {
        parametersString.append("args '").append(parameter).append("'\n");
    }
    StringBuilder vmParametersString = new StringBuilder();
    for (String parameter : params.getVMParametersList().getParameters()) {
        vmParametersString.append("jvmArgs '").append(parameter).append("'\n");
    }
    ExternalSystemTaskExecutionSettings taskSettings = new ExternalSystemTaskExecutionSettings();
    taskSettings.setExternalSystemIdString(GradleConstants.SYSTEM_ID.getId());
    taskSettings.setExternalProjectPath(ExternalSystemApiUtil.getExternalProjectPath(module));
    final String runAppTaskName = "run " + mainClass.getName();
    taskSettings.setTaskNames(Collections.singletonList(runAppTaskName));
    String executorId = executor == null ? DefaultRunExecutor.EXECUTOR_ID : executor.getId();
    ExecutionEnvironment environment = ExternalSystemUtil.createExecutionEnvironment(project, GradleConstants.SYSTEM_ID, taskSettings, executorId);
    if (environment != null) {
        RunnerAndConfigurationSettings runnerAndConfigurationSettings = environment.getRunnerAndConfigurationSettings();
        assert runnerAndConfigurationSettings != null;
        ExternalSystemRunConfiguration runConfiguration = (ExternalSystemRunConfiguration) runnerAndConfigurationSettings.getConfiguration();
        final String gradlePath = GradleProjectResolverUtil.getGradlePath(module);
        if (gradlePath == null)
            return null;
        final String sourceSetName;
        if (GradleConstants.GRADLE_SOURCE_SET_MODULE_TYPE_KEY.equals(ExternalSystemApiUtil.getExternalModuleType(module))) {
            sourceSetName = GradleProjectResolverUtil.getSourceSetName(module);
        } else {
            sourceSetName = ModuleRootManager.getInstance(module).getFileIndex().isInTestSourceContent(virtualFile) ? "test" : "main";
        }
        if (sourceSetName == null)
            return null;
        @Language("Groovy") String initScript = "projectsEvaluated {\n" + "  rootProject.allprojects {\n" + "    if(project.path == '" + gradlePath + "' && project.sourceSets) {\n" + "      project.tasks.create(name: '" + runAppTaskName + "', overwrite: true, type: JavaExec) {\n" + (javaExePath != null ? "        executable = '" + javaExePath + "'\n" : "") + "        classpath = project.sourceSets.'" + sourceSetName + "'.runtimeClasspath\n" + "        main = '" + mainClass.getQualifiedName() + "'\n" + parametersString.toString() + vmParametersString.toString() + "      }\n" + "    }\n" + "  }\n" + "}\n";
        runConfiguration.putUserData(GradleTaskManager.INIT_SCRIPT_KEY, initScript);
        return environment;
    } else {
        return null;
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) PsiClass(com.intellij.psi.PsiClass) ExternalSystemRunConfiguration(com.intellij.openapi.externalSystem.service.execution.ExternalSystemRunConfiguration) JavaSdkType(com.intellij.openapi.projectRoots.JavaSdkType) CantRunException(com.intellij.execution.CantRunException) Language(org.intellij.lang.annotations.Language) JavaParameters(com.intellij.execution.configurations.JavaParameters) RunnerAndConfigurationSettings(com.intellij.execution.RunnerAndConfigurationSettings) Sdk(com.intellij.openapi.projectRoots.Sdk) SdkTypeId(com.intellij.openapi.projectRoots.SdkTypeId) Module(com.intellij.openapi.module.Module) ApplicationConfiguration(com.intellij.execution.application.ApplicationConfiguration) ExternalSystemTaskExecutionSettings(com.intellij.openapi.externalSystem.model.execution.ExternalSystemTaskExecutionSettings) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

JavaParameters (com.intellij.execution.configurations.JavaParameters)30 Module (com.intellij.openapi.module.Module)9 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 ExecutionException (com.intellij.execution.ExecutionException)7 Project (com.intellij.openapi.project.Project)6 File (java.io.File)5 Nullable (org.jetbrains.annotations.Nullable)5 CantRunException (com.intellij.execution.CantRunException)4 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)4 NotNull (org.jetbrains.annotations.NotNull)4 ParametersList (com.intellij.execution.configurations.ParametersList)3 ExecutionEnvironment (com.intellij.execution.runners.ExecutionEnvironment)3 SourceScope (com.intellij.execution.testframework.SourceScope)3 Sdk (com.intellij.openapi.projectRoots.Sdk)3 PathsList (com.intellij.util.PathsList)3 IOException (java.io.IOException)3 OSProcessHandler (com.intellij.execution.process.OSProcessHandler)2 JUnit5IdeaTestRunner (com.intellij.junit5.JUnit5IdeaTestRunner)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 NonNls (org.jetbrains.annotations.NonNls)2