matrei commented on code in PR #15361:
URL: https://github.com/apache/grails-core/pull/15361#discussion_r2749476501
##########
grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/core/GrailsGradlePlugin.groovy:
##########
@@ -263,29 +263,35 @@ ${importStatements}
}
/**
- * Check if a dependency is present in the configuration hierarchy.
- * This checks all dependencies including those from parent configurations
via extendsFrom.
+ * Check if a class exists on the given classpath.
+ * This detects classes from any source: direct dependencies, transitive
dependencies, or local jars.
*
- * @param project The Gradle project
- * @param configurationName The configuration to check (e.g.,
'compileClasspath')
- * @param group The dependency group (e.g., 'org.apache.grails.data')
- * @param name The dependency name (e.g., 'grails-datamapping-core')
- * @return true if the dependency is present in the configuration hierarchy
+ * @param classpath The FileCollection representing the classpath to search
+ * @param className The fully qualified class name to look for (e.g.,
'grails.gorm.annotation.CreatedDate')
+ * @return true if the class is found on the classpath
*/
- protected boolean hasDependency(Project project, String configurationName,
String group, String name) {
- try {
- def configuration =
project.configurations.findByName(configurationName)
- if (configuration == null) {
- return false
- }
-
- return configuration.allDependencies.any { d ->
- d.group == group && d.name == name
+ protected boolean isClassOnClasspath(FileCollection classpath, String
className) {
+ String classPath = className.replace('.', '/') + '.class'
+ for (File file : classpath.files) {
+ try {
+ if (file.isFile() && file.name.endsWith('.jar')) {
+ def zip = new java.util.zip.ZipFile(file)
+ try {
+ if (zip.getEntry(classPath) != null) {
+ return true
+ }
+ } finally {
+ zip.close()
+ }
+ } else if (file.isDirectory()) {
+ if (new File(file, classPath).exists()) {
+ return true
+ }
+ }
+ } catch (Exception ignored) {
}
- } catch (Exception e) {
- project.logger.debug("Failed to check for dependency
${group}:${name} in ${configurationName}: ${e.message}")
- return false
}
+ return false
}
Review Comment:
```suggestion
private static boolean isClassOnClasspath(FileCollection classpath,
String className) {
def classEntry = className.replace('.', '/') + '.class'
classpath.files.any { f ->
try {
if (f.file && f.name.endsWith('.jar')) {
new ZipFile(f).withCloseable { zip ->
zip.getEntry(classEntry) != null
}
} else if (f.directory) {
new File(f, classEntry).exists()
} else {
false
}
} catch (Exception ignored) {
false
}
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]