Skip to content

Commit 9aa3fae

Browse files
committed
Allow dependency on project with avro scope
1 parent 8c1038d commit 9aa3fae

File tree

12 files changed

+191
-6
lines changed

12 files changed

+191
-6
lines changed

plugin/src/main/scala/com/github/sbt/avro/SbtAvro.scala

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ object SbtAvro extends AutoPlugin {
100100
// source generation
101101
avroGenerate / target := configSrcSub(avroGenerate / target).value,
102102
managedSourceDirectories += (avroGenerate / target).value,
103-
avroGenerate := sourceGeneratorTask(avroGenerate).dependsOn(avroUnpackDependencies).value,
103+
avroGenerate := sourceGeneratorTask(avroGenerate)
104+
.dependsOn(avroUnpackDependencies)
105+
.dependsOn(avroUnpackDependencies.?.all(filterDependsOn))
106+
.value,
104107
sourceGenerators += avroGenerate.taskValue,
105108
compile := compile.dependsOn(avroGenerate).value,
106109
// packaging
@@ -128,6 +131,13 @@ object SbtAvro extends AutoPlugin {
128131
inConfig(Avro)(Defaults.configSettings) ++
129132
Seq(Compile, Test).flatMap(c => inConfig(c)(configScopedSettings))
130133

134+
// This filter is meant evaluate for all dependant submodules
135+
// eg. source files / unpack dependencies
136+
private val filterDependsOn = ScopeFilter(
137+
inDependencies(ThisProject, transitive = false),
138+
inConfigurations(Compile)
139+
)
140+
131141
private def unpack(
132142
cacheBaseDirectory: File,
133143
deps: Seq[File],
@@ -191,7 +201,11 @@ object SbtAvro extends AutoPlugin {
191201
private def sourceGeneratorTask(key: TaskKey[Seq[File]]) = Def.task {
192202
val out = (key / streams).value
193203
val externalSrcDir = (avroUnpackDependencies / target).value
194-
val srcDirs = avroUnmanagedSourceDirectories.value :+ externalSrcDir
204+
val unmanagedSrcDirs = avroUnmanagedSourceDirectories.value
205+
val dependsOnDirs = (avroUnpackDependencies / target).?.all(filterDependsOn).value.flatten ++
206+
avroUnmanagedSourceDirectories.?.all(filterDependsOn).value.flatten.flatten
207+
val srcDirs = Seq(externalSrcDir) ++ unmanagedSrcDirs ++ dependsOnDirs
208+
195209
val outDir = (key / target).value
196210
implicit val conv: xsbti.FileConverter = fileConverter.value // used by PluginCompat
197211

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
val checkUnpacked = TaskKey[Unit]("checkUnpacked")
2+
val checkGenerated = TaskKey[Unit]("checkGenerated")
3+
4+
def exists(f: File): Unit = assert(f.exists(), s"$f does not exist")
5+
def absent(f: File): Unit = assert(!f.exists(), s"$f does exists")
6+
7+
lazy val commonSettings = Seq(
8+
organization := "com.github.sbt",
9+
scalaVersion := "2.13.15"
10+
)
11+
12+
lazy val avroOnlySettings = Seq(
13+
crossScalaVersions := Seq.empty,
14+
crossPaths := false,
15+
autoScalaLibrary := false,
16+
// only create avro jar
17+
Compile / packageAvro / publishArtifact := true,
18+
Compile / packageBin / publishArtifact := false,
19+
Compile / packageSrc / publishArtifact := false,
20+
Compile / packageDoc / publishArtifact := false,
21+
)
22+
23+
lazy val `external`: Project = project
24+
.in(file("external"))
25+
.enablePlugins(SbtAvro)
26+
.settings(commonSettings)
27+
.settings(avroOnlySettings)
28+
.settings(
29+
name := "external",
30+
version := "0.0.1-SNAPSHOT",
31+
)
32+
33+
lazy val `transitive`: Project = project
34+
.in(file("transitive"))
35+
.enablePlugins(SbtAvro)
36+
.settings(commonSettings)
37+
.settings(avroOnlySettings)
38+
.settings(
39+
name := "transitive",
40+
version := "0.0.1-SNAPSHOT",
41+
libraryDependencies ++= Seq(
42+
// when using avro scope, it won't be part of the pom dependencies -> intransitive
43+
// to declare transitive dependency use the compile scope
44+
"com.github.sbt" % "external" % "0.0.1-SNAPSHOT" classifier "avro"
45+
),
46+
Compile / avroDependencyIncludeFilter := artifactFilter(classifier = "avro"),
47+
// create a test jar with a schema as resource
48+
Test / packageBin / publishArtifact := true,
49+
)
50+
51+
lazy val root: Project = project
52+
.in(file("."))
53+
.enablePlugins(SbtAvro)
54+
.dependsOn(`transitive` % "avro->avro")
55+
.settings(commonSettings)
56+
.settings(
57+
name := "local-dependency",
58+
crossScalaVersions := Seq("2.13.15", "2.12.20"),
59+
libraryDependencies ++= Seq(
60+
"org.specs2" %% "specs2-core" % "4.20.9" % Test
61+
),
62+
// add additional avro source test jar
63+
// we unfortunatelly must recompile schemas from compile scope when test schema depends on it.
64+
Test / avroDependencyIncludeFilter := (Compile / avroDependencyIncludeFilter).value ||
65+
artifactFilter(name = "transitive", classifier = "tests"),
66+
67+
Compile / checkUnpacked := {
68+
exists((`transitive` / crossTarget).value / "src_managed" / "avro" / "main" / "com" / "github" / "sbt" / "avro" / "test" / "external" / "avdl.avdl")
69+
exists((`transitive` / crossTarget).value / "src_managed" / "avro" / "main" / "com" / "github" / "sbt" / "avro" / "test" / "external" / "avpr.avpr")
70+
exists((`transitive` / crossTarget).value / "src_managed" / "avro" / "main" / "com" / "github" / "sbt" / "avro" / "test" / "external" / "avsc.avsc")
71+
},
72+
Compile / checkGenerated := {
73+
exists(crossTarget.value / "src_managed" / "compiled_avro" / "main" / "com" / "github" / "sbt" / "avro" / "test" / "external" / "Avdl.java")
74+
exists(crossTarget.value / "src_managed" / "compiled_avro" / "main" / "com" / "github" / "sbt" / "avro" / "test" / "external" / "Avpr.java")
75+
exists(crossTarget.value / "src_managed" / "compiled_avro" / "main" / "com" / "github" / "sbt" / "avro" / "test" / "external" / "Avsc.java")
76+
exists(crossTarget.value / "src_managed" / "compiled_avro" / "main" / "com" / "github" / "sbt" / "avro" / "test" / "transitive" / "Avsc.java")
77+
}
78+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@namespace("com.github.sbt.avro.test.external")
2+
protocol ProtocolAvdl {
3+
record Avdl {
4+
string stringField;
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"namespace": "com.github.sbt.avro.test.external",
3+
"protocol": "ProtocolAvpr",
4+
"types": [
5+
{
6+
"name": "Avpr",
7+
"type": "record",
8+
"fields": [
9+
{
10+
"name": "stringField",
11+
"type": "string"
12+
}
13+
]
14+
}
15+
]
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "Avsc",
3+
"namespace": "com.github.sbt.avro.test.external",
4+
"type": "record",
5+
"fields": [
6+
{
7+
"name": "stringField",
8+
"type": "string"
9+
}
10+
]
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
sys.props.get("plugin.version") match {
2+
case Some(x) => addSbtPlugin("com.github.sbt" % "sbt-avro" % x)
3+
case _ => sys.error("""|The system property 'plugin.version' is not defined.
4+
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.github.sbt.avro.test
2+
3+
object Main extends App {
4+
5+
external.Avsc.newBuilder().setStringField("external").build()
6+
external.Avpr.newBuilder().setStringField("external").build()
7+
external.Avdl.newBuilder().setStringField("external").build()
8+
transitive.Avsc.newBuilder().setStringField("transitive").build()
9+
10+
println("success")
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.github.sbt.avro.test
2+
3+
import com.github.sbt.avro.test.transitive.Test
4+
5+
object AvroTest extends App {
6+
7+
Test.newBuilder().setStringField("external").build()
8+
9+
println("success")
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
> external/publishLocal
2+
3+
> avroGenerate
4+
> checkUnpacked
5+
> checkGenerated
6+
7+
> +compile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "Avsc",
3+
"namespace": "com.github.sbt.avro.test.transitive",
4+
"type": "record",
5+
"fields": [
6+
{
7+
"name": "stringField",
8+
"type": "string"
9+
},
10+
{
11+
"name": "referencedTypeField",
12+
"type": "com.github.sbt.avro.test.external.Avsc"
13+
}
14+
]
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "Test",
3+
"namespace": "com.github.sbt.avro.test.transitive",
4+
"type": "record",
5+
"fields": [
6+
{
7+
"name": "stringField",
8+
"type": "string"
9+
},
10+
{
11+
"name": "referencedTypeField",
12+
"type": "com.github.sbt.avro.test.external.Avsc"
13+
}
14+
]
15+
}

plugin/src/sbt-test/sbt-avro/publishing/build.sbt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ lazy val `transitive`: Project = project
4343
// to declare transitive dependency use the compile scope
4444
"com.github.sbt" % "external" % "0.0.1-SNAPSHOT" classifier "avro"
4545
),
46-
transitiveClassifiers += "avro",
4746
Compile / avroDependencyIncludeFilter := artifactFilter(classifier = "avro"),
4847
// create a test jar with a schema as resource
4948
Test / packageBin / publishArtifact := true,
@@ -62,9 +61,7 @@ lazy val root: Project = project
6261
"org.specs2" %% "specs2-core" % "4.20.9" % Test
6362
),
6463
// add additional avro source test jar
65-
// we unfortunatelly must recompile schemas from compile scope when test schema depends on it.
66-
Test / avroDependencyIncludeFilter := (Compile / avroDependencyIncludeFilter).value ||
67-
artifactFilter(name = "transitive", classifier = "tests"),
64+
Test / avroDependencyIncludeFilter := artifactFilter(name = "transitive", classifier = "tests"),
6865
// exclude specific avsc file
6966
Compile / avroUnpackDependencies / excludeFilter := (Compile / avroUnpackDependencies / excludeFilter).value || "exclude.avsc",
7067

0 commit comments

Comments
 (0)