If you really want to ship those classes together, you should probably just move them from src/test/java to src/main/java.
But most probably it would be a better idea to just use the java-test-fixtures plugin as the test fixtures are then by default also published automatically and a downstream project can depend on specifically the test fixtures in their tests when necessary.
Btw. you should neither define artifact... directly in a publication, but better add it to the java component, nor do you need to manually set up a sourcesJar task, you can simply do java { withSourcesJar() } which will create a sources jar task and also publish it properly.
The problem is more about the need to exclude some classes from ‘src/main’.
I find out only one solution for this problem
tasks.register('apiJar', Jar) {
from sourceSets.main.output
exclude("path/to/class")
}
I don’t know how to specify that I also need to generate the pom file with dependencies in that case
Previously this work has been covered by this part
maven(MavenPublication) {
from components.java
...
but now if I use
maven(MavenPublication) {
from components.java
artifact apiJar
...
I get an error: “Invalid publication ‘maven’: multiple artifacts with the identical extension and classifier (‘jar’, ‘null’).”
Yeah, well, what the error tells you.
You try to publish two jars with the same coordinates, as your apiJar has no classifier, so has the same coordinates as the original jar.
Besides that - as said before - you should not use artifact ... in the plublication, but if you want to add an artifact there, add it to the java component instead.
How to properly solve your issue is hard to say, as it is still unclear what you want to achieve. You could for example define a classifier for the apiJar task if you indeed want to publish it additionally. Or you could for example do the exclude on the standard jar task if not.