Skip to content

Commit

Permalink
Replace 'wsdlFiles' with 'includes' to make it more clear that files …
Browse files Browse the repository at this point in the history
…need to be in the 'wsdlDir' directory
  • Loading branch information
bjornvester committed May 16, 2021
1 parent 4a33e56 commit 7195b3d
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 70 deletions.
41 changes: 22 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ A Gradle plugin for generating Java classes from WSDL files through CXF.
* It supports the Gradle build cache (enabled by setting "org.gradle.caching=true" in your gradle.properties file).
* It supports project relocation for the build cache (e.g. you move your project to a new path, or make a new copy/clone of it).
This is especially useful in a CI context, where you might clone PRs and/or branches for a repository in their own locations.
* It supports parallel execution (enabled with "org.gradle.parallel=true", possibly along with "org.gradle.priority=low", in your gradle.properties file).
* It supports parallel execution (enabled with "org.gradle.parallel=true" in your gradle.properties file).

## Configuration
Apply the plugin ID "com.github.bjornvester.wsdl2java" as specific in the [Gradle Plugin portal page](https://plugins.gradle.org/plugin/com.github.bjornvester.wsdl2java), e.g. like this:
Expand All @@ -21,7 +21,7 @@ plugins {
Put your WSDL and referenced XSD files somewhere in your src/main/resource directory.
By default, the plugin will create Java classes for all the WSDL files in the resource directory.

The generated code will by default end up in the directory build/generated/wsdl2java folder.
The generated code will by default end up in the directory build/generated/sources/wsdl2java folder.

You can configure the plugin using the "wsdl2java" extension like this:

Expand All @@ -33,17 +33,17 @@ wsdl2java {

Here is a list of all available properties:

| Property | Type | Default | Description |
|-----------------------|-----------------------|--------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------|
| wsdlDir | DirectoryProperty | "$projectDir/src<br>/main/resources" | The directory holding the xsd files to compile. |
| wsdlFiles | FileCollection | wsdlDir<br>&nbsp;&nbsp;.asFileTree<br>&nbsp;&nbsp;.matching { include("**/*.wsdl") } | The schemas to compile.<br>If empty, all files in the xsdDir will be compiled. |
| generatedSourceDir | DirectoryProperty | "$buildDir/generated<br>/sources/wsdl2java/java" | The output directory for the generated Java sources.<br>Note that it will be deleted when running XJC. |
| bindingFile | RegularFileProperty | \[not set\] | A binding file to use in the schema compiler |
| cxfVersion | Provider\<String> | "3.4.3" | The version of CXF to use. |
| verbose | Provider\<Boolean> | true | Enables verbose output from CXF. |
| suppressGeneratedDate | Provider\<Boolean> | true | Suppresses generating dates in CXF. Default is true to enable reproducible builds and to work with the build cache. |
| markGenerated | Provider\<Boolean> | "no" | Adds the @Generated annotation to the generated sources. See below for details as there are some gotchas with this. | |
| options | ListProperty\<String> | \[empty\] | Additional options to pass to the tool. See [here](https://cxf.apache.org/docs/wsdl-to-java.html) for details. |
| Property | Type | Default | Description |
|-----------------------|-----------------------|--------------------------------------------------|----------------------------------------------------------------------------------------------------------------------|
| wsdlDir | DirectoryProperty | "$projectDir/src<br>/main/resources" | The directory holding the WSDL and referenced XSD files to compile. |
| includes | ListProperty\<String> | \["**/*.wsdl"] | The inclusion filer (Ant style) for which WSDLs to include |
| generatedSourceDir | DirectoryProperty | "$buildDir/generated<br>/sources/wsdl2java/java" | The output directory for the generated Java sources.<br>Note that it will be deleted when running XJC. |
| bindingFile | RegularFileProperty | \[not set\] | A binding file to use in the schema compiler |
| cxfVersion | Provider\<String> | "3.4.3" | The version of CXF to use. |
| verbose | Provider\<Boolean> | true | Enables verbose output from CXF. |
| suppressGeneratedDate | Provider\<Boolean> | true | Suppresses generating dates in CXF. Default is true to support reproducible builds and to work with the build cache. |
| markGenerated | Provider\<Boolean> | "no" | Adds the @Generated annotation to the generated sources. See below for details as there are some gotchas with this. | |
| options | ListProperty\<String> | \[empty\] | Additional options to pass to the tool. See [here](https://cxf.apache.org/docs/wsdl-to-java.html) for details. |


### Configure the CXF version
Expand All @@ -61,7 +61,7 @@ You can optionally specify WSDL and generated source directories like this:
```kotlin
wsdl2java {
generatedSourceDir.set(layout.projectDirectory.file("src/generated/wsdl2java"))
wsdlDir.set(layout.projectDirectory.dir("src/main/resources"))
wsdlDir.set(layout.projectDirectory.dir("src/main/wsdl"))
}
```

Expand All @@ -70,13 +70,16 @@ Note that the `generatedSourceDir` will be wiped completely on each run, so don'
The `wsdlDir` is only used for up-to-date checking. It must contain all resources used by the WSDLs (e.g. included XSDs as well).

By default, the plugin will find all WSDL files in the `wsdlDir` directory.
To specify other files, you can use the `wsdlFiles` property.
This is a `ConfigurableFileCollection` and can be configured like this:
To specify other files, you can use the `includes` property.
Example:

```kotlin
wsdlFiles.setFrom(
"src/main/resources/HelloWorldAbstractService.wsdl",
"src/main/resources/nested/HelloWorldNestedService.wsdl"
wsdlDir.set(layout.projectDirectory.file("src/main/wsdl")) // Only if different from the default 'src/main/resources'
wsdlFiles.set(
listOf( // Kotlin method. For the Groovy DSL, use ["one.wsdl", "two.wsdl"] instead
"src/main/wsdls/MyFirstService.wsdl",
"src/main/wsdls/MySecondService.wsdl"
)
)
```

Expand Down
9 changes: 6 additions & 3 deletions integration-test/filter-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ plugins {
}

wsdl2java {
wsdlFiles.setFrom(
"src/main/resources/HelloWorldAbstractService.wsdl",
"src/main/resources/nested/HelloWorldNestedService.wsdl"
wsdlDir.set(layout.projectDirectory.dir("src/main/wsdl"))
includes.set(
listOf(
"HelloWorldAbstractService.wsdl",
"nested/HelloWorldNestedService.wsdl"
)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.bjornvester.wsdl2java;

import com.github.bjornvester.example._abstract.HelloWorldAbstract;
import com.github.bjornvester.example._abstract.SayHi;
import com.github.bjornvester.example._abstract.SayHiResponse;

public class HelloWorldAbstractImpl implements HelloWorldAbstract {
@Override
public SayHiResponse sayHi(SayHi request) {
SayHiResponse response = new SayHiResponse();
response.setReturn("Hi, " + request.getArg0());
return response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.bjornvester.wsdl2java;

import com.github.bjornvester.example.nested.HelloWorldNested;
import com.github.bjornvester.example.nested.SayHi;
import com.github.bjornvester.example.nested.SayHiResponse;

public class HelloWorldNestedImpl implements HelloWorldNested {
@Override
public SayHiResponse sayHi(SayHi request) {
SayHiResponse response = new SayHiResponse();
response.setReturn("Hi, " + request.getArg0());
return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import org.gradle.api.tasks.SourceSet.MAIN_SOURCE_SET_NAME
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.util.GradleVersion


@Suppress("unused")
class Wsdl2JavaPlugin : Plugin<Project> {
companion object {
const val MINIMUM_GRADLE_VERSION = "6.0"
Expand Down Expand Up @@ -53,9 +51,10 @@ class Wsdl2JavaPlugin : Plugin<Project> {

private fun verifyGradleVersion() {
if (GradleVersion.current() < GradleVersion.version(MINIMUM_GRADLE_VERSION)) {
throw UnsupportedOperationException("Plugin $PLUGIN_ID requires at least Gradle $MINIMUM_GRADLE_VERSION, " +
"but you are using ${GradleVersion.current().version}")
throw UnsupportedOperationException(
"Plugin $PLUGIN_ID requires at least Gradle $MINIMUM_GRADLE_VERSION, " +
"but you are using ${GradleVersion.current().version}"
)
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import javax.inject.Inject

open class Wsdl2JavaPluginExtension @Inject constructor(objects: ObjectFactory, layout: ProjectLayout) {
val wsdlDir = objects.directoryProperty().convention(layout.projectDirectory.dir("src/main/resources"))
val wsdlFiles = objects.fileCollection().from(wsdlDir.asFileTree.matching { include("**/*.wsdl") })
val includes = objects.listProperty(String::class.java).convention(listOf("**/*.wsdl"))
val bindingFile = objects.fileProperty()
val generatedSourceDir = objects.directoryProperty().convention(layout.buildDirectory.dir("generated/sources/wsdl2java"))
val cxfVersion = objects.property(String::class.java).convention("3.4.3")
Expand All @@ -16,8 +16,13 @@ open class Wsdl2JavaPluginExtension @Inject constructor(objects: ObjectFactory,
val markGenerated = objects.property(String::class.java).convention(MARK_GENERATED_NO)

companion object {
@JvmStatic val MARK_GENERATED_NO = "no"
@JvmStatic val MARK_GENERATED_YES_JDK8 = "yes-jdk8"
@JvmStatic val MARK_GENERATED_YES_JDK9 = "yes-jdk9"
@JvmStatic
val MARK_GENERATED_NO = "no"

@JvmStatic
val MARK_GENERATED_YES_JDK8 = "yes-jdk8"

@JvmStatic
val MARK_GENERATED_YES_JDK9 = "yes-jdk9"
}
}
60 changes: 32 additions & 28 deletions src/main/kotlin/com/github/bjornvester/wsdl2java/Wsdl2JavaTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ open class Wsdl2JavaTask @Inject constructor(
@get:PathSensitive(PathSensitivity.RELATIVE)
val wsdlInputDir = objects.directoryProperty().convention(getWsdl2JavaExtension().wsdlDir)

@get:InputFiles
@get:PathSensitive(PathSensitivity.RELATIVE)
@Optional
val wsdlFiles = objects.fileCollection().from(getWsdl2JavaExtension().wsdlFiles)
@get:Input
val includes = objects.listProperty(String::class.java).convention(getWsdl2JavaExtension().includes)

@get:InputFile
@get:PathSensitive(PathSensitivity.RELATIVE)
Expand Down Expand Up @@ -71,34 +69,40 @@ open class Wsdl2JavaTask @Inject constructor(
}

val defaultArgs = buildDefaultArguments()

wsdlFiles.forEach { wsdlFile ->
val computedArgs = mutableListOf<String>()
computedArgs.addAll(defaultArgs)

if (!computedArgs.contains("-wsdlLocation")) {
computedArgs.addAll(
listOf(
"-wsdlLocation",
wsdlFile.relativeTo(wsdlInputDir.asFile.get()).invariantSeparatorsPath
val wsdlToArgs = mutableMapOf<String, List<String>>()

wsdlInputDir
.asFileTree
.matching { include(this@Wsdl2JavaTask.includes.get()) }
.forEach { wsdlFile ->
val computedArgs = mutableListOf<String>()
computedArgs.addAll(defaultArgs)

if (!computedArgs.contains("-wsdlLocation")) {
computedArgs.addAll(
listOf(
"-wsdlLocation",
wsdlFile.relativeTo(wsdlInputDir.asFile.get()).invariantSeparatorsPath
)
)
)
}
}

computedArgs.add(wsdlFile.path)

workerExecutor.submit(Wsdl2JavaWorker::class.java) {
args = computedArgs.toTypedArray()
outputDir = sourcesOutputDir.get()
switchGeneratedAnnotation = (markGenerated.get() == MARK_GENERATED_YES_JDK9)
removeDateFromGeneratedAnnotation =
(markGenerated.get() in listOf(
MARK_GENERATED_YES_JDK8,
MARK_GENERATED_YES_JDK9
)) && suppressGeneratedDate.get()
computedArgs.add(wsdlFile.path)
wsdlToArgs[wsdlFile.path] = computedArgs
}
}

// Note that we don't run the CXF tool on each WSDL file as the build might be configured with parallel execution
// This could be a problem if multiple WSDLs references the same schemas as CXF might try and write to the same files
workerExecutor.submit(Wsdl2JavaWorker::class.java) {
this.wsdlToArgs = wsdlToArgs
outputDir = sourcesOutputDir.get()
switchGeneratedAnnotation = (markGenerated.get() == MARK_GENERATED_YES_JDK9)
removeDateFromGeneratedAnnotation =
(markGenerated.get() in listOf(
MARK_GENERATED_YES_JDK8,
MARK_GENERATED_YES_JDK9
)) && suppressGeneratedDate.get()
}
}

private fun buildDefaultArguments(): MutableList<String> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ abstract class Wsdl2JavaWorker : WorkAction<Wsdl2JavaWorkerParams> {
private val logger: Logger = LoggerFactory.getLogger(Wsdl2JavaWorker::class.java)

override fun execute() {
logger.info("Running WSDLToJava tool with args: {}", parameters.args)

try {
WSDLToJava(parameters.args).run(ToolContext())
} catch (e: Exception) {
// We can't propagate the exception as it might contain classes from CXF which are not available outside the worker execution context

logger.error("Failed to generate sources from WSDL", e)
throw GradleException("Failed to generate Java sources from WSDL. See the log for details.")
parameters.wsdlToArgs.forEach { (wsdlPath, args) ->
logger.info("Running WSDLToJava tool on file {] with args: {}", wsdlPath, args)

try {
WSDLToJava(args.toTypedArray()).run(ToolContext())
} catch (e: Exception) {
// We can't propagate the exception as it might contain classes from CXF which are not available outside the worker execution context
// Also, for some reason, we can't even put the message from the original exception in as it has shown to cause problems with serialization (though it is just a string)
logger.error("Failed to generate sources from WSDL", e)
throw GradleException("Failed to generate Java sources from WSDL. See the log for details.")
}
}

fixGeneratedAnnotations()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.gradle.api.file.Directory
import org.gradle.workers.WorkParameters

interface Wsdl2JavaWorkerParams : WorkParameters {
var args: Array<String>
var wsdlToArgs: Map<String, List<String>>
var outputDir: Directory
var switchGeneratedAnnotation: Boolean
var removeDateFromGeneratedAnnotation: Boolean
Expand Down

0 comments on commit 7195b3d

Please sign in to comment.