Skip to content

Commit

Permalink
Fix issue with binding files, logging and more
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornvester committed Jun 18, 2023
1 parent 69eb26f commit bb6e726
Show file tree
Hide file tree
Showing 16 changed files with 177 additions and 111 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# IntelliJ IDEA
# Everything but the codeStyle folder should be ignored
/.idea/*
/.idea/modules/
/**/out/
.idea/
!/.idea/codeStyles/

# Gradle
/**/.gradle/
Expand Down
70 changes: 39 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ e.g. like this:

```kotlin
plugins {
id("com.github.bjornvester.xjc") version "1.7.1"
id("com.github.bjornvester.xjc") version "1.8.0"
}
```

Expand All @@ -39,21 +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. |
| xsdFiles | FileCollection | xsdDir<br>&nbsp;&nbsp;.asFileTree<br>&nbsp;&nbsp;.matching { include("**/*.xsd") } | The schemas to compile.<br>If empty, all files in the xsdDir will be compiled. |
| 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`. |
| bindingFiles | FileCollection | \[empty\] | The binding files to use in the schema compiler |
| 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 | \[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`. |

### Choosing which schemas to generate source code for

Expand All @@ -66,15 +67,17 @@ xjc {
}
```

If you don't want to compile all schemas in the folder, you can specify which ones through the xsdFiles property.
Note that they must still be located under the directory specified by xsdDir, or up-to-date checking might not work properly.
All referenced resources must be located under this directory, or up-to-date checking might not work properly.

If you don't want to compile all schemas in the folder, you can specify which ones through the `includes` and `excludes` properties.
These are both Ant patterns.
Here is an example where all schema files are referenced relative to the xsdDir property:

```kotlin
xjc {
xsdFiles = project.files(
xsdDir.file("MySchema1.xsd"),
xsdDir.file("MySchema2.xsd")
xsdDir.file("MySchema1.xsd"),
xsdDir.file("MySchema2.xsd")
)
// Or
xsdFiles = xsdDir.asFileTree.matching { include("subfolder/**/*.xsd") }
Expand All @@ -93,7 +96,7 @@ xjc {
}
```

If you specify a version in the 2.x range, which generates source code with the javax namespace, you should also set the `useJakarta` configration to false.
If you specify a version in the 2.x range, which generates source code with the javax namespace, you should also set the `useJakarta` configuration to false.
Note that setting `useJakarta` to false, it will by default select an appropriate version in the 2.x range.

By applying the plugin, it will register the Java plugin as well if it isn't there already (so the generated source code can be compiled).
Expand Down Expand Up @@ -171,7 +174,9 @@ You can also provide your own binding files (or custom episode files) through th

```kotlin
xjc {
bindingFiles = project.files("$projectDir/src/main/bindings/mybinding.xjb")
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)
}
```

Expand Down Expand Up @@ -232,16 +237,20 @@ Then create a binding file with content with the types you like to map:
</bindings>
```

In the above, for `javax`, use version 1.2.0 and the binding attributes to `xmlns="http://java.sun.com/xml/ns/jaxb" version="2.1"`.
The above configuration is for `jakarta`.
For `javax`, use version 1.2.0 and the binding attributes to `xmlns="http://java.sun.com/xml/ns/jaxb" version="2.1"`.

Lastly, configure XJC to use the binding file (in this case it is called `src/main/bindings/bindings.xml`):
Lastly, for both `jakarta` and `javax`, configure the plugin to use the binding file (in this case it is called `src/main/bindings/bindings.xml`):

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

Note that some use `.xml` as the extension for the binding files, and some use `.xjb`.
This makes no difference to XJC and you can choose whatever you like.

## Generate resources with different configurations

_The grouping functionality described here requires Gradle 7.0 or higher_
Expand All @@ -259,11 +268,11 @@ xjc {

groups {
register("group1") {
xsdFiles = files(xsdDir.file("schema1.xsd"))
includes.set(listOf("MySchemaWithFunnyChar.xsd"))
defaultPackage.set("com.example.group1")
}
register("group2") {
xsdFiles = files(xsdDir.file("schema2.xsd"))
includes.set(listOf("MySchemaWithFunnyChar.xsd"))
defaultPackage.set("com.example.group2")
}
}
Expand All @@ -283,12 +292,11 @@ Here are some of the features I like to implement at some point.
* Support for catalog files.
* Support for schemas in wsdl files.
* Support for optionally adding "if-exists" attributes to generated episode files to prevent failures on unreferenced schemas.
* Document how to use the XJC task directly (for having multiple XJC tasks in the same Gradle project)
* Document how to register multiple XJC tasks yourself in the same project (for instance for testing only)

You are welcome to create issues and PRs for anything else.

## Alternatives

If you need additional functionality than what is provided here, you may want to try the one
from [unbroken-dome](https://github.com/unbroken-dome/gradle-xjc-plugin).
It was created after this project, so I haven't tried it myself. It appears to be of high quality.
If you need additional functionality than what is provided here, you may want to try the one from [unbroken-dome](https://github.com/unbroken-dome/gradle-xjc-plugin).
Just be aware that, at least at the time of this writing, the latest version is 2.0.0, which has some known issues with the jakarta dependencies and the Gradle configuration cache.
12 changes: 4 additions & 8 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = "com.github.bjornvester"
version = "1.7.1"
version = "1.8.0"

repositories {
mavenCentral()
Expand Down Expand Up @@ -45,13 +45,9 @@ gradlePlugin {
displayName = "Gradle XJC plugin"
tags.set(listOf("xjc", "jaxb", "xsd"))
description = """Changes:
|- Fixed a problem with a missing runtime dependency.
|Changelog from 1.7.0:
| - Added support for the Gradle configuration cache.
| - It now defaults to XJC 3.x, generating source code using the jakarta namespace. This can be changed back to using javax with a configuration change.
| - Minor bump of default versions.
| - Added configurations "useJakarta" and "addCompilationDependencies".
| - Minimum required of version of Gradle is now 6.7 (up from 6.0).""".trimMargin()
|- Fixed an issue with specifying binding files.
|- Fixed an issue with missing logging output from XJC.
|- Removed the xsdFiles configuration and added an includes and excludes configuration.""".trimMargin()
}
}
}
1 change: 1 addition & 0 deletions integration-test/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ includeBuild("..")
include(
"test-producer-2x",
"test-producer-3x",
"test-producer-3x-binding-files",
"test-producer-3x-grouped",
"test-consumer-2x",
"test-consumer-3x"
Expand Down
2 changes: 1 addition & 1 deletion integration-test/test-producer-2x/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ repositories {

xjc {
generateEpisode.set(true)
xsdFiles = files(xsdDir.file("MySchemaWithFunnyChar.xsd"))
includes.set(listOf("MySchemaWithFunnyChar.xsd"))
useJakarta.set(false)
}
22 changes: 22 additions & 0 deletions integration-test/test-producer-3x-binding-files/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
plugins {
id("com.github.bjornvester.xjc")
id("com.github.bjornvester.xjc.internal.java-conventions")
}

repositories {
mavenCentral()
}

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

dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.1")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.1")
}

tasks.test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings
xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="3.0">

<jaxb:bindings schemaLocation="../xsd/MySchemaWithSimpleType.xsd" node="//xs:schema">
<jaxb:schemaBindings>
<jaxb:package name="com.github.bjornvester.custom"/>
</jaxb:schemaBindings>
</jaxb:bindings>

</jaxb:bindings>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tsk="http://github.com/bjornvester/producer"
targetNamespace="http://github.com/bjornvester/producer"
elementFormDefault="qualified"
version="1.0">
<xs:element name="MyElement">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="tsk:MyDateType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="MyDateType">
<xs:restriction base="xs:dateTime"/>
</xs:simpleType>
</xs:schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.bjornvester.xjc;

import com.github.bjornvester.custom.ObjectFactory;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class XjcTest {
@Test
void testXjc() {
Assertions.assertEquals(new ObjectFactory().createMyElement().getClass().getPackage().getName(), "com.github.bjornvester.custom");
}
}
4 changes: 2 additions & 2 deletions integration-test/test-producer-3x-grouped/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ tasks.test {
xjc {
groups {
register("group1") {
xsdFiles = files(xsdDir.file("MySchemaWithFunnyChar.xsd"))
includes.set(listOf("MySchemaWithFunnyChar.xsd"))
defaultPackage.set("com.example.group1")
}
register("group2") {
xsdFiles = files(xsdDir.file("MySchemaWithFunnyChar.xsd"))
includes.set(listOf("MySchemaWithFunnyChar.xsd"))
defaultPackage.set("com.example.group2")
}
}
Expand Down
2 changes: 1 addition & 1 deletion integration-test/test-producer-3x/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ repositories {

xjc {
generateEpisode.set(true)
xsdFiles = files(xsdDir.file("MySchemaWithFunnyChar.xsd"))
includes.set(listOf("MySchemaWithFunnyChar.xsd"))
}
Loading

0 comments on commit bb6e726

Please sign in to comment.