diff --git a/README.md b/README.md index d1eb577..76204c7 100644 --- a/README.md +++ b/README.md @@ -263,7 +263,6 @@ dependencies { xjcGenerate { episodes compileClasspath // compileClasspath includes all "implementation" dependencies - } ``` @@ -283,10 +282,14 @@ And then reference it in the importing schema: schemaLocation="http://schemas.example.com/my-model.xsd" /> ``` -By default, all JARs in the - `compileClasspath` configuration are taken into account, but you can use a custom configuration by setting the - `catalogResolutionClasspath` property on the `XjcGenerate` task. +By default, all JARs in the special configuration `xjcCatalogResolution` are taken into account, which + inherits all dependencies from `compileClasspath` (unless `includeInMainCompilation` is set to `false` - + see below). You can add dependencies to this configuration, or use a custom configuration by setting + the `catalogResolutionClasspath` property on the `XjcGenerate` task. +When using catalogs together with a separate source set (i.e. setting `includeInMainCompilation` to false), +you will need to specify the catalog resolution dependencies manually. The reason for this is that usually your +`compileClasspath` will include the XJC-generated files itself, which would lead to a circular task dependency. The `maven:` scheme works similar to the `classpath:` scheme, but allows you to specify additional Maven coordinates to filter the dependency. The syntax is (without spaces) diff --git a/src/main/groovy/org/unbrokendome/gradle/plugins/xjc/XjcPlugin.groovy b/src/main/groovy/org/unbrokendome/gradle/plugins/xjc/XjcPlugin.groovy index 3129815..7753916 100644 --- a/src/main/groovy/org/unbrokendome/gradle/plugins/xjc/XjcPlugin.groovy +++ b/src/main/groovy/org/unbrokendome/gradle/plugins/xjc/XjcPlugin.groovy @@ -2,18 +2,21 @@ package org.unbrokendome.gradle.plugins.xjc import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.api.artifacts.Configuration import org.gradle.api.plugins.JavaPlugin import org.gradle.api.tasks.SourceSet import org.gradle.api.tasks.SourceSetContainer -import org.gradle.jvm.tasks.Jar +import org.gradle.api.tasks.bundling.Jar +@SuppressWarnings("GrMethodMayBeStatic") class XjcPlugin implements Plugin { - public static final String XJC_EXTENSION_NAME = 'xjc' - public static final String XJC_EPISODE_CONFIGURATION_NAME = 'xjcEpisode' - public static final String XJC_CLASSPATH_CONFIGURATION_NAME = 'xjcClasspath' - public static final String XJC_GENERATE_TASK_NAME = 'xjcGenerate' + static final String XJC_EXTENSION_NAME = 'xjc' + static final String XJC_EPISODE_CONFIGURATION_NAME = 'xjcEpisode' + static final String XJC_CLASSPATH_CONFIGURATION_NAME = 'xjcClasspath' + static final String XJC_CATALOG_RESOLUTION_CONFIGURATION_NAME = 'xjcCatalogResolution' + static final String XJC_GENERATE_TASK_NAME = 'xjcGenerate' @Override @@ -37,19 +40,24 @@ class XjcPlugin implements Plugin { xjcTask.source = project.fileTree('src/main/schema') { include '*.xsd' } xjcTask.bindingFiles = project.fileTree('src/main/schema') { include '*.xjb' } xjcTask.urlSources = project.fileTree('src/main/schema') { include '*.url' } - xjcTask.episodes = project.configurations.create XJC_EPISODE_CONFIGURATION_NAME - xjcTask.pluginClasspath = project.configurations.create XJC_CLASSPATH_CONFIGURATION_NAME + xjcTask.episodes = createInternalConfiguration(project, XJC_EPISODE_CONFIGURATION_NAME) + xjcTask.pluginClasspath = createInternalConfiguration(project, XJC_CLASSPATH_CONFIGURATION_NAME) + xjcTask.catalogResolutionClasspath = createInternalConfiguration(project, XJC_CATALOG_RESOLUTION_CONFIGURATION_NAME) xjcTask.conventionMapping.with { map('outputDirectory') { project.file("${project.buildDir}/xjc/generated-sources") } map('episodeTargetFile') { project.file("${project.buildDir}/xjc/sun-jaxb.episode") } - map('catalogResolutionClasspath') { - project.configurations.findByName('compileClasspath') ?: project.configurations.findByName('compile') - } } xjcTask } + private Configuration createInternalConfiguration(Project project, String name) { + project.configurations.create(name) { Configuration c -> + c.visible = false + } + } + + private void handleIncludeInMainCompilation(Project project, XjcGenerate xjcTask) { project.afterEvaluate { def xjcExtension = project.extensions.getByType XjcExtension @@ -61,6 +69,18 @@ class XjcPlugin implements Plugin { def compileJavaTask = project.tasks.getByName JavaPlugin.COMPILE_JAVA_TASK_NAME compileJavaTask.dependsOn xjcTask + + def catalogResolutionConfiguration = project.configurations.getByName XJC_CATALOG_RESOLUTION_CONFIGURATION_NAME + + // If the catalog resolution classpath hasn't been modified, make it extend from the compileClasspath + // (note that we can only do this if includeInMainCompilation is true, otherwise we would get a + // circular dependency - see issue #11) + if (catalogResolutionConfiguration.extendsFrom.empty && catalogResolutionConfiguration.dependencies.empty) { + //noinspection GrDeprecatedAPIUsage + catalogResolutionConfiguration.extendsFrom( + project.configurations.findByName('compileClasspath') + ?: project.configurations.findByName(JavaPlugin.COMPILE_CONFIGURATION_NAME)) + } } } }