Skip to content

Commit

Permalink
Merge pull request #12 from unbroken-dome/fix-circular-dependency-on-…
Browse files Browse the repository at this point in the history
…catalog-resolution

Fix circular dependency on catalog resolution
  • Loading branch information
tkrullmann authored Nov 23, 2018
2 parents d5e165f + 899afb8 commit 6050a4d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ dependencies {
xjcGenerate {
episodes compileClasspath // compileClasspath includes all "implementation" dependencies
}
```

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Project> {

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
Expand All @@ -37,19 +40,24 @@ class XjcPlugin implements Plugin<Project> {
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
Expand All @@ -61,6 +69,18 @@ class XjcPlugin implements Plugin<Project> {

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))
}
}
}
}
Expand Down

0 comments on commit 6050a4d

Please sign in to comment.