Skip to content

Commit 109b2cd

Browse files
authored
Merge pull request #8897 from Achal1607/add-source-set-gradle
Support running classes from specific Gradle source sets in Gradle actions
2 parents f0c387f + 7d33f32 commit 109b2cd

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
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_NAMES = "runSourceSetNames";
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_NAMES);
73+
if (sourceSetValue != null) {
74+
SourceSetContainer sourceSets = project.getExtensions().findByType(SourceSetContainer.class);
75+
if (sourceSets != null) {
76+
FileCollection updatedClasspath = 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(updatedClasspath);
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_NAMES = "sourceSetNames"; //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_NAMES,
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_NAMES, ss.getName(), (oldVal, newVal) -> oldVal.trim() + "," + newVal.trim());
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} -PrunSourceSetNames=${sourceSetNames} ${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} -PrunSourceSetNames=${sourceSetNames} ${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} -PrunSourceSetNames=${sourceSetNames} ${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: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Collection;
2929
import java.util.Collections;
3030
import java.util.List;
31+
import java.util.Locale;
3132
import java.util.Map;
3233
import java.util.Objects;
3334
import java.util.Optional;
@@ -60,6 +61,8 @@
6061
import org.netbeans.api.extexecution.ExecutionService;
6162
import org.netbeans.api.java.classpath.ClassPath;
6263
import org.netbeans.api.java.queries.UnitTestForSourceQuery;
64+
import org.netbeans.api.java.source.ClasspathInfo;
65+
import org.netbeans.api.java.source.SourceUtils;
6366
import org.netbeans.api.project.FileOwnerQuery;
6467
import org.netbeans.api.project.Project;
6568
import org.netbeans.api.project.ProjectUtils;
@@ -84,6 +87,7 @@
8487
import org.netbeans.spi.project.SingleMethod;
8588

8689
import org.openide.filesystems.FileObject;
90+
import org.openide.filesystems.FileUtil;
8791
import org.openide.util.BaseUtilities;
8892
import org.openide.util.Lookup;
8993
import org.openide.util.NbBundle;
@@ -114,6 +118,7 @@ public abstract class NbLaunchDelegate {
114118

115119
private final RequestProcessor requestProcessor = new RequestProcessor(NbLaunchDelegate.class);
116120
private final Map<DebugAdapterContext, DebuggerManagerListener> debuggerListeners = new ConcurrentHashMap<>();
121+
private final static String JAVA_FILE_EXT = ".java";
117122

118123
public abstract void preLaunch(Map<String, Object> launchArguments, DebugAdapterContext context);
119124

@@ -570,8 +575,20 @@ public void finished(boolean success) {
570575
} else if (launchType == LaunchType.RUN_TEST) {
571576
mainSource = false;
572577
} else {
573-
FileObject fileRoot = sourceCP != null ? sourceCP.findOwnerRoot(toRun) : null;
574-
mainSource = fileRoot == null || UnitTestForSourceQuery.findSources(fileRoot).length == 0;
578+
mainSource = true;
579+
if (sourceCP != null) {
580+
FileObject root = sourceCP.findOwnerRoot(toRun);
581+
if (root != null) {
582+
if (UnitTestForSourceQuery.findSources(root).length > 0) {
583+
String relativePath = FileUtil.getRelativePath(root, toRun);
584+
if (relativePath != null && relativePath.toLowerCase(Locale.ENGLISH).endsWith(JAVA_FILE_EXT)) {
585+
String className = relativePath.substring(0, relativePath.length() - JAVA_FILE_EXT.length()).replace('/', '.');
586+
ClasspathInfo cpi = ClasspathInfo.create(toRun);
587+
mainSource = SourceUtils.isMainClass(className, cpi, true);
588+
}
589+
}
590+
}
591+
}
575592
}
576593
ActionProvider provider = null;
577594
String command = null;

0 commit comments

Comments
 (0)