Changing archivesBaseName in custom plugin


(radhakrishnand) #1

Hi,

we are writing a custom gradle plugin for standardizing our gradle files.

The idea is to append -api, -impl, and -deployready to the uploaded artifacts.

Today,we are doing it by giving archivesBaseName=“xxx-api” in the gradle file.

How we want to do it , in the gradle file we will have serviceName=“xxx”;

the plugin must set the archivesBaseName as serviceName-api.

How to do this. i tired getting project.archivesBaseName. It is throwing error saying archivesBaseName is not found in project.


(Rolf Suurd) #2

baseName is a property of AbstractArchiveTask. Your plugin should get all tasks that are an AbstractArchiveTask and then set the baseName property based on the serviceName property.

Here’s the simplest way to do this for one project:

apply plugin: 'java'
  String serviceName='api'
  project.tasks.withType(AbstractArchiveTask).each {AbstractArchiveTask task ->
 task.baseName = "${project.name}-${serviceName}"
}

When invoking “gradle jar”, the above script results in a xxx-api.jar in the build/libs folder.


(radhakrishnand) #3

Thanks rolf, it worked :slight_smile:


(Luke Daley) #4

@Rolf, you don’t need the .each there and it’s actually harmful.

You should do…

project.tasks.withType(AbstractArchiveTask) { AbstractArchiveTask task ->
    task.baseName = "${project.name}-${serviceName}"
}

The key difference is that with that version the closure gets stored as a kind of callback and gets run for all future 'AbstractArchiveTask’s as well. Using ‘.each’ there only applies the configuration to existing tasks.


(Rolf Suurd) #5

Hey Luke,

Is that why i couldn’t get

task.baseName = “${project.name}-${ext.serviceName}”

to work properly?

Greets Rolf


(Luke Daley) #6

Rolf,

I think so. The way you had it, it would only apply to tasks that exist right at that point and wouldn’t configure tasks created in the future.