arktekk / sbt-aether-deploy

Deploy SBT artifacts using Maven Artifact Resolver (formerly Eclipse Aether)
Other
84 stars 31 forks source link

Sbt plugin publishing is duplicated with SBT 1.9.0 #81

Closed seveneves closed 1 year ago

seveneves commented 1 year ago

SBT 1.9.0 introduced POM consistency of sbt plugin publishing, this publishes artifacts in two formats.

sbt 1.9.0 will publish using both the conventional POM-inconsistent style and POM-consisntent sytle so prior sbt releases can still consume the plugin. However, this can be opted-out using sbtPluginPublishLegacyMavenStyle setting.

Aether plugin does not support this new format. For example here is output of sbt publishM2 with different sbt version

1.8.3

[info]  published sbt-upload-bug to ~/.m2/repository/com/example/sbt-upload-bug_2.12_1.0/0.1.0-SNAPSHOT/sbt-upload-bug-0.1.0-SNAPSHOT.pom
[info]  published sbt-upload-bug to ~/.m2/repository/com/example/sbt-upload-bug_2.12_1.0/0.1.0-SNAPSHOT/sbt-upload-bug-0.1.0-SNAPSHOT.jar
[info]  published sbt-upload-bug to ~/.m2/repository/com/example/sbt-upload-bug_2.12_1.0/0.1.0-SNAPSHOT/sbt-upload-bug-0.1.0-SNAPSHOT-sources.jar
[info]  published sbt-upload-bug to ~/.m2/repository/com/example/sbt-upload-bug_2.12_1.0/0.1.0-SNAPSHOT/sbt-upload-bug-0.1.0-SNAPSHOT-javadoc.jar

1.9.0

[info]  published sbt-upload-bug to ~/.m2/repository/com/example/sbt-upload-bug_2.12_1.0/0.1.0-SNAPSHOT/sbt-upload-bug-0.1.0-SNAPSHOT-javadoc.jar
[info]  published sbt-upload-bug_2.12_1.0 to ~/.m2/repository/com/example/sbt-upload-bug_2.12_1.0/0.1.0-SNAPSHOT/sbt-upload-bug_2.12_1.0-0.1.0-SNAPSHOT.pom
[info]  published sbt-upload-bug to ~/.m2/repository/com/example/sbt-upload-bug_2.12_1.0/0.1.0-SNAPSHOT/sbt-upload-bug-0.1.0-SNAPSHOT-sources.jar
[info]  published sbt-upload-bug_2.12_1.0 to ~/.m2/repository/com/example/sbt-upload-bug_2.12_1.0/0.1.0-SNAPSHOT/sbt-upload-bug_2.12_1.0-0.1.0-SNAPSHOT.jar
[info]  published sbt-upload-bug_2.12_1.0 to ~/.m2/repository/com/example/sbt-upload-bug_2.12_1.0/0.1.0-SNAPSHOT/sbt-upload-bug_2.12_1.0-0.1.0-SNAPSHOT-sources.jar
[info]  published sbt-upload-bug to ~/.m2/repository/com/example/sbt-upload-bug_2.12_1.0/0.1.0-SNAPSHOT/sbt-upload-bug-0.1.0-SNAPSHOT.jar
[info]  published sbt-upload-bug to ~/.m2/repository/com/example/sbt-upload-bug_2.12_1.0/0.1.0-SNAPSHOT/sbt-upload-bug-0.1.0-SNAPSHOT.pom
[info]  published sbt-upload-bug_2.12_1.0 to ~/.m2/repository/com/example/sbt-upload-bug_2.12_1.0/0.1.0-SNAPSHOT/sbt-upload-bug_2.12_1.0-0.1.0-SNAPSHOT-javadoc.jar

❗Aether plugin is installing these artifacts into the same destination, this results in duplicated request to install artifacts to a remote repository, which can fail if re-uploading is disabled

Here's output of sbt aetherInstall | grep Installing to show this problem when running locally

1.9.0

[info] Installing com.example:sbt-upload-bug:jar:0.1.0-SNAPSHOT to ~/.m2/repository/com/example/sbt-upload-bug/0.1.0-SNAPSHOT/sbt-upload-bug-0.1.0-SNAPSHOT.jar
[info] Installing com.example:sbt-upload-bug:jar:javadoc:0.1.0-SNAPSHOT to ~/.m2/repository/com/example/sbt-upload-bug/0.1.0-SNAPSHOT/sbt-upload-bug-0.1.0-SNAPSHOT-javadoc.jar
[info] Installing com.example:sbt-upload-bug:pom:0.1.0-SNAPSHOT to ~/.m2/repository/com/example/sbt-upload-bug/0.1.0-SNAPSHOT/sbt-upload-bug-0.1.0-SNAPSHOT.pom
[info] Installing com.example:sbt-upload-bug:jar:sources:0.1.0-SNAPSHOT to ~/.m2/repository/com/example/sbt-upload-bug/0.1.0-SNAPSHOT/sbt-upload-bug-0.1.0-SNAPSHOT-sources.jar
[info] Installing com.example:sbt-upload-bug:jar:0.1.0-SNAPSHOT to ~/.m2/repository/com/example/sbt-upload-bug/0.1.0-SNAPSHOT/sbt-upload-bug-0.1.0-SNAPSHOT.jar
[info] Installing com.example:sbt-upload-bug:jar:sources:0.1.0-SNAPSHOT to ~/.m2/repository/com/example/sbt-upload-bug/0.1.0-SNAPSHOT/sbt-upload-bug-0.1.0-SNAPSHOT-sources.jar
[info] Installing com.example:sbt-upload-bug:pom:0.1.0-SNAPSHOT to ~/.m2/repository/com/example/sbt-upload-bug/0.1.0-SNAPSHOT/sbt-upload-bug-0.1.0-SNAPSHOT.pom
[info] Installing com.example:sbt-upload-bug:jar:javadoc:0.1.0-SNAPSHOT to ~/.m2/repository/com/example/sbt-upload-bug/0.1.0-SNAPSHOT/sbt-upload-bug-0.1.0-SNAPSHOT-javadoc.jar
[info] Installing com.example:sbt-upload-bug_2.12_1.0:0.1.0-SNAPSHOT/maven-metadata.xml to ~/.m2/repository/com/example/sbt-upload-bug_2.12_1.0/0.1.0-SNAPSHOT/maven-metadata-local.xml
[info] Installing com.example:sbt-upload-bug/maven-metadata.xml to ~/.m2/repository/com/example/sbt-upload-bug/maven-metadata-local.xml

Also, when setting key sbtPluginPublishLegacyMavenStyle to disable legacy format, Aether plugin is doing following

1.9.0 with sbtPluginPublishLegacyMavenStyle := false

[info] Installing com.example:sbt-upload-bug:jar:0.1.0-SNAPSHOT to ~/.m2/repository/com/example/sbt-upload-bug/0.1.0-SNAPSHOT/sbt-upload-bug-0.1.0-SNAPSHOT.jar
[info] Installing com.example:sbt-upload-bug:jar:0.1.0-SNAPSHOT to ~/.m2/repository/com/example/sbt-upload-bug/0.1.0-SNAPSHOT/sbt-upload-bug-0.1.0-SNAPSHOT.jar
[info] Installing com.example:sbt-upload-bug:jar:sources:0.1.0-SNAPSHOT to ~/.m2/repository/com/example/sbt-upload-bug/0.1.0-SNAPSHOT/sbt-upload-bug-0.1.0-SNAPSHOT-sources.jar
[info] Installing com.example:sbt-upload-bug:jar:javadoc:0.1.0-SNAPSHOT to ~/.m2/repository/com/example/sbt-upload-bug/0.1.0-SNAPSHOT/sbt-upload-bug-0.1.0-SNAPSHOT-javadoc.jar
[info] Installing com.example:sbt-upload-bug:pom:0.1.0-SNAPSHOT to ~/.m2/repository/com/example/sbt-upload-bug/0.1.0-SNAPSHOT/sbt-upload-bug-0.1.0-SNAPSHOT.pom
[info] Installing com.example:sbt-upload-bug_2.12_1.0:0.1.0-SNAPSHOT/maven-metadata.xml to ~/.m2/repository/com/example/sbt-upload-bug_2.12_1.0/0.1.0-SNAPSHOT/maven-metadata-local.xml
[info] Installing com.example:sbt-upload-bug/maven-metadata.xml to ~/.m2/repository/com/example/sbt-upload-bug/maven-metadata-local.xml

com.example:sbt-upload-bug:jar:0.1.0-SNAPSHOT is installed twice once again.

seveneves commented 1 year ago

Also, the pom files are broken for transitive plugin dependencies with 1.9.0 and some dependencies are not included resulting in wrong dependency tree when a released plugin is used

hamnis commented 1 year ago

This is known #76 . Patches are welcome.

seveneves commented 1 year ago

This is known #76 . Patches are welcome.

I saw the issues but misunderstood its content. Will see if I can create a PR for it

seveneves commented 1 year ago

Unfortunately, no time to fix this plugin in order to support sbt 1.9.0. Instead, I wrote a small plugin that suits my use case.

Here is some relevant info how to fix the problem post sbt 1.9.0 if anyone else is having the issue of publishing to maven repositories and having maven-metadata.xml updated at the same time (quite relevant for GitLab package repositories for example).

Precondition: all sbt projects that depend on the custom sbt plugin must be updated to 1.9.x

When publishing sbt plugin, use org.eclipse.aether.RepositorySystem to publish along with following set sbtPluginPublishLegacyMavenStyle := false

Create org.eclipse.aether.RepositorySystem with default services, no need to have a special case for sbt plugin.

Because of sbtPluginPublishLegacyMavenStyle no special overrides are required for org.eclipse.aether.spi.connector.layout.RepositoryLayoutFactory and org.eclipse.aether.impl.MetadataGenerator

val locator = new DefaultServiceLocator()
locator.addService(classOf[VersionResolver], classOf[DefaultVersionResolver])
locator.addService(classOf[VersionRangeResolver], classOf[DefaultVersionRangeResolver])
locator.addService(classOf[ArtifactDescriptorReader], classOf[DefaultArtifactDescriptorReader])
locator.addService(classOf[RepositoryConnectorFactory], classOf[BasicRepositoryConnectorFactory])
locator.addService(classOf[MetadataGeneratorFactory], classOf[VersionsMetadataGeneratorFactory])
locator.addService(classOf[ModelCacheFactory], classOf[DefaultModelCacheFactory])
locator.addService(classOf[TransporterFactory], classOf[HttpTransporterFactory])
val system: RepositorySystem = locator.getService(classOf[RepositorySystem])

convert Compile / packagedArtifacts into org.eclipse.aether.artifact.DefaultArtifact as

val request = new DeployRequest
request.addArtifact(
  new DefaultArtifact(
    organization,
    art.name,
    art.classifier.orNull,
    art.extension,
    version,
    Collections.emptyMap[String, String](),
    file,
  )
)

Finally, call system.deploy(session, deployRequest) to publish all artifacts with maven-metadata.xml updated.