Skip to content

Commit

Permalink
Extract XJC plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
florian-beetz committed Jul 5, 2018
1 parent 5b6c540 commit 8dce805
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 50 deletions.
61 changes: 53 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import groovy.json.JsonSlurper
import org.gradle.internal.os.OperatingSystem
import org.jabref.build.xjc.XjcTask

import static org.jabref.build.Dependencies.libraries

Expand Down Expand Up @@ -41,10 +42,10 @@ apply plugin: 'install4j'
apply plugin: 'me.champeau.gradle.jmh'
apply plugin: 'checkstyle'
apply plugin: org.jabref.build.antlr.AntlrPlugin
apply plugin: org.jabref.build.xjc.XjcPlugin

apply from: 'eclipse.gradle'
apply from: 'localization.gradle'
apply from: 'xjc.gradle'

group = "org.jabref"
version = "5.0-dev"
Expand Down Expand Up @@ -144,6 +145,7 @@ dependencies {
testCompile libraries.testFx

checkstyle libraries.checkstyle
xjc libraries.xjc
}

jacoco {
Expand Down Expand Up @@ -221,7 +223,12 @@ processResources {
}


task generateSource(dependsOn: ["generateBstGrammarSource", "generateSearchGrammarSource"]) {
task generateSource(dependsOn: ["generateBstGrammarSource",
"generateSearchGrammarSource",
"generateMedlineSource",
"generateBibtexmlSource",
"generateEndnoteSource",
"generateModsSource"]) {
group = 'JabRef'
description 'Generates all Java source files.'
}
Expand All @@ -245,14 +252,52 @@ task generateSearchGrammarSource(type: org.jabref.build.antlr.AntlrTask) {
javaPackage = "org.jabref.search"
}

compileJava {
options.encoding = 'UTF-8'
options.compilerArgs << "-Xlint:none"
task generateMedlineSource(type: XjcTask) {
group = 'JabRef'
description = "Generates java files for the medline importer."

schemaFile = "src/main/resources/xjc/medline/medline.xsd"
outputDirectory = "src/main/gen/"
javaPackage = "org.jabref.logic.importer.fileformat.medline"
}

task generateBibtexmlSource(type: XjcTask) {
group = 'JabRef'
description = "Generates java files for the bibtexml importer."

schemaFile = "src/main/resources/xjc/bibtexml/bibtexml.xsd"
outputDirectory = "src/main/gen"
javaPackage = "org.jabref.logic.importer.fileformat.bibtexml"
}

task generateEndnoteSource(type: XjcTask) {
group = 'JabRef'
description = "Generates java files for the endnote importer."

schemaFile = "src/main/resources/xjc/endnote/RSXML.dtd"
outputDirectory = "src/main/gen/"
javaPackage = "org.jabref.logic.importer.fileformat.endnote"
arguments = '-dtd'
}

task generateModsSource(type: XjcTask) {
group = 'JabRef'
description = "Generates java files for the mods importer."

schemaFile = "src/main/resources/xjc/mods/mods-3-7.xsd"
bindingFile = "src/main/resources/xjc/mods/mods-binding.xjb"
outputDirectory = "src/main/gen/"
javaPackage = "org.jabref.logic.importer.fileformat.mods"
arguments = '-npa'
}

tasks.withType(JavaCompile) {
options.encoding = 'UTF-8' // use UTF-8 for regular and test compilation
}
compileJava.dependsOn "generateSource"

compileTestJava {
options.encoding = 'UTF-8'
compileJava {
options.compilerArgs << "-Xlint:none"
dependsOn "generateSource"
}

javadoc {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ class Dependencies {
archUnit: "0.8.2",
testFx: "4.0.+",

checkstyle: "8.10.1"
checkstyle: "8.10.1",
xjc: "2.2.4-1",
]

static def libraries = [
Expand Down Expand Up @@ -171,6 +172,7 @@ class Dependencies {
],

checkstyle: "com.puppycrawl.tools:checkstyle:${versions.checkstyle}",
xjc: "com.sun.xml.bind:jaxb-xjc:${versions.xjc}",
]

}
20 changes: 20 additions & 0 deletions buildSrc/src/main/groovy/org/jabref/build/xjc/XjcPlugin.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.jabref.build.xjc

import org.gradle.api.Plugin
import org.gradle.api.Project

class XjcPlugin implements Plugin<Project> {

static final def CONFIGURATION_NAME = "xjc"

@Override
void apply(Project target) {
def configuration = target.configurations.create(CONFIGURATION_NAME)
configuration.description = "Dependencies needed to run the XJC tool."

target.afterEvaluate { evaluated ->
evaluated.logger.info(evaluated.configurations.xjc.asPath)
evaluated.ant.taskdef(name: 'xjc', classname: 'com.sun.tools.xjc.XJCTask', classpath: evaluated.configurations.getByName(CONFIGURATION_NAME).asPath)
}
}
}
93 changes: 93 additions & 0 deletions buildSrc/src/main/groovy/org/jabref/build/xjc/XjcTask.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package org.jabref.build.xjc

import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction

class XjcTask extends DefaultTask {

private def schemaFile
@Optional
private def bindingFile
private def outputDirectory
private String javaPackage
@Optional
private String arguments

@TaskAction
def generateClasses() {
project.mkdir(outputDirectory)
project.ant.xjc(destdir: outputDirectory, package: javaPackage) {
schema(dir: schemaFile.getParent(), includes: schemaFile.getName())
if (bindingFile != null) {
binding(dir: bindingFile.getParent(), includes: bindingFile.getName())
}
if (arguments != null) {
arg(value: arguments)
}
}
}

@Input
File getSchemaFile() {
if (schemaFile == null) {
return null
}
return project.file(schemaFile)
}

void setSchemaFile(Object schemaFile) {
this.schemaFile = schemaFile
}

File getOutputDirectory() {
if (outputDirectory == null) {
return null
}
return project.file(outputDirectory)
}

void setOutputDirectory(Object outputDirectory) {
this.outputDirectory = outputDirectory
updateOutput()
}

String getJavaPackage() {
return javaPackage
}

void setJavaPackage(String javaPackage) {
this.javaPackage = javaPackage
updateOutput()
}

String getArguments() {
return arguments
}

void setArguments(String args) {
this.arguments = args
}

@Input
File getBindingFile() {
if (bindingFile == null) {
return null
}
return project.file(bindingFile)
}

void setBindingFile(Object binding) {
this.bindingFile = binding
}

private void updateOutput() {
if (outputDirectory != null && javaPackage != null)
outputs.dir(new File(getOutputDirectory(), packageAsPath(javaPackage)))
}

private static String packageAsPath(String pkg) {
return pkg.replace((char) '.', File.separatorChar)
}
}
41 changes: 0 additions & 41 deletions xjc.gradle

This file was deleted.

0 comments on commit 8dce805

Please sign in to comment.