How to run jaxb xjc task with gradle? - gradle

I picked up 3 gradle plugins for jaxb/xjc and none of them worked for me.
For each case, I've put the maven model xsd maven-4.0.0.xsd https://maven.apache.org/xsd/maven-4.0.0.xsd in src/main/xsd. (it's just an example, any other xsd will do)
no.entitas.gradle.jaxb:gradle-jaxb-plugin:2.0
http://github.com/stianh/gradle-jaxb-plugin
This plugin looks for xsd files in src/<source set>/xsd/**/*.xsd.
Here is the 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'
}
}
Error:
* 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.
org.hibernate.build.gradle:gradle-xjc-plugin:1.0.2.Final
https://github.com/hibernate/gradle-xjc-plugin
Here is the 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')
}
}
}
Error:
* What went wrong:
Neither path nor baseDir may be null or empty string. path='null' basedir='C:\Users\xxxxx\eclipse-workspace\test-gradle'
com.intershop.gradle.jaxb:jaxb-gradle-plugin:5.1.0
https://github.com/IntershopCommunicationsAG/jaxb-gradle-plugin
Here is the 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'
}
Error:
* 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.
EDIT
Gradle version : 2.3 (it's the version that I must use)
JDK version : 1.8.0_311
Command : gradle build

Here is an example without using specific plugins (just java), and v4 of jaxb-xjc:
(config expects maven-4.0.0.xsd to be in 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")
}
Run with ./gradlew generateSources (verified with Gradle v7.4.2).

I found 2 other plugins.
The jacobono/gradle-jaxb-plugin uses relative directories and creates the destination directory :
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.github.jacobono:gradle-jaxb-plugin:1.3.5"
}
}
apply plugin: "com.github.jacobono.jaxb"
repositories {
mavenCentral()
}
sourceSets {
main {
java {
srcDirs = ["${project.buildDir}/generated-sources/xjc",'src/main/java']
}
resources {
srcDirs = ['src/main/resources']
}
}
}
dependencies {
jaxb 'org.jvnet.jaxb2_commons:jaxb2-basics-ant:1.11.1'
jaxb 'org.jvnet.jaxb2_commons:jaxb2-basics:1.11.1'
jaxb 'org.jvnet.jaxb2_commons:jaxb2-basics-annotate:1.1.0'
jaxb 'org.glassfish.jaxb:jaxb-xjc:2.2.11'
jaxb 'org.glassfish.jaxb:jaxb-runtime:2.2.11'
jaxb 'javax.xml.bind:jaxb-api:2.2.11'
jaxb 'org.slf4j:slf4j-nop:1.7.30'
}
jaxb {
xsdDir = "src/main/xsd"
xjc {
taskClassname = "org.jvnet.jaxb2_commons.xjc.XJC2Task"
generatePackage = "org.apache.maven.model"
destinationDir = "build/generated-sources/xjc"
args = ["-Xinheritance", "-Xannotate"]
}
}
compileJava.dependsOn 'xjc'
The rackerlabs/gradle-jaxb-plugin uses absolute directories and creates the destination directory only if it's the default one ${project.buildDir}/generated-sources/xjc:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'gradle.plugin.org.openrepose:gradle-jaxb-plugin:2.5.0'
}
}
apply plugin: "org.openrepose.gradle.plugins.jaxb"
repositories {
mavenCentral()
}
sourceSets {
main {
java {
srcDirs = ["${project.buildDir}/generated-sources/xjc",'src/main/java']
}
resources {
srcDirs = ['src/main/resources']
}
}
}
dependencies {
jaxb 'org.jvnet.jaxb2_commons:jaxb2-basics-ant:1.11.1'
jaxb 'org.jvnet.jaxb2_commons:jaxb2-basics:1.11.1'
jaxb 'org.jvnet.jaxb2_commons:jaxb2-basics-annotate:1.1.0'
jaxb 'org.glassfish.jaxb:jaxb-xjc:2.2.11'
jaxb 'org.glassfish.jaxb:jaxb-runtime:2.2.11'
jaxb 'javax.xml.bind:jaxb-api:2.2.11'
jaxb 'org.slf4j:slf4j-nop:1.7.30'
}
jaxb {
xsdDir = "${project.projectDir}/src/main/xsd"
xjc {
generateEpisodeFiles = false
taskClassname = "org.jvnet.jaxb2_commons.xjc.XJC2Task"
generatePackage = "org.apache.maven.model"
args = ["-Xinheritance", "-Xannotate"]
}
}
compileJava.dependsOn 'xjc'

Related

Gradle | Configuration with name 'complileClasspath' not found. for multi-level project build

I am trying to build a multi-project build in Gradle using Kotlin DSL.
Root build.gradle.kts:
allprojects {
repositories {
jcenter()
google()
}
}
subprojects{
version = "1.0"
}
plugins {
java
application
}
repositories {
jcenter()
}
dependencies {
implementation("com.google.guava:guava:27.0.1-jre")
testImplementation("junit:junit:4.12")
}
application {
mainClassName = "gradle.youtube.series.App"
}
gitutils subproject's build.gradle.kts
plugins {
`java-library`
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.sl4j:sl4j-api:1.7.2")
implementation("org.eclipse.jgit:org.eclipse.jgit:5.3.0.201903130848-r")
testImplementation("org.junit.jupiter:junit-jupiter:5.4.1")
testRuntimeOnly("org.sl4j:sl4j-simple:1.7.2")
}
tasks.test {
useJUnitPlatform()
}
I am trying to download gradeutil's dependencies using the following command:
./gradlew gitutils:dependencies --configuration complileClasspath
I am getting the following error:
Configuration with name 'complileClasspath' not found.
Is compileClassPath required for multi-level gradle project? What is its use?

Gradle (Kotlin DSL): "Unresolved reference: proguard"

Im trying to get Proguard to work but Im still new to Gradle.
My build gradle.kts haves an error (Unresolved reference: proguard), I cant create a proguard Task:
plugins {
id("com.github.johnrengelman.shadow") version "5.2.0"
java
kotlin("jvm") version "1.3.61"
}
group = "*...*"
version = "*...*"
repositories {
mavenCentral()
jcenter()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
//*...*
implementation("net.sf.proguard","proguard-gradle","6.2.2") //is this correct?
}
configure<JavaPluginConvention> {
sourceCompatibility = JavaVersion.VERSION_1_8
}
tasks {
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
jar{
manifest {
attributes["Main-Class"] = "*...*"
}
}
shadowJar{
archiveBaseName.set("*...*")
archiveClassifier.set("")
archiveVersion.set("")
}
register<proguard.gradle.ProGuardTask>("myProguardTask") { //Unresolved reference: proguard
}
}
This is not an Android Project
Because Stackoverflow wants me to write more than just code: Im planing to somehow link the proguard output to the shadowjar task. If you know how to do it Im also interested to that (and I could not try it myself because of this problem).
You declared a dependency of proguard in project rather than for Gradle itself.
Move the dependency to the buildscript block:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'net.sf.proguard:proguard-gradle:6.2.2'
}
}
Then you should be able to create your task.
Alternatively, you can declare the repository in settings.gradle.kts:
pluginManagement {
repositories {
jcenter()
}
}
which will trim down the buildscript block in build.gradle.kts:
buildscript {
dependencies {
classpath("net.sf.proguard:proguard-gradle:6.2.2")
}
}

How to rename file after project is builded in gradle 5 kotlin DSL

Could you please help me to find a proper way to rename built artifact with Gradle 5 Kotlin DSL
I created a Gradle 5.5.1 Spring Boot 2 project based on Kotlin DSL.
After executing gradle build the built artifact is inside $buildDir/libs folder.
How can I rename it? Let's say I want to give a simple name - app.jar
plugins {
id("java")
id("idea")
id("org.springframework.boot") version "2.1.5.RELEASE"
id("io.spring.dependency-management") version "1.0.8.RELEASE"
}
group = "com.hbv"
version = "1.0.0-SNAPSHOT"
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.5.RELEASE")
}
}
the<DependencyManagementExtension>().apply {
imports {
mavenBom(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
}
}
java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
implementation(platform("org.springframework.cloud:spring-cloud-dependencies:Greenwich.RELEASE"))
implementation("org.springframework.cloud:spring-cloud-starter-config")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.springframework.boot:spring-boot-starter-security")
testImplementation("com.h2database", "h2")
testImplementation("org.springframework.boot", "spring-boot-starter-test")
testImplementation("org.springframework.security", "spring-security-test")
}```
Configure the bootJar task which is generating the jar and is a gradle jar task and set its archiveFileName property:
tasks {
bootJar {
archiveFileName.set("app.jar")
}
}
define archiveBaseName in jar task
tasks.withType<Jar> {
archiveBaseName.set("app")
manifest {
attributes["Main-Class"] = application.mainClass
attributes["Implementation-Version"] = archiveVersion
attributes["Implementation-Title"] = "Test for App"
}
}

java.lang.NoClassDefFoundError in Intellij Plugin

I'm trying to build a plugin for Intellij but I'm getting a java.lang.NoClassDefFoundError at runtime every time my code point to a class in another module or to an external library.
Everything works fine in my tests and in the sandbox via runIde.
I also managed to reproduce the error by creating a new project with just an action and a module with a class and an empty method.
root gradle:
buildscript {
ext.kotlin_version = '1.2.31'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
plugins {
id 'org.jetbrains.intellij' version '0.3.12'
}
group 'test'
version '1.0-SNAPSHOT'
apply plugin: 'kotlin'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile project(':testmodule')
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
intellij {
version '2018.1.6'
}
patchPluginXml {
changeNotes """
Add change notes here.
most HTML tags may be used"""
}
action:
package action
import com.intellij.openapi.actionSystem.*
import packages.OtherModuleClass
class TestAction : AnAction() {
override fun actionPerformed(e: AnActionEvent?) {
OtherModuleClass().otherModuleMethod()
}
}
other module class:
package packages
class OtherModuleClass {
fun otherModuleMethod() {}
}
I found out what my problem was.
I was installing in my IDE the jar in build/libs instead of the zip in build/distributions.
If someone else is looking for a solution for similar problem, I fixed it by adding the java plugin in build.gradle
apply plugin "java"

How to copy some buildscript classpath JARs but exclude others in Gradle?

I have buildscript block that references many JARs:
buildscript {
dependencies { classpath "com.github.kulya:jmeter-gradle-plugin:1.3.4-2.13" }
dependencies { classpath "com.eriwen:gradle-css-plugin:2.14.0" }
dependencies { classpath "com.eriwen:gradle-js-plugin:2.14.1" }
dependencies { classpath "com.google.javascript:closure-compiler:v20160208" }
dependencies { classpath "org.akhikhl.gretty:gretty:+" }
}
task copyWarGradlePlugins(type: Copy) {
destinationDir = file(project.buildDir.name + '/warGradlePlugins')
from (buildscript.configurations.classpath)
}
I want to copy (to my WAR file) some of the referenced JARs (e.g. dependencies of gradle-css-plugin, gradle-js-plugin, closure-compiler) but not others (e.g. gretty, jmeter-gradle-plugin).
How do I exclude for example jmeter-gradle-plugin and all its dependencies?
UPDATE:
There are 150+ JARs. Excluding them by file name is not practical/maintainable.
buildscript {
dependencies { classpath "com.github.kulya:jmeter-gradle-plugin:1.3.4-2.13" }
dependencies { classpath "com.eriwen:gradle-css-plugin:2.14.0" }
dependencies { classpath "com.eriwen:gradle-js-plugin:2.14.1" }
dependencies { classpath "com.google.javascript:closure-compiler:v20160208" }
dependencies { classpath "org.akhikhl.gretty:gretty:+" }
}
task copyWarGradlePlugins(type: Copy) { copy ->
destinationDir = file(project.buildDir.name + '/warGradlePlugins')
copy.from(buildscript.configurations.classpath) { copySpec ->
// exclusions are based on the file name as here you will be handed
// DefaultFileVisitDetails type
copySpec.exclude { it.name.startsWith 'gretty' }
copySpec.exclude { it.name.startsWith 'closure-compiler' }
}
}

Resources