2014-02-06 43 views
2

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

错误:

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

在此先感谢您。

+0

这很奇怪。正确解析'exclude'(如果拼写错误,则会出错),但在生成模式时会被忽略。如果您使用替代选项'excludedes'或'excludesfile',则会发生同样的情况。这可能是一个错误? – joergl

回答

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

我仍然遇到这个问题,完全相同的错误。 我的依赖关系是: xjc'com.sun.xml.bind:jaxb-xjc:2.2.7-b41' xjc'com.sun.xml.bind:jaxb-impl:2.2.7-b41' xjc' javax.xml.bind:jaxb-api:2.2.7' - 奇怪的是,即使我收到错误消息,也会生成xsd –