gradle-nexus / publish-plugin

Gradle plugin for publishing to Nexus repositories
Apache License 2.0
402 stars 29 forks source link

The default artifact will still be published even when an artifact ID has been explicitly specified. #309

Open Goooler opened 8 months ago

Goooler commented 8 months ago

My config like this:

publishing {
    publications {
        create<MavenPublication>("mavenJava") {
            artifactId = "legacy-osgi-gradle-plugin"
            ...
        }
    }
}

artifactId has been declared explicitly, and the default project name is gradle-legacy-osgi-plugin, then publish it to MavenLocal:

13:08:30: Executing 'publishToMavenLocal --no-configuration-cache'...

> Task :compileJava FROM-CACHE
> Task :compileGroovy NO-SOURCE
> Task :pluginDescriptors
> Task :processResources
> Task :classes
> Task :jar
> Task :javadoc FROM-CACHE
> Task :javadocJar
> Task :sourcesJar
> Task :generateMetadataFileForMavenJavaPublication
> Task :generatePomFileForMavenJavaPublication
> Task :signMavenJavaPublication
> Task :generatePomFileForOsgiPluginPluginMarkerMavenPublication
> Task :signOsgiPluginPluginMarkerMavenPublication
> Task :generateMetadataFileForPluginMavenPublication
> Task :generatePomFileForPluginMavenPublication
> Task :signPluginMavenPublication
> Task :publishMavenJavaPublicationToMavenLocal
> Task :publishOsgiPluginPluginMarkerMavenPublicationToMavenLocal
> Task :publishPluginMavenPublicationToMavenLocal
> Task :publishToMavenLocal

BUILD SUCCESSFUL in 33s
18 actionable tasks: 16 executed, 2 from cache

tree ~/.m2/repository

/Users/snebula/.m2/repository
└── io
    └── github
        └── goooler
            └── osgi
                ├── gradle-legacy-osgi-plugin
                │   ├── 0.8.3-SNAPSHOT
                │   │   ├── gradle-legacy-osgi-plugin-0.8.3-SNAPSHOT-javadoc.jar
                │   │   ├── gradle-legacy-osgi-plugin-0.8.3-SNAPSHOT-javadoc.jar.asc
                │   │   ├── gradle-legacy-osgi-plugin-0.8.3-SNAPSHOT-sources.jar
                │   │   ├── gradle-legacy-osgi-plugin-0.8.3-SNAPSHOT-sources.jar.asc
                │   │   ├── gradle-legacy-osgi-plugin-0.8.3-SNAPSHOT.jar
                │   │   ├── gradle-legacy-osgi-plugin-0.8.3-SNAPSHOT.jar.asc
                │   │   ├── gradle-legacy-osgi-plugin-0.8.3-SNAPSHOT.module
                │   │   ├── gradle-legacy-osgi-plugin-0.8.3-SNAPSHOT.module.asc
                │   │   ├── gradle-legacy-osgi-plugin-0.8.3-SNAPSHOT.pom
                │   │   ├── gradle-legacy-osgi-plugin-0.8.3-SNAPSHOT.pom.asc
                │   │   └── maven-metadata-local.xml
                │   └── maven-metadata-local.xml
                ├── io.github.goooler.osgi.gradle.plugin
                │   ├── 0.8.3-SNAPSHOT
                │   │   ├── io.github.goooler.osgi.gradle.plugin-0.8.3-SNAPSHOT.pom
                │   │   ├── io.github.goooler.osgi.gradle.plugin-0.8.3-SNAPSHOT.pom.asc
                │   │   └── maven-metadata-local.xml
                │   └── maven-metadata-local.xml
                └── legacy-osgi-gradle-plugin
                    ├── 0.8.3-SNAPSHOT
                    │   ├── legacy-osgi-gradle-plugin-0.8.3-SNAPSHOT-javadoc.jar
                    │   ├── legacy-osgi-gradle-plugin-0.8.3-SNAPSHOT-javadoc.jar.asc
                    │   ├── legacy-osgi-gradle-plugin-0.8.3-SNAPSHOT-sources.jar
                    │   ├── legacy-osgi-gradle-plugin-0.8.3-SNAPSHOT-sources.jar.asc
                    │   ├── legacy-osgi-gradle-plugin-0.8.3-SNAPSHOT.jar
                    │   ├── legacy-osgi-gradle-plugin-0.8.3-SNAPSHOT.jar.asc
                    │   ├── legacy-osgi-gradle-plugin-0.8.3-SNAPSHOT.module
                    │   ├── legacy-osgi-gradle-plugin-0.8.3-SNAPSHOT.module.asc
                    │   ├── legacy-osgi-gradle-plugin-0.8.3-SNAPSHOT.pom
                    │   ├── legacy-osgi-gradle-plugin-0.8.3-SNAPSHOT.pom.asc
                    │   └── maven-metadata-local.xml
                    └── maven-metadata-local.xml

11 directories, 28 files

We just expect io.github.goooler.osgi.gradle.plugin and legacy-osgi-gradle-plugin here, but gradle-legacy-osgi-plugin has been published as well, which with the default project name.

Full config here https://github.com/Goooler/gradle-legacy-osgi-plugin/commit/b941a218a7a89beb207dec0033d7aec22872a017.

TWiStErRob commented 8 months ago

Beautiful report!

Based on what you have in OP, I think this is WAI because we have 3 publications in the logs:

> Task :publishMavenJavaPublicationToMavenLocal
> Task :publishOsgiPluginPluginMarkerMavenPublicationToMavenLocal
> Task :publishPluginMavenPublicationToMavenLocal

and 3 artifacts in the folder.

Try to change to

named<MavenPublication>("pluginMaven").configure { ... }

~(You might need afterEvaluate or matching to get the timing right. source)~ The timing issue seems fixed, so I need to review my workaround.

Btw, I think this issue is with id("com.gradle.plugin-publish") because this nexus plugin only sets up the sonatype tasks not the maveLocal ones. That said it would be the same issue just with harder visibility on folder contents.

Goooler commented 8 months ago

Seems can't:

Publication with name 'pluginMaven' not found.

publishing.publications is empty.

TWiStErRob commented 8 months ago

even in afterEvaluate?

Goooler commented 8 months ago
publishing {
    publications {
        afterEvaluate {
            named<MavenPublication>("pluginMaven") {
                ...
            }
        }
    }
}
FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'gradle-legacy-osgi-plugin'.
> Maven publication 'pluginMaven' cannot include multiple components
TWiStErRob commented 8 months ago

That's so weird, looking.

TWiStErRob commented 8 months ago

Ah, the error is not that it's not found, but it by default includes the Java component, see org.gradle.plugin.devel.plugins.MavenPluginPublishPlugin.

Here's what I would go with: use all the built-ins and no workarounds needed: publishing.patch (delete all the commented lines when applying, they're just for you, keep // Pre-configure ...)

Docs:

Goooler commented 8 months ago

Thanks a lot! It works for me now.

Should we note this in README or fallback it?

TWiStErRob commented 8 months ago

I'm not sure what we could add, @szpak thoughts?

Note: this plugin is a generic publishing plugin, publishing Gradle plugins still uses all the same facilities, except Gradle provided plugin does most of the wiring that's still required for normal library jars.

I think this might need a full rewrite of the readme, giving examples for the most common use cases, so it's easier for people to copy-paste when they don't know/want to know the details?

szpak commented 8 months ago

I would agree with Rob, that it's rather a problem with the generic publishing configuration in Gradle.

Do you have a good idea/proposal how the new README could look like? We could also fold some sections to make it easier to read (and to find the basic cases).