How to remove a prefix from the archive names for all subprojects?

In my multiproject project I’m giving different names to my projects than the folder name. This is the contents of my settings.gradle file:

include 'eventos', 'modelo', 'negocio', 'negocio-impl', 'persistencia', 'persistencia-impl', 'visao'

rootProject.children.each {it.name = "eventos-" + it.name}

This is working well, but I noticed that the generated archives (ear, war, jar) are being named ‘eventos-eventos.ear’, ‘eventos-visao.war’, ‘eventos-persistencia.jar’ etc. I don’t want this. I would like to strip the prefix ‘eventos-’ from all this archives as was the case when the project names were the same as the folder names. How can I achieve this?

Thank you.

You should be able to change the archivesBaseName (it defaults to the project name):

https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:archivesBaseName

e.g., something like:

subprojects {
   archivesBaseName = project.name - 'eventos-'
}

Hello, sterling.

I’m using this code, based on yours, and almost everything is working fine:

subprojects {
	tasks.withType(Jar) {
		archivesBaseName = archivesBaseName - "eventos-"
	}	
	tasks.withType(War) {
		archivesBaseName = archivesBaseName - "eventos-"
	}	
	tasks.withType(Ear) {
		archivesBaseName = archivesBaseName - "eventos-"
	}	
}

The problem is that now my application.xml file in the ear file has two entries for the web module:

<?xml version="1.0"?>
<application xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="6">
  <module>
    <web>
      <web-uri>eventos-visao.war</web-uri>
      <context-root>eventos</context-root>
    </web>
  </module>
  <module>
    <web>
      <web-uri>visao.war</web-uri>
      <context-root>visao</context-root>
    </web>
  </module>
  <module>
    <ejb>negocio-impl.jar</ejb>
  </module>
  <library-directory>lib</library-directory>
</application>

Before changing the names it was only this:

  <module>
    <web>
      <web-uri>visao.war</web-uri>
      <context-root>eventos</context-root>
    </web>
  </module>

Is there a way to fix this? I would like to understand what’s going on.

For some reason it is working now, with the same code. By the way, is there a way to simplify this code to eliminate the duplication between the tasks?

subprojects {
	tasks.withType(Jar) {
		archivesBaseName = archivesBaseName - "eventos-"
	}	
	tasks.withType(War) {
		archivesBaseName = archivesBaseName - "eventos-"
	}	
	tasks.withType(Ear) {
		archivesBaseName = archivesBaseName - "eventos-"
	}	
}

Have you tried to do this with just the task.withType(Jar) {...} closure? The War and Ear tasks both extend Jar so task.withType(Jar) {...} should already be providing those two other task types to you as well. Temporarily throw println it into the closure if you want to verify exactly which tasks are being affected with this code.

subprojects {
    tasks.withType(Jar) {
        archivesBaseName = archivesBaseName - "eventos-"
    }
}
1 Like

Before your solution I was using:

subprojects
{
	def nomeBaseArquivos = {archivesBaseName = archivesBaseName - "eventos-"}
	tasks.withType(Jar) {configure nomeBaseArquivos}	
	tasks.withType(War) {configure nomeBaseArquivos}	
	tasks.withType(Ear) {configure nomeBaseArquivos}	
}

but your solution is far better as ear and war tasks are also jar tasks. Thank you.