Skip to content

Commit 83c0fbb

Browse files
committed
when mainClass is inside any source set other then main then run or debug action didn't work, fixed that
1 parent 5630f28 commit 83c0fbb

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

extide/gradle/netbeans-gradle-tooling/src/main/java/org/netbeans/modules/gradle/tooling/NetBeansRunSinglePlugin.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
package org.netbeans.modules.gradle.tooling;
2121

22+
import java.util.Arrays;
2223
import static java.util.Arrays.asList;
2324
import java.util.List;
25+
import java.util.Objects;
2426
import java.util.Set;
2527
import org.gradle.api.DefaultTask;
2628
import org.gradle.api.logging.Logger;
@@ -29,7 +31,9 @@
2931
import org.gradle.api.Plugin;
3032
import org.gradle.api.Project;
3133
import org.gradle.api.Task;
34+
import org.gradle.api.file.FileCollection;
3235
import org.gradle.api.tasks.JavaExec;
36+
import org.gradle.api.tasks.SourceSet;
3337
import org.gradle.api.tasks.SourceSetContainer;
3438
import org.gradle.api.tasks.TaskProvider;
3539
import org.gradle.process.CommandLineArgumentProvider;
@@ -47,6 +51,7 @@ class NetBeansRunSinglePlugin implements Plugin<Project> {
4751
private static final String RUN_SINGLE_ARGS = "runArgs";
4852
private static final String RUN_SINGLE_JVM_ARGS = "runJvmArgs";
4953
private static final String RUN_SINGLE_CWD = "runWorkingDir";
54+
private static final String RUN_SINGLE_SOURCE_SET_NAME = "runSourceSetName";
5055

5156
private static final String DEPRECATE_RUN_SINGLE =
5257
"runSingle task is deprecated. Inspect your configuration and use just 'run' task instead of 'runSingle'";
@@ -64,6 +69,20 @@ public void apply(Project project) {
6469
}
6570

6671
private void configureJavaExec(Project project, JavaExec je) {
72+
Object sourceSetValue = project.findProperty(RUN_SINGLE_SOURCE_SET_NAME);
73+
if (sourceSetValue != null) {
74+
SourceSetContainer sourceSets = project.getExtensions().findByType(SourceSetContainer.class);
75+
if (sourceSets != null) {
76+
FileCollection additionalClassPath = Arrays.stream(sourceSetValue.toString().split(","))
77+
.map(String::trim)
78+
.map(sourceSets::findByName)
79+
.filter(Objects::nonNull)
80+
.map(SourceSet::getRuntimeClasspath)
81+
.reduce(project.getObjects().fileCollection(), FileCollection::plus);
82+
83+
je.setClasspath(je.getClasspath().plus(additionalClassPath));
84+
}
85+
}
6786
if (project.hasProperty(RUN_SINGLE_MAIN)) {
6887
String mainClass = project.property(RUN_SINGLE_MAIN).toString();
6988
if (GRADLE_VERSION.compareTo(GradleVersion.version("6.4")) < 0) {

java/gradle.java/src/org/netbeans/modules/gradle/java/GradleJavaTokenProvider.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ public class GradleJavaTokenProvider implements ReplaceTokenProvider {
5353
private static final String AFFECTED_BUILD_TASK = "affectedBuildTasks";//NOI18N
5454
private static final String TEST_TASK_NAME = "testTaskName"; //NOI18N
5555
private static final String CLEAN_TEST_TASK_NAME = "cleanTestTaskName"; //NOI18N
56+
private static final String SOURCE_SET_NAME = "sourceSetName"; //NOI18N
5657

5758
private static final Set<String> SUPPORTED = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(
5859
SELECTED_CLASS,
5960
SELECTED_CLASS_NAME,
6061
SELECTED_METHOD,
6162
SELECTED_PACKAGE,
63+
SOURCE_SET_NAME,
6264
AFFECTED_BUILD_TASK
6365
)));
6466

@@ -123,6 +125,7 @@ private void processSourceSets(final Map<String, String> map, Lookup context) {
123125
GradleJavaSourceSet ss = gjp.containingSourceSet(f);
124126
if (ss != null) {
125127
Set<GradleJavaSourceSet.SourceType> types = ss.getSourceTypes(f);
128+
map.merge(SOURCE_SET_NAME, ss.getName(), (oldVal, newVal) -> oldVal + "," + newVal);
126129
for (GradleJavaSourceSet.SourceType type : types) {
127130
buildTasks.add(ss.getBuildTaskName(type));
128131
}

java/gradle.java/src/org/netbeans/modules/gradle/java/action-mapping.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@
5353
</action>
5454

5555
<action name="run.single">
56-
<args>-PrunClassName=${selectedClass} ${javaExec.workingDir} ${javaExec.environment} run --stacktrace ${javaExec.jvmArgs} ${javaExec.args}</args>
56+
<args>-PrunClassName=${selectedClass} -PrunSourceSetName=${sourceSetName} ${javaExec.workingDir} ${javaExec.environment} run --stacktrace ${javaExec.jvmArgs} ${javaExec.args}</args>
5757
</action>
5858

5959
<action name="debug.single">
60-
<args>-PrunClassName=${selectedClass} ${javaExec.workingDir} ${javaExec.environment} run --stacktrace --debug-jvm ${javaExec.jvmArgs} ${javaExec.args}</args>
60+
<args>-PrunClassName=${selectedClass} -PrunSourceSetName=${sourceSetName} ${javaExec.workingDir} ${javaExec.environment} run --stacktrace --debug-jvm ${javaExec.jvmArgs} ${javaExec.args}</args>
6161
</action>
6262
</apply-for>
6363

@@ -91,7 +91,7 @@
9191
<actions>
9292
<apply-for plugins="java">
9393
<action name="run.single">
94-
<args>-PrunClassName=${selectedClass} ${javaExec.workingDir} ${javaExec.environment} run --continuous ${javaExec.jvmArgs} ${javaExec.args}</args>
94+
<args>-PrunClassName=${selectedClass} -PrunSourceSetName=${sourceSetName} ${javaExec.workingDir} ${javaExec.environment} run --continuous ${javaExec.jvmArgs} ${javaExec.args}</args>
9595
</action>
9696
<action name="test.single">
9797
<args>"${cleanTestTaskName}" "${testTaskName}" --tests "${selectedClass}" --continuous</args>

java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/launch/NbLaunchDelegate.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@
5959
import org.netbeans.api.extexecution.ExecutionDescriptor;
6060
import org.netbeans.api.extexecution.ExecutionService;
6161
import org.netbeans.api.java.classpath.ClassPath;
62-
import org.netbeans.api.java.queries.UnitTestForSourceQuery;
62+
import org.netbeans.api.java.source.ClasspathInfo;
63+
import org.netbeans.api.java.source.SourceUtils;
6364
import org.netbeans.api.project.FileOwnerQuery;
6465
import org.netbeans.api.project.Project;
6566
import org.netbeans.api.project.ProjectUtils;
@@ -84,6 +85,7 @@
8485
import org.netbeans.spi.project.SingleMethod;
8586

8687
import org.openide.filesystems.FileObject;
88+
import org.openide.filesystems.FileUtil;
8789
import org.openide.util.BaseUtilities;
8890
import org.openide.util.Lookup;
8991
import org.openide.util.NbBundle;
@@ -570,8 +572,18 @@ public void finished(boolean success) {
570572
} else if (launchType == LaunchType.RUN_TEST) {
571573
mainSource = false;
572574
} else {
573-
FileObject fileRoot = sourceCP != null ? sourceCP.findOwnerRoot(toRun) : null;
574-
mainSource = fileRoot == null || UnitTestForSourceQuery.findSources(fileRoot).length == 0;
575+
mainSource = false;
576+
if (sourceCP != null) {
577+
FileObject root = sourceCP.findOwnerRoot(toRun);
578+
if (root != null) {
579+
String relativePath = FileUtil.getRelativePath(root, toRun);
580+
if (relativePath != null && relativePath.toLowerCase().endsWith(".java")) {
581+
String className = relativePath.substring(0, relativePath.length() - 5).replace('/', '.');
582+
ClasspathInfo cpi = ClasspathInfo.create(toRun);
583+
mainSource = SourceUtils.isMainClass(className, cpi, true);
584+
}
585+
}
586+
}
575587
}
576588
ActionProvider provider = null;
577589
String command = null;

0 commit comments

Comments
 (0)