# Generate another jar from main with compiled/generated classes

**URL:** <https://discuss.gradle.org/t/generate-another-jar-from-main-with-compiled-generated-classes/45624>\
**Category:** Help/Discuss\
**Created:** [May 18, 2023, 3:23pm UTC](https://discuss.gradle.org/t/generate-another-jar-from-main-with-compiled-generated-classes/45624 "2023-05-18T15:23:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leonardo](https://avatars.discourse-cdn.com/v4/letter/l/e56c9b/32.png) [@Leonardo](https://discuss.gradle.org/u/Leonardo)\
**Post date:** [May 18, 2023, 3:23pm UTC](https://discuss.gradle.org/t/generate-another-jar-from-main-with-compiled-generated-classes/45624/1 "2023-05-18T15:23:52Z")

</div>

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

I have a Java project with a conventional structure:

```plaintext
- 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:

```plaintext
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.

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [May 18, 2023, 5:14pm UTC](https://discuss.gradle.org/t/generate-another-jar-from-main-with-compiled-generated-classes/45624/2 "2023-05-18T17:14:07Z")

</div>

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.
