【问题标题】:schemagen gradle build errorschemagen gradle 构建错误
【发布时间】:2014-02-06 00:30:09
【问题描述】:

我正在尝试将现有的 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;
   }
  }

提前谢谢你。

【问题讨论】:

  • 这很奇怪。 exclude 被正确解析(如果拼写错误,则会出现错误),但在生成模式时会被忽略。如果您使用替代方案 excludesexcludesfile,也会发生同样的情况。这可能是一个错误吗?

标签: java ant gradle maven-3 schemagen


【解决方案1】:

我的类路径问题很少,通过以下更改解决。

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

【讨论】:

  • 我仍然遇到这个问题,完全相同的错误。我的依赖项是: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 也会生成
猜你喜欢
  • 2015-07-02
  • 2016-10-21
  • 2016-05-08
  • 2015-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多