Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate version information when publishing artifacts #1188

Merged
merged 1 commit into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ lazy val core = project
.enablePlugins(AutomateHeaderPlugin)
.disablePlugins(SitePlugin)
.settings(commonSettings)
.settings(VersionGenerator.settings)
.settings(
name := "akka-stream-kafka",
AutomaticModuleName.settings("akka.stream.alpakka.kafka"),
Expand Down
35 changes: 35 additions & 0 deletions project/VersionGenerator.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import sbt._
import sbt.Keys._

/**
* Generate version.conf and akka/kafka/Version.scala files based on the version setting.
*
* This was adapted from https://github.com/akka/akka/blob/v2.6.8/project/VersionGenerator.scala
*/
object VersionGenerator {

val settings: Seq[Setting[_]] = inConfig(Compile)(
Seq(
resourceGenerators += generateVersion(resourceManaged, _ / "version.conf", """|akka.kafka.version = "%s"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't the version.conf file collide with others in a fat-jar scenario?
I guess those could be merged.

Copy link
Contributor Author

@marcospereira marcospereira Aug 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If multiple artifacts contain version.conf files, users need to configure a merge strategy when using sbt-assembly, or they will get an error* like this:

[error] 1 error was encountered during merge
[error] stack trace is suppressed; run last assembly for the full output
[error] (assembly) deduplicate: different file contents found in the following:
[error] /Users/marcospereira/.ivy2/local/com.typesafe.akka/akka-stream-kafka_2.13/2.0.4+12-31cf63dd/jars/akka-stream-kafka_2.13.jar:version.conf
[error] /Users/marcospereira/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/com/typesafe/akka/akka-actor_2.13/2.5.30/akka-actor_2.13-2.5.30.jar:version.conf

But since we are using a distinct key for alpakka-kafka version, it is safe to concat the files, as you guessed.


* I'm not sure why sbt-assembly is not handling all filenames ending in .conf as configuration files. Maybe it is worth to send a PR there too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, merging *.conf files seems reasonable to me.

An alternative for now would be to create a file named akka-kafa-version.conf.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum, "for now" implying that it is going to change in the future, right? If so, I would not bother to have another name, and then move to version.conf later. Users should be able to configure what to do pretty easily, so I don't see why to have extra steps here. But also, I don't have a strong opinion on that. My only concern is to handle multiple version files in Cinnamon in the future.

Finally, I created a new issue in sbt-assembly: sbt/sbt-assembly#406. If they decide it make sense to support all .conf files out-of-the-box, I will submit a PR there, and users would only need to update sbt-assembly then.

WDYT?

|"""),
sourceGenerators += generateVersion(
sourceManaged,
_ / "akka" / "kafka" / "Version.scala",
"""|package akka.kafka
|
|object Version {
| val current: String = "%s"
|}
|"""
)
)
)

def generateVersion(dir: SettingKey[File], locate: File => File, template: String) = Def.task[Seq[File]] {
val file = locate(dir.value)
val content = template.stripMargin.format(version.value)
if (!file.exists || IO.read(file) != content) IO.write(file, content)
Seq(file)
}

}