Skip to content

Commit

Permalink
Fix episode consumption and change bindingFiles to a fileCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornvester committed Jun 18, 2023
1 parent 2c54594 commit 58f2d14
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 31 deletions.
41 changes: 20 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,22 @@ xjc {

Here is a list of all available properties:

| Property | Type | Default | Description |
|----------------------------|----------------------------|-------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| xsdDir | DirectoryProperty | "$projectDir/src<br>/main/resources" | The directory holding the xsd files to compile. |
| includes | ListProperty\<String> | \[empty\] | An optional include pattern for the files in the xsdDir property |
| excludes | ListProperty\<String> | \[empty\] | An optional exclude pattern for the files in the xsdDir property |
| bindingFiles | FileCollection | \[empty\] | The binding files to use in the schema compiler |
| outputJavaDir | DirectoryProperty | "$buildDir/generated<br>/sources/xjc/java" | The output directory for the generated Java sources.<br>Note that it will be deleted when running XJC. |
| outputResourcesDir | DirectoryProperty | "$buildDir/generated<br>/sources/xjc/resources" | The output directory for the generated resources (if any).<br>Note that it will be deleted when running XJC. |
| useJakarta | Provider\<Boolean> | true | Set to use the `jakarta` namespace. If false, uses the `javax` namespace. This value determines the default version of XJC and the JAXB binding provider. |
| xjcVersion | Provider\<String> | "3.0.2" for jakarta / "2.3.8" for javax | The version of XJC to use. |
| defaultPackage | Provider\<String> | \[not set\] | The default package for the generated Java classes.<br>If empty, XJC will infer it from the namespace. |
| generateEpisode | Provider\<Boolean> | false | If true, generates an Episode file for the generated Java classes. |
| markGenerated | Provider\<Boolean> | false | If true, marks the generated code with the annotation `@javax.annotation.Generated`. |
| options | ListProperty\<String> | \[empty\] | Options to pass to either the XJC core, or to third party plugins in the `xjcPlugins` configuration |
| groups | NamedDomainObjectContainer | \[empty\] | Allows you to group a set of XSDs and generate sources with different configurations. Requires Gradle 7.0 or higher. See below for details. |
| addCompilationDependencies | Provider\<Boolean> | true | Adds dependencies to the `implementation` configuration for compiling the generated sources. These includes `jakarta.xml.bind:jakarta.xml.bind-api` and possibly `jakarta.annotation:jakarta.annotation-api`. |
| Property | Type | Default | Description |
|----------------------------|----------------------------|--------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| xsdDir | DirectoryProperty | "$projectDir/src<br>/main/resources" | The directory holding the xsd files to compile. |
| includes | ListProperty\<String> | \[empty\] | An optional include pattern for the files in the xsdDir property |
| excludes | ListProperty\<String> | \[empty\] | An optional exclude pattern for the files in the xsdDir property |
| bindingFiles | FileCollection | xsdDir.asFileTree.matching<br> { include("**/*.xjb") } | The binding files to use in the schema compiler |
| outputJavaDir | DirectoryProperty | "$buildDir/generated<br>/sources/xjc/java" | The output directory for the generated Java sources.<br>Note that it will be deleted when running XJC. |
| outputResourcesDir | DirectoryProperty | "$buildDir/generated<br>/sources/xjc/resources" | The output directory for the generated resources (if any).<br>Note that it will be deleted when running XJC. |
| useJakarta | Provider\<Boolean> | true | Set to use the `jakarta` namespace. If false, uses the `javax` namespace. This value determines the default version of XJC and the JAXB binding provider. |
| xjcVersion | Provider\<String> | "3.0.2" for jakarta /<br> "2.3.8" for javax | The version of XJC to use. |
| defaultPackage | Provider\<String> | \[not set\] | The default package for the generated Java classes.<br>If empty, XJC will infer it from the namespace. |
| generateEpisode | Provider\<Boolean> | false | If true, generates an Episode file for the generated Java classes. |
| markGenerated | Provider\<Boolean> | false | If true, marks the generated code with the annotation `@javax.annotation.Generated`. |
| options | ListProperty\<String> | \[empty\] | Options to pass to either the XJC core, or to third party plugins in the `xjcPlugins` configuration |
| groups | NamedDomainObjectContainer | \[empty\] | Allows you to group a set of XSDs and generate sources with different configurations. Requires Gradle 7.0 or higher. See below for details. |
| addCompilationDependencies | Provider\<Boolean> | true | Adds dependencies to the `implementation` configuration for compiling the generated sources. These includes `jakarta.xml.bind:jakarta.xml.bind-api` and possibly `jakarta.annotation:jakarta.annotation-api`. |

### Choosing which schemas to generate source code for

Expand Down Expand Up @@ -158,13 +158,14 @@ The file will be generated at META-INF/sun-jaxb.episode and added as a resource
XJC can consume the episode files so that it is possible to compile java classes from a schema in one project, and consume it in XJC generators in other
projects, so you don't have to compile the same schemas multiple times.
To do this, you need to add the jar file to the configuration named "xjcBindings".
This is done using normal Gradle dependency management.

For multi-projects, assuming the episode file is generated in a project called "test-producer", you can do this like this:

```kotlin
dependencies {
implementation(project(":test-producer"))
xjcBindings(project(":test-producer", "apiElements"))
xjcBindings(project(":test-producer"))
}
```

Expand All @@ -174,9 +175,7 @@ You can also provide your own binding files (or custom episode files) through th

```kotlin
xjc {
bindingFiles.from(layout.projectDirectory.dir("src/main/xjb")) // Everything in the src/main/xjb directory
// or
bindingFiles.from(xsdDir.asFileTree.matching { include('**/*.xjb') }) // Files with an .xjb extension in the xsdDir directory (defaulting to src/main/resources)
bindingFiles = layout.projectDirectory.dir("src/main/xjb").asFileTree.matching { include("**/*.xjb") } // Files with an .xjb extension in the "src/main/xjb" directory
}
```

Expand Down Expand Up @@ -244,7 +243,7 @@ Lastly, for both `jakarta` and `javax`, configure the plugin to use the binding

```kotlin
xjc {
bindingFiles.from(layout.projectDirectory.dir("src/main/xjb").asFileTree.matching { include("**/*.xjb") })
bindingFiles = layout.projectDirectory.dir("src/main/xjb").asFileTree.matching { include("**/*.xjb") }
}
```

Expand Down
2 changes: 1 addition & 1 deletion integration-test/test-consumer-2x/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dependencies {
implementation(project(":test-producer-2x"))
implementation("org.jvnet.jaxb2_commons:jaxb2-basics-runtime:1.11.1") // Though called "runtime", it is required at compile time...

xjcBindings(project(":test-producer-2x", "apiElements"))
xjcBindings(project(":test-producer-2x"))
xjcPlugins("org.jvnet.jaxb2_commons:jaxb2-basics:1.11.1")

testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.1")
Expand Down
2 changes: 2 additions & 0 deletions integration-test/test-consumer-3x/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ repositories {
}

dependencies {
xjcBindings(project(":test-producer-3x"))

implementation(project(":test-producer-3x"))
testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.1")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repositories {

xjc {
xsdDir.set(layout.projectDirectory.dir("src/main/xsd"))
bindingFiles.from(layout.projectDirectory.dir("src/main/xjb").asFileTree.matching { include("**/*.xjb") })
bindingFiles = layout.projectDirectory.dir("src/main/xjb").asFileTree.matching { include("**/*.xjb") }
}

dependencies {
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/com/github/bjornvester/xjc/XjcExtension.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.bjornvester.xjc

import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.file.FileCollection
import org.gradle.api.file.ProjectLayout
import org.gradle.api.model.ObjectFactory
import javax.inject.Inject
Expand All @@ -18,7 +19,7 @@ open class XjcExtension @Inject constructor(objects: ObjectFactory, layout: Proj
override val outputResourcesDir = objects.directoryProperty().convention(layout.buildDirectory.dir("generated/sources/xjc/resources"))
override val defaultPackage = objects.property(String::class.java)
override val generateEpisode = objects.property(Boolean::class.java).convention(false)
override val bindingFiles = objects.fileCollection()
override var bindingFiles: FileCollection = xsdDir.asFileTree.matching { include("**/*.xjb") }
override val options = objects.listProperty(String::class.java)
override val markGenerated = objects.property(Boolean::class.java).convention(false)

Expand All @@ -33,7 +34,7 @@ open class XjcExtension @Inject constructor(objects: ObjectFactory, layout: Proj
outputResourcesDir.convention(layout.buildDirectory.dir("generated/sources/xjc-$name/resources"))
defaultPackage.convention(this@XjcExtension.defaultPackage)
generateEpisode.convention(this@XjcExtension.generateEpisode)
bindingFiles.from(this@XjcExtension.bindingFiles)
bindingFiles = this@XjcExtension.bindingFiles
options.convention(this@XjcExtension.options)
markGenerated.convention(this@XjcExtension.markGenerated)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.bjornvester.xjc

import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.FileCollection
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property

Expand All @@ -14,7 +14,7 @@ interface XjcExtensionGroup {
val outputResourcesDir: DirectoryProperty
val defaultPackage: Property<String>
val generateEpisode: Property<Boolean>
val bindingFiles: ConfigurableFileCollection
var bindingFiles: FileCollection
val options: ListProperty<String>
val markGenerated: Property<Boolean>
}
2 changes: 1 addition & 1 deletion src/main/kotlin/com/github/bjornvester/xjc/XjcPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class XjcPlugin : Plugin<Project> {
xjcConfiguration.from(project.configurations.named(XJC_CONFIGURATION_NAME))
xjcBindConfiguration.from(project.configurations.named(XJC_BIND_CONFIGURATION_NAME))
xjcPluginsConfiguration.from(project.configurations.named(XJC_PLUGINS_CONFIGURATION_NAME))
bindingFiles.from(group.bindingFiles)
bindingFiles = group.bindingFiles

val sourceSets = project.properties["sourceSets"] as SourceSetContainer

Expand Down
8 changes: 5 additions & 3 deletions src/main/kotlin/com/github/bjornvester/xjc/XjcTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.bjornvester.xjc
import com.github.bjornvester.xjc.XjcPlugin.Companion.XJC_EXTENSION_NAME
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.file.ArchiveOperations
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.FileSystemOperations
import org.gradle.api.file.ProjectLayout
Expand All @@ -18,8 +19,9 @@ import javax.inject.Inject
open class XjcTask @Inject constructor(
private val workerExecutor: WorkerExecutor,
private val objectFactory: ObjectFactory,
private val archiveOperations: ArchiveOperations,
private val fileSystemOperations: FileSystemOperations,
projectLayout: ProjectLayout,
private val fileSystemOperations: FileSystemOperations
) : DefaultTask() {
@get:Optional
@get:Input
Expand Down Expand Up @@ -53,7 +55,7 @@ open class XjcTask @Inject constructor(
@Optional
@get:InputFiles
@get:PathSensitive(PathSensitivity.RELATIVE)
val bindingFiles = objectFactory.fileCollection().from(getXjcExtension().bindingFiles)
var bindingFiles = getXjcExtension().bindingFiles

@get:Input
val options: ListProperty<String> = objectFactory.listProperty(String::class.java).convention(getXjcExtension().options)
Expand Down Expand Up @@ -188,7 +190,7 @@ open class XjcTask @Inject constructor(

xjcBindConfiguration.forEach { bindJarFile ->
if (bindJarFile.extension == "jar") {
val episodeFiles = objectFactory.fileTree().from(bindJarFile).filter { it.name == "sun-jaxb.episode" }.files
val episodeFiles = archiveOperations.zipTree(bindJarFile).matching { include("**/sun-jaxb.episode") }.files
if (episodeFiles.isEmpty()) {
logger.warn("No episodes (sun-jaxb.episode) found in bind jar file ${bindJarFile.name}")
} else {
Expand Down

0 comments on commit 58f2d14

Please sign in to comment.