Generate another jar from main with compiled/generated classes

Dear all, thank you for your time in reading this.

I have a Java project with a conventional structure:

- build
- src
| - main
| - test

The project uses a bunch of annotation processors that generate Java files and Resource files. The project is like the server side or provider.

The annotation processors also generate a bunch of files that should be shared with different projects that want to consume the service. I will need to create a different jar file with only the specific files that can be shared.

I was able to create a Jar task to achieve it:

var sharedFiles = "META-INF/A5/shared-files.txt"  
  
tasks.register<Jar>("sharedJar") {  
	group = "A5-Build"  
	description = "Assemble a jar archive containing the client classes"  
	dependsOn(tasks.named("jar"))  
	onlyIf {  
		sourceSets["main"].output.filter { 
			file -> File(file,sharedFiles).exists()  
		}.files.size > 0  }  
		
	archiveBaseName.set(archiveBaseName.get() + "-shared")  
	from(sourceSets["main"].output) {  
		include("META-INF/A5/**")  
		for (file in sourceSets["main"].output) {  
			var tmp = File(file, sharedFiles)  
			if (tmp.exists()) {  
				var lines = Files.readAllLines(tmp.toPath()).map { s -> s.replace(".", "/") }  
				lines.forEach { f -> include("$f**") }  
				break;  
			}  
		}  
	}  
}  
tasks.named("jar") { finalizedBy("sharedJar") }

That does the job. For sure it can be improved (suggestions are welcome) but now my problem is to publish the generated jar file. Please note, I am using the maven-publish plugging which publishes the main jar but not the one that I just created with my custom task.

All these generated jars will be registered in a private Maven repository. So, projects can pick them up using their coordinates. Besides, I will need to include the Jandex indexing of the files.

I am looking for a different approach to creating a custom SourceSet and taking advantage of the Java plugin that does all that for me. The trick is that I can create a sourceSet only after the jar task is executed due that the files are generated automatically at compilation time.

Thank you for all the help and guidance that you can provide to me.

Note: I am generating a Java Framework to be used by developers. It requires to develop Gradle plugin and extensions as well.

You probably want to create a feature variant that is published too.
If you want it more ad-hoc and dirty, just add the artifact to the Java component so that it gets published along.
Or if you want to publish under different coordinates, define a second publication for those coordinates where you publish that artifact.