How can i identify if a gradle project is a java project when it does not directly apply the java plugin

I am able to identify if a gradle project has applied java/ java-library/ application/ java-platform plugins in the build.gradle if they are applied directly.

However there is no way to identify the same if there are other plugins being used which uses java plugins indirectly.

Question : how can i successfully identify if a gradle project is a pure java project (and not say scala/or kotlin projects,etc)

For example take a look at the below build.gradle file

I am using org.springframework.boot plugins and it is a java source code .

plugins {
	id 'io.spring.nohttp'
	id 'org.springframework.pulsar.root-project'
	id 'org.springframework.pulsar.update-version'
	id 'org.ajoberstar.grgit' version '4.0.1' apply false
	id "org.springframework.boot" version "3.2.0-SNAPSHOT"
	id 'io.spring.dependency-management' version '1.1.0'
}

description = 'Spring for Apache Pulsar'

apply from: 'gradle/aggregate-jacoco-report.gradle'
apply from: 'gradle/update-copyrights.gradle'


allprojects {
	group = 'org.springframework.pulsar'
	configurations.all {
		resolutionStrategy {
			cacheChangingModulesFor 0, "seconds"
			cacheDynamicVersionsFor 0, "seconds"
		}
	}
}

if (hasProperty('buildScan')) {
	buildScan {
		termsOfServiceUrl = 'https://gradle.com/terms-of-service'
		termsOfServiceAgree = 'yes'
	}
}

nohttp {
	allowlistFile = project.file('src/nohttp/allowlist.lines')
	source.exclude "**/bin/**"
	source.exclude "**/build/**"
	source.exclude "**/out/**"
	source.exclude "**/target/**"
	source.exclude "**/*.dylib"
	source.exclude "**/*.gif"
	source.exclude "**/.gradle/**"
}

check {
	dependsOn checkstyleNohttp
}

I am able to run gradlew compileJava command successfully, however when i try to locate the compileJava tasks by running gradlew tasks it does not appear.

When i use the below groovy code , even then it does not appear.

println "Found task: ${project.tasks.findByPath('Jar')} ${project.findProperty('compileJava')} ${project.findProperty('JavaCompile')}"

If i use the below groovy code,

println "${project.plugins}"

i get the following output,

[org.gradle.api.plugins.HelpTasksPlugin$Inject@4d9e0c94, org.gradle.buildinit.plugins.BuildInitPlugin$Inject@3deb5450, org.gradle.buildinit.plugins.WrapperPlugin$Inject@3739ab18, org.gradle.api.plugins.ReportingBasePlugin$Inject@38e2508, org.gradle.api.plugins.quality.CheckstylePlugin$Inject@114f8c2a, io.spring.nohttp.gradle.NoHttpCheckstylePlugin@42bf2747, io.spring.nohttp.gradle.NoHttpCliPlugin@2f7ccde6, io.spring.nohttp.gradle.NoHttpPlugin@6482f523, org.gradle.language.base.plugins.LifecycleBasePlugin$Inject@968d5e8, org.gradle.api.plugins.BasePlugin$Inject@4a444d03, io.github.gradlenexus.publishplugin.NexusPublishPlugin@67a88d76, org.springframework.pulsar.gradle.publish.SpringNexusPublishPlugin@6385273f, org.jfrog.gradle.plugin.artifactory.ArtifactoryPlugin@36ddd78d, io.spring.gradle.convention.ArtifactoryPlugin@d18d5cd, org.sonarqube.gradle.SonarQubePlugin@144353eb, org.springframework.pulsar.gradle.check.SonarQubeConventionsPlugin@32d9d657, org.springframework.pulsar.gradle.RootProjectPlugin@1f0cbc2f, org.springframework.pulsar.gradle.versions.UpdateProjectVersionPlugin@4d7f5ab4, org.springframework.boot.gradle.plugin.SpringBootPlugin@30a313be, io.spring.gradle.dependencymanagement.DependencyManagementPlugin@1f319d9b, org.gradle.testing.jacoco.plugins.JacocoPlugin$Inject@54d9b30e, org.ajoberstar.grgit.gradle.GrgitPlugin@1aa6025d]

Besides that you have several bad practices in that build script like using legacy script plugins (the ones with “apply from”), using allprojects { ... }, and using the Spring Dependency Management plugin instead of the built-in BOM support which by now does more harm than good and for which even the maintainer recommends not to use it anymore.

What is the reason you want to check for “pure Java” project?

If your intention / use-case is more clear, you can maybe get a better advice.

I am building a ci script that can generate some metrics for my project, essentially aggregating pure java gradle projects vs some scala gradle projects.

Hope that helps

Unfortunately not enough for me to come by with a good suggestion.

Idiomatically and reliably you cannot really check whether a plugin is not applied.
You can react to a plugin being applied using pluginManager.withPlugin("...") { ... }, but you would then e. g. only know the java plugin was applied, but not that the scala plugin was not applied.

If you want to do different things in Java projects than in Scala projects, but with centralized build logic, the idiomatic way is to have a convention plugin that does java things and a convention plugin that does scala things and then apply it accordingly in the according projects directly.

But I’m not sure how to best do what you want, as I have no idea what your “ci script” is or does or how it works.

Thank you Bjorn,

one question, is there something that can explain why i am able to perform a “gradlew compileJava” successfully, however i am not able to see that task when i run “gradlew tasks”

It is Björn, or Bjoern, not Bjorn. :wink:

The compileJava task has no assigned group, so it is only shown if you use gradlew tasks --all.

My apologies, Bjoern

Indeed, i am able to see compileJava task when i execute ‘./gradlew spring-pulsar:tasks --all’

Would you say that this is a good indicator for figuring out if a project is a java project?

Is there a groovy /gradle api equivalent to get the same result? I am afraid the below doesnt really work, i dont see compileJava listed


println "found ${gradleProject.tasks.getNames()}"
def tt =gradleProject.getDefaultTasks()
tt.each{task->
println "default tasks : ${task}"
}

Would you say that this is a good indicator for figuring out if a project is a java project?

No, scala applies java implicitly, so also a Scala project has compileJava.

I am afraid the below doesnt really work, i dont see compileJava listed

getDefaultTasks gives you the configured default tasks. I.e. the tasks that are run if you just invoke Gradle without specifying any task explicitly, so that does of course not contain compileJava.

project.tasks.names should indeed contain compileJava unless you invoke it at a time where the java plugin was not applied yet.

1 Like