I am trying to migrate existing maven project to gradle build. In the maven project i am using jaxb2-maven-plugin
to generate xsd(schemagen)/classes(xjc). I wanted to get the same functionality in gradle. I have few classes with jaxb annotation but some are not so how do i exclude the files which is not needed. When i do as below i get error.
Error:
:createschemaTargetDir UP-TO-DATE
:schemagen
[ant:schemagen] error: org.gradle.Person does not have a no-arg default constructor.
[ant:schemagen] this problem is related to the following location:
[ant:schemagen] at org.gradle.Person(Person.java:5)
[ant:schemagen] 1 error
:schemagen FAILED
FAILURE: Build failed with an exception.
* Where:
Build file 'E:\gradle-schemagen\build.gradle' line: 31
* What went wrong:
Execution failed for task ':schemagen'.
> schema generation failed
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = 1.6
version = '1.0'
schemaTargetDir = new File('build/generated-schema')
configurations {
jaxb
}
jar {
manifest {
attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version
}
}
repositories {
mavenCentral()
}
task createschemaTargetDir () {
schemaTargetDir.mkdirs()
}
task schemagen (dependsOn: createschemaTargetDir) {
doLast {
ant.taskdef(name: 'schemagen', classname: 'com.sun.tools.jxc.SchemaGenTask', classpath: configurations.jaxb.asPath)
ant.schemagen(srcdir: new File('src/main/java'), destdir: schemaTargetDir, includeAntRuntime:'false') {
exclude (name:'Person.java')
}
}
}
test {
systemProperties 'property': 'value'
}
uploadArchives {
repositories {
flatDir {
dirs 'repos'
}
}
}
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'
jaxb group: 'com.sun.xml.bind', name: 'jaxb-xjc', version: '2.1.6'
}
Book.java
package org.gradle;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "book")
// If you want you can define the order in which the fields are written
// Optional
@XmlType(propOrder = { "author", "name", "publisher", "isbn" })
public class Book {
private String name;
private String author;
private String publisher;
private String isbn;
// If you like the variable name, e.g. "name", you can easily change this
// name for your XML-Output:
@XmlElement(name = "title")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
}
Person.java
package org.gradle;
import org.apache.commons.collections.list.GrowthList;
public class Person {
private final String name;
public Person(String name) {
this.name = name;
new GrowthList();
}
public String getName() {
return name;
}
}
Thank you in advance.
I had few classpath issue, which was solved by below changes.
ant.schemagen(srcdir: new File('src/main/java'), destdir: schemaTargetDir, includeAntRuntime:'false') {
classpath {
pathElement(path: configurations.jaxb.asPath )
}
}
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'
jaxb (
'com.sun.xml.bind:jaxb-xjc:2.1.6',
'org.apache.avro:avro-tools:1.7.5',
'org.apache.avro:avro:1.7.5'
)
}