schemagen gradle构建错误

时间:2014-02-06 00:30:09

标签: java ant gradle maven-3 schemagen

我正在尝试将现有的maven项目迁移到gradle build。在maven项目中,我使用jaxb2-maven-plugin生成xsd(schemagen)/ classes(xjc)。我想在gradle中获得相同的功能。我有几个类与jaxb注释,但有些不是这样我如何排除不需要的文件。当我这样做时,我得到错误。

错误:

: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;
   }
  }

提前谢谢。

1 个答案:

答案 0 :(得分:0)

我有几个类路径问题,这是通过以下更改解决的。

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' 
          ) 
}