5151 */
5252public class FilesTaskV2 extends DefaultTask {
5353
54- private static final GradleVersion GRADLE_9 = GradleVersion .version ("9.0" );
55-
5654 private final SkaffoldFilesOutput skaffoldFilesOutput = new SkaffoldFilesOutput ();
5755
5856 @ Nullable private JibExtension jibExtension ;
@@ -81,26 +79,26 @@ public void listFiles() throws IOException {
8179
8280 // Add extra layer
8381 List <Path > extraDirectories =
84- jibExtension .getExtraDirectories ().getPaths ().stream ()
85- .map (ExtraDirectoryParameters ::getFrom )
86- .collect (Collectors .toList ());
82+ jibExtension .getExtraDirectories ().getPaths ().stream ()
83+ .map (ExtraDirectoryParameters ::getFrom )
84+ .collect (Collectors .toList ());
8785 extraDirectories .stream ().filter (Files ::exists ).forEach (skaffoldFilesOutput ::addInput );
8886
8987 // Find project dependencies
9088 Set <ProjectDependency > projectDependencies = findProjectDependencies (project );
9189
9290 Set <File > projectDependencyJars = new HashSet <>();
9391 for (ProjectDependency projectDependency : projectDependencies ) {
94- addProjectFiles (projectDependency .getDependencyProject ());
92+ Project dependentProject = getDependentProject (projectDependency );
93+ addProjectFiles (dependentProject );
9594
9695 // Keep track of project dependency jars for filtering out later
9796 String configurationName = projectDependency .getTargetConfiguration ();
9897 if (configurationName == null ) {
9998 configurationName = "default" ;
10099 }
101- Project dependencyProject = projectDependency .getDependencyProject ();
102100 for (Configuration targetConfiguration :
103- dependencyProject .getConfigurations ().getByName (configurationName ).getHierarchy ()) {
101+ dependentProject .getConfigurations ().getByName (configurationName ).getHierarchy ()) {
104102 for (PublishArtifact artifact : targetConfiguration .getArtifacts ()) {
105103 projectDependencyJars .add (artifact .getFile ());
106104 }
@@ -109,7 +107,7 @@ public void listFiles() throws IOException {
109107
110108 // Add SNAPSHOT, non-project dependency jars
111109 for (File file :
112- project .getConfigurations ().getByName (jibExtension .getConfigurationName ().get ())) {
110+ project .getConfigurations ().getByName (jibExtension .getConfigurationName ().get ())) {
113111 if (!projectDependencyJars .contains (file ) && file .toString ().contains ("SNAPSHOT" )) {
114112 skaffoldFilesOutput .addInput (file .toPath ());
115113 projectDependencyJars .add (file ); // Add to set to avoid printing the same files twice
@@ -142,20 +140,42 @@ private void addGradleFiles(Project project) {
142140 skaffoldFilesOutput .addBuild (project .getBuildFile ().toPath ());
143141
144142 // Add settings.gradle
145- if (GradleVersion .current ().compareTo (GRADLE_9 ) < 0
146- && project .getGradle ().getStartParameter ().getSettingsFile () != null ) {
147- skaffoldFilesOutput .addBuild (
148- project .getGradle ().getStartParameter ().getSettingsFile ().toPath ());
149- } else if (Files .exists (projectPath .resolve (Settings .DEFAULT_SETTINGS_FILE ))) {
150- skaffoldFilesOutput .addBuild (projectPath .resolve (Settings .DEFAULT_SETTINGS_FILE ));
151- }
143+ addSettingsFile (project , projectPath );
152144
153145 // Add gradle.properties
154146 if (Files .exists (projectPath .resolve ("gradle.properties" ))) {
155147 skaffoldFilesOutput .addBuild (projectPath .resolve ("gradle.properties" ));
156148 }
157149 }
158150
151+ /**
152+ * Adds the settings.gradle file for a project.
153+ *
154+ * <p>Uses reflection to call getSettingsFile() for compatibility with both Gradle 6 and 9
155+ * (getSettingsFile() was removed in Gradle 9).
156+ *
157+ * @param project the project
158+ * @param projectPath the project directory path
159+ */
160+ private void addSettingsFile (Project project , Path projectPath ) {
161+ try {
162+ Object startParameter = project .getGradle ().getStartParameter ();
163+ java .lang .reflect .Method getSettingsFileMethod =
164+ startParameter .getClass ().getMethod ("getSettingsFile" );
165+ File settingsFile = (File ) getSettingsFileMethod .invoke (startParameter );
166+ if (settingsFile != null ) {
167+ skaffoldFilesOutput .addBuild (settingsFile .toPath ());
168+ } else if (Files .exists (projectPath .resolve (Settings .DEFAULT_SETTINGS_FILE ))) {
169+ skaffoldFilesOutput .addBuild (projectPath .resolve (Settings .DEFAULT_SETTINGS_FILE ));
170+ }
171+ } catch (ReflectiveOperationException e ) {
172+ // Fall back to default settings file location if reflection fails
173+ if (Files .exists (projectPath .resolve (Settings .DEFAULT_SETTINGS_FILE ))) {
174+ skaffoldFilesOutput .addBuild (projectPath .resolve (Settings .DEFAULT_SETTINGS_FILE ));
175+ }
176+ }
177+ }
178+
159179 /**
160180 * Prints build files, sources, and resources associated with a project.
161181 *
@@ -167,19 +187,19 @@ private void addProjectFiles(Project project) {
167187
168188 // Add sources + resources
169189 SourceSetContainer sourceSetContainer =
170- project .getExtensions ().findByType (SourceSetContainer .class );
190+ project .getExtensions ().findByType (SourceSetContainer .class );
171191 if (sourceSetContainer != null ) {
172192 SourceSet mainSourceSet = sourceSetContainer .findByName (SourceSet .MAIN_SOURCE_SET_NAME );
173193 if (mainSourceSet != null ) {
174194 mainSourceSet
175- .getAllSource ()
176- .getSourceDirectories ()
177- .forEach (
178- sourceDirectory -> {
179- if (sourceDirectory .exists ()) {
180- skaffoldFilesOutput .addInput (sourceDirectory .toPath ());
181- }
182- });
195+ .getAllSource ()
196+ .getSourceDirectories ()
197+ .forEach (
198+ sourceDirectory -> {
199+ if (sourceDirectory .exists ()) {
200+ skaffoldFilesOutput .addInput (sourceDirectory .toPath ());
201+ }
202+ });
183203 }
184204 }
185205 }
@@ -204,15 +224,15 @@ private Set<ProjectDependency> findProjectDependencies(Project project) {
204224
205225 // Search through all dependencies
206226 Configuration runtimeClasspath =
207- currentProject .getConfigurations ().findByName (configurationName );
227+ currentProject .getConfigurations ().findByName (configurationName );
208228 if (runtimeClasspath != null ) {
209229 for (Configuration configuration : runtimeClasspath .getHierarchy ()) {
210230 for (Dependency dependency : configuration .getDependencies ()) {
211231 if (dependency instanceof ProjectDependency ) {
212232 // If this is a project dependency, save it
213233 ProjectDependency projectDependency = (ProjectDependency ) dependency ;
214234 if (!projectDependencies .contains (projectDependency )) {
215- projects .push (projectDependency . getDependencyProject ( ));
235+ projects .push (getDependentProject ( projectDependency ));
216236 projectDependencies .add (projectDependency );
217237 }
218238 }
@@ -222,4 +242,34 @@ private Set<ProjectDependency> findProjectDependencies(Project project) {
222242 }
223243 return projectDependencies ;
224244 }
225- }
245+
246+ /**
247+ * Resolves a {@link ProjectDependency} to its corresponding {@link Project} instance.
248+ *
249+ * <p>Uses reflection to handle both Gradle 6 (getDependencyProject()) and Gradle 9+ (getPath()).
250+ *
251+ * @param projectDependency the project dependency to resolve
252+ * @return the resolved project
253+ * @throws RuntimeException if the dependent project could not be resolved
254+ */
255+ private Project getDependentProject (ProjectDependency projectDependency ) {
256+ // Try getDependencyProject() first (Gradle 6-8)
257+ try {
258+ java .lang .reflect .Method getDependencyProjectMethod =
259+ projectDependency .getClass ().getMethod ("getDependencyProject" );
260+ return (Project ) getDependencyProjectMethod .invoke (projectDependency );
261+ } catch (ReflectiveOperationException e ) {
262+ // Fall through to getPath() approach (Gradle 9+)
263+ }
264+
265+ // Try getPath() approach (Gradle 9+)
266+ try {
267+ java .lang .reflect .Method getPathMethod = projectDependency .getClass ().getMethod ("getPath" );
268+ String path = (String ) getPathMethod .invoke (projectDependency );
269+ return getProject ().project (path );
270+ } catch (ReflectiveOperationException ex ) {
271+ throw new RuntimeException (
272+ "Failed to resolve dependent project from " + projectDependency , ex );
273+ }
274+ }
275+ }
0 commit comments