Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit d269be8

Browse files
ttddyysdeleuze
authored andcommitted
Simplify "codeGenDebugPort" implementation in gradle plugin
Previously, the `codeGenDebugPort` implementation in gradle plugin was not effectively using the `JavaExec` infrastructure. This commit updates to use the debug options from `JavaExec` to configure the remote debugging. For code simplification, remove the `springAot` DSL support and only take `codeGenDebugPort` project property. This is because, in most cases, remote debug is specified in the command line. Also, adding documentation about `codeGenDebugPort` parameter.
1 parent 88e07e3 commit d269be8

File tree

5 files changed

+34
-52
lines changed

5 files changed

+34
-52
lines changed

spring-aot-gradle-plugin/src/main/java/org/springframework/aot/gradle/SpringAotGradlePlugin.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class SpringAotGradlePlugin implements Plugin<Project> {
6565

6666
public static final String GENERATE_TEST_TASK_NAME = "generateTestAot";
6767

68-
public static final String CODE_GEN_DEBUG_PORT_PROPERTY = "codeGenDebugPort";
68+
private static final String CODE_GEN_DEBUG_PORT_PROPERTY = "codeGenDebugPort";
6969

7070

7171
@Override
@@ -173,9 +173,27 @@ private GenerateAotSources createGenerateAotSourcesTask(Project project, SourceS
173173
generate.setResourceInputDirectories(mainSourceSet.getResources());
174174
generate.getSourcesOutputDirectory().set(aotSourcesDirectory);
175175
generate.getResourcesOutputDirectory().set(aotResourcesDirectory);
176+
177+
Integer debugPort = getDebugPort(project);
178+
if (debugPort != null) {
179+
generate.setDebug(true);
180+
generate.getDebugOptions().getPort().set(debugPort);
181+
}
176182
return generate;
177183
}
178184

185+
private Integer getDebugPort(Project project) {
186+
Object debugPortProperty = project.findProperty(CODE_GEN_DEBUG_PORT_PROPERTY);
187+
if (debugPortProperty != null) {
188+
try {
189+
return Integer.parseInt(debugPortProperty.toString());
190+
} catch (NumberFormatException exception) {
191+
// ignore
192+
}
193+
}
194+
return null;
195+
}
196+
179197
private void configureAotTasks(Project project, SourceSet aotSourceSet, GenerateAotSources generateAotSources) {
180198
project.getTasks().named(aotSourceSet.getCompileJavaTaskName(), JavaCompile.class, (aotCompileJava) -> {
181199
aotCompileJava.source(generateAotSources.getSourcesOutputDirectory());

spring-aot-gradle-plugin/src/main/java/org/springframework/aot/gradle/dsl/SpringAotExtension.java

-10
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ public class SpringAotExtension {
5151

5252
private final Property<String> mainClass;
5353

54-
private final Property<Integer> codeGenDebugPort;
55-
5654
public SpringAotExtension(ObjectFactory objectFactory) {
5755
this.mode = objectFactory.property(AotMode.class).convention(AotMode.NATIVE);
5856
this.debugVerify = objectFactory.property(Boolean.class).convention(false);
@@ -66,7 +64,6 @@ public SpringAotExtension(ObjectFactory objectFactory) {
6664
this.buildTimePropertiesMatchIfMissing = objectFactory.property(Boolean.class).convention(true);
6765
this.buildTimePropertiesChecks = objectFactory.property(String[].class).convention(new String[0]);
6866
this.mainClass = objectFactory.property(String.class).convention((String)null);
69-
this.codeGenDebugPort = objectFactory.property(Integer.class).convention((Integer) null);
7067
}
7168

7269
/**
@@ -139,13 +136,6 @@ public Property<String> getMainClass() {
139136
return this.mainClass;
140137
}
141138

142-
/**
143-
* Set remote debug port for code generation (not set by default).
144-
*/
145-
public Property<Integer> getCodeGenDebugPort() {
146-
return this.codeGenDebugPort;
147-
}
148-
149139
/**
150140
* Whether AOT should honor the {@code matchIfMissing} directive in configuration properties.
151141
* If disabled, the application needs to explicitly activate {@code *.enable} configuration properties

spring-aot-gradle-plugin/src/main/java/org/springframework/aot/gradle/tasks/GenerateAotOptions.java

-9
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ public class GenerateAotOptions implements Serializable {
5757

5858
private final Property<String[]> buildTimePropertiesChecks;
5959

60-
private final Property<Integer> codeGenDebugPort;
61-
6260
public GenerateAotOptions(SpringAotExtension extension) {
6361
this.mode = extension.getMode().map(aotMode -> aotMode.getSlug());
6462
this.debugVerify = extension.getDebugVerify();
@@ -72,7 +70,6 @@ public GenerateAotOptions(SpringAotExtension extension) {
7270
this.mainClass = extension.getMainClass();
7371
this.buildTimePropertiesMatchIfMissing = extension.getBuildTimePropertiesMatchIfMissing();
7472
this.buildTimePropertiesChecks = extension.getBuildTimePropertiesChecks();
75-
this.codeGenDebugPort = extension.getCodeGenDebugPort();
7673
}
7774

7875
@Input
@@ -126,12 +123,6 @@ public Property<String> getMainClass() {
126123
return this.mainClass;
127124
}
128125

129-
@Input
130-
@Optional
131-
public Property<Integer> getCodeGenDebugPort() {
132-
return this.codeGenDebugPort;
133-
}
134-
135126
@Input
136127
public Property<Boolean> getBuildTimePropertiesMatchIfMissing() {
137128
return this.buildTimePropertiesMatchIfMissing;

spring-aot-gradle-plugin/src/main/java/org/springframework/aot/gradle/tasks/GenerateAotSources.java

-32
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import java.io.File;
2020
import java.nio.file.Path;
2121
import java.util.ArrayList;
22-
import java.util.Arrays;
23-
import java.util.Collections;
2422
import java.util.List;
2523
import java.util.Set;
2624
import java.util.stream.Collectors;
@@ -40,7 +38,6 @@
4038

4139
import org.springframework.aot.BootstrapCodeGenerator;
4240
import org.springframework.aot.context.bootstrap.BootstrapCodeGeneratorRunner;
43-
import org.springframework.aot.gradle.SpringAotGradlePlugin;
4441
import org.springframework.aot.gradle.dsl.SpringAotExtension;
4542
import org.springframework.util.StringUtils;
4643

@@ -69,7 +66,6 @@ public GenerateAotSources() {
6966
this.aotOptions = new GenerateAotOptions(getProject().getExtensions().findByType(SpringAotExtension.class));
7067
setMain(BootstrapCodeGeneratorRunner.class.getCanonicalName());
7168
getArgumentProviders().add(new BootstrapGeneratorArgumentProvider());
72-
getJvmArgumentProviders().add(new BootstrapGeneratorJvmArgumentProvider());
7369
}
7470

7571
@InputFiles
@@ -145,32 +141,4 @@ private String toPathArgument(Set<File> files) {
145141
}
146142
}
147143

148-
private class BootstrapGeneratorJvmArgumentProvider implements CommandLineArgumentProvider {
149-
150-
@Override
151-
public Iterable<String> asArguments() {
152-
Integer debugPort = getDebugPort();
153-
if (debugPort != null) {
154-
return Arrays.asList("-Xdebug",
155-
"-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address="
156-
+ debugPort);
157-
}
158-
return Collections.emptyList();
159-
}
160-
161-
private Integer getDebugPort() {
162-
Object debugPortProperty =
163-
getProject().findProperty(SpringAotGradlePlugin.CODE_GEN_DEBUG_PORT_PROPERTY);
164-
if (debugPortProperty != null) {
165-
try {
166-
return Integer.parseInt(debugPortProperty.toString());
167-
} catch (NumberFormatException exception) {
168-
// ignore
169-
}
170-
}
171-
return GenerateAotSources.this.aotOptions.getCodeGenDebugPort().getOrNull();
172-
}
173-
174-
}
175-
176144
}

spring-native-docs/src/main/asciidoc/spring-aot.adoc

+15
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,21 @@ about specifying properties that activate configurations. (This is a work-in-pro
216216
by a comma separated list of prefixes to explicitly include or exclude (for example `default-include-all,!spring.dont.include.these.,!or.these` or `default-exclude-all,spring.include.this.one.though.,and.this.one`). When considering a property the
217217
longest matching prefix in this setting will apply (in cases where a property matches multiple prefixes).
218218

219+
==== Debugging the source generation
220+
221+
The Spring AOT plugins spawn a new process to perform the source generation. To remote debug this process, set the `codeGenDebugPort` parameter in the command line argument; then, the source generation process launches with a listener accepting a remote debugger on the specified port.
222+
223+
[source,bash,role="primary"]
224+
.Maven
225+
----
226+
$ mvn -Pnative spring-aot:generate@generate -Dspring.aot.codeGenDebugPort=9000
227+
----
228+
[source,bash,role="secondary"]
229+
.Gradle
230+
----
231+
$ gradle generateAot -PcodeGenDebugPort=9000
232+
----
233+
219234

220235
[[spring-aot-modes]]
221236
=== AOT Modes

0 commit comments

Comments
 (0)