作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我为 jaxb/xjc 选择了 3 个 gradle 插件,但没有一个对我有用。
对于每种情况,我都将 maven 模型 xsd maven-4.0.0.xsd
https://maven.apache.org/xsd/maven-4.0.0.xsd在 src/main/xsd
. (这只是一个示例,任何其他 xsd 都可以)
http://github.com/stianh/gradle-jaxb-plugin
此插件在 src/<source set>/xsd/**/*.xsd
中查找 xsd 文件.
这是 build.gradle
:
apply plugin: 'jaxb'
dependencies {
jaxb 'com.sun.xml.bind:jaxb-xjc:2.2.4-1'
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'no.entitas.gradle.jaxb:gradle-jaxb-plugin:2.0'
}
}
错误:
* What went wrong:
A problem occurred evaluating root project 'test-gradle'.
> Failed to apply plugin [id 'jaxb']
> Could not find method add() for arguments [jaxb, no.entitas.gradle.jaxb.plugin.JaxbPlugin$_apply_closure1@62c72501] on configuration container.
https://github.com/hibernate/gradle-xjc-plugin
这是 build.gradle
:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.hibernate.build.gradle:gradle-xjc-plugin:1.0.2.Final'
}
}
apply plugin: 'org.hibernate.build.gradle.xjc'
dependencies {
xjc 'org.glassfish.jaxb:jaxb-xjc:2.2.11'
xjc 'org.jvnet.jaxb2_commons:jaxb2-basics:0.9.3'
xjc 'org.jvnet.jaxb2_commons:jaxb2-basics-ant:0.9.3'
}
xjc {
xjcTaskName = 'org.jvnet.jaxb2_commons.xjc.XJC2Task'
// access the schemas NamedDomainObjectContainer
schemas {
// and add a new SchemaDescriptor to it under the name 'cfg'
cfg {
// and now, configure the SchemaDescriptor
xsd = file( 'src/main/xsd/maven-4.0.0.xsd')
}
}
}
错误:
* What went wrong:
Neither path nor baseDir may be null or empty string. path='null' basedir='C:\Users\xxxxx\eclipse-workspace\test-gradle'
https://github.com/IntershopCommunicationsAG/jaxb-gradle-plugin
这是 build.gradle
:
plugins {
id 'java'
id 'com.intershop.gradle.jaxb' version '5.1.0'
}
jaxb {
javaGen {
posConfig {
packageName = 'org.apache.maven.model'
schema = file('src/main/xsd/maven-4.0.0.xsd')
}
}
}
repositories {
mavenCentral()
}
dependencies {
jaxb 'com.sun.xml.bind:jaxb-xjc:3.0.1'
jaxb 'com.sun.xml.bind:jaxb-jxc:3.0.1'
jaxb 'com.sun.xml.bind:jaxb-impl:3.0.1'
jaxb 'com.sun.xml.bind:jaxb-core:3.0.1'
jaxb 'org.glassfish.jaxb:jaxb-runtime:3.0.1'
jaxb 'jakarta.xml.bind:jakarta.xml.bind-api:3.0.1'
jaxb 'jakarta.activation:jakarta.activation-api:2.0.1'
}
错误:
* What went wrong:
An exception occurred applying plugin request [id: 'com.intershop.gradle.jaxb', version: '5.1.0']
> Failed to apply plugin [id 'com.intershop.gradle.jaxb']
> Could not generate a proxy class for class com.intershop.gradle.jaxb.extension.JaxbExtension.
编辑
命令:渐变构建
最佳答案
这是一个没有使用特定插件(只是java
)和v4的例子jaxb-xjc 的:
(配置期望 maven-4.0.0.xsd
在 src/main/resources
中)
plugins {
id 'java'
}
group 'com.example.xjc-demo'
archivesBaseName = "xjcdemo"
version '1.0.0-SNAPSHOT'
repositories {
mavenCentral()
}
def generated_dir = "src/main/java"
sourceSets {
generated {
java.srcDir "$generated_dir"
}
}
// JAXB configuration holds classpath for running the JAXB XJC compiler
configurations {
jaxb
}
dependencies {
jaxb "org.glassfish.jaxb:jaxb-xjc:4.0.1"
jaxb "org.glassfish.jaxb:jaxb-runtime:4.0.1"
// The next two dependencies are not necessary for generating code, only when running the code:
// Generated code depends on the JAXB API, which is removed from base Java in JDK 11, and therefore needs to be added
implementation "org.glassfish.jaxb:jaxb-runtime:4.0.1"
implementation 'jakarta.xml.bind:jakarta.xml.bind-api:4.0.0' // In JAXB v3, 'javax.xml.bind' was moved to 'jakarta.xml.bind'
}
// Cookie cutter function for defining multiple XJC tasks
def addXjcTask(taskName, schema, pkg, dest) {
// If you haven't already, create the generated output dir before running XJC or it will fail
file(dest).mkdirs()
// The main XJC task, calls XJCFacade which is the entry point of the XJC JAR
tasks.create(name: taskName, type: JavaExec) {
classpath configurations.jaxb
mainClass = 'com.sun.tools.xjc.XJCFacade'
// To explore available args, download the XJC JAR and run java -jar jaxb-xjc.jar --help
args schema, "-p", pkg, "-d", dest, "-no-header"
}
}
// Add all the XJC tasks you need
addXjcTask("maven-xsd",
"src/main/resources/maven-4.0.0.xsd",
"${archivesBaseName}.my_maven_package_name",
"$generated_dir")
tasks.register("generateSources") {
description "Generate Java sources fram XSD files"
dependsOn tasks.getByName("maven-xsd")
}
使用 ./gradlew generateSources
运行(使用 Gradle v7.4.2 验证)。
关于gradle - 如何用gradle运行jaxb xjc任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71242720/
我是一名优秀的程序员,十分优秀!