Check if project is a webapp or plain java


(Wujek Srujek) #1

I am writing a small task in buildSrc that will apply to Java project as well as to webapp projects. If the project is a webapp project, it should do the same things as for Java, plus a few things more. I want to keep it dumb and simple for now, there will be pretty much no configuration available; nonetheless, I would like to be able to discover automatically if a projects is a webapp project. How can this be achieved? I could for example check if the project has a task of type war or similar, but it is a kind of a hack. Is there a canonical way?

wujek


(Peter Niederwieser) #2
project.plugins.withType(org.gradle.api.plugins.WarPlugin) {
   // put web-specific code here
}

Usually, such decisions are made by a plugin, which then adds the appropriate tasks.


(Wujek Srujek) #3

I came up with:

if (project.plugins.hasPlugin(WarPlugin))

but I guess this is the same thing as yours.

Yeah, I am not using a plugin yet, but I might end up writing one. But it would mean that in the subprojects closure, I would ask for all projects that have the type java (I guess groovy and scala and whatnots would be caught as well) and apply a plugin to them. Then, I would filter all war projects, and apply the same plugin, maybe with some setting so that the task knows it is for a web (alternatively, applying two different tasks, but they are so similar…) What I do now is much simpler - I just apply the task to all projects, and the task itself, when it is called, decided whether it should do the war-additional stuff or not. So I have one decision point, instead of 3 (picking non-wars, picking wars, and iffing whether it is a war or not). Do you think a plugin would be better here? Maybe you have another idea what I should do this most elegantly and easily?

Thanks for your reply, it confirmed what I did.

wujek


(Andrew Oberstar) #4
plugins.withType(WarPlugin)

and

plugins.hasPlugin(WarPlugin)

These are actually quite different. HasPlugin checks whether the plugin is applied at the particular moment the method is executed. In Gradle that’s not always a safe assumption to make because the WarPlugin could be added afterwards.

WithType on the other hand will apply the code immediately, if the plugin is already applied, otherwise it will run if and when the plugin is applied in the future.

However, that behavior can make it tricky to implement something where if just the JavaPlugin is applied you do one thing, but if the WarPlugin is applied you do another thing.

I don’t know that this is particularly helpful, but it can be an important distinction.


(Wujek Srujek) #5

Oh, ok, thanks, I didn’t know that. I thought hasPlugin(war) was sth like withType(war).size() > 0. But in my case, as that code runs in a task, I think it is safe to assume that all plugins have been applied already? I would consider other plugins being applied at that stage (this would have to happen in other tasks) to be unhealthy. But is it? What do you say?


(Andrew Oberstar) #6

Yeah, you should be safe if you’re doing that in the task action.