2

I have a Spring Boot (v1.4.2) Application with Spring Data JPA and Hibernate (5.0.11.Final if I'm not mistaken). I added the jpadmodelgen-plugin to generate the metamodel classes for me, because, you know, I'm lazy. Unfortunately, when I run gradle build I get some nasty errors. The curious thing about this is that doing gradle test (which does real unit and integration tests of the complete application) runs without an error. I can even run the app in IntelliJ and life's great. It's just that my butler Jenkins can't build it.

> gradle build
:initJpaModelgenSourcesDir
:compileJpaModelgen UP-TO-DATE
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:findMainClass
:jar
:bootRepackage
:assemble
:compileJpaModelgenJava

Mapping_.java:3: error: cannot find symbol
import com.<snip>.model.domain.auth.Member;
                                   ^
  symbol:   class Member
  location: package com.<snip>.model.domain.auth
Mapping_.java:7: error: package javax.persistence.metamodel does not exist
import javax.persistence.metamodel.SingularAttribute;
                                  ^
Mapping_.java:8: error: package javax.persistence.metamodel does not exist
import javax.persistence.metamodel.StaticMetamodel;
                                  ^

100 errors
:compileJpaModelgenJava FAILED

FAILURE: Build failed with an exception.

The error shows that not only are my own classes not found but javax.persistence as well. Here's the build.gradle file.

buildscript {
    ext {
        springBootVersion = '1.4.2.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath('gradle.plugin.at.comm_unity.gradle.plugins:jpamodelgen-plugin:1.1.2')
    }
}

plugins {
    id "at.comm_unity.gradle.plugins.jpamodelgen" version "1.1.2"
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'findbugs'
apply plugin: 'jacoco'

jar {
    baseName = 'services'
    version = '1.0.0'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

jpaModelgen {
    library = "org.hibernate:hibernate-jpamodelgen:5.0.11.Final"
    jpaModelgenSourcesDir = "src/generated/java"
}

findbugs {
    ignoreFailures = true
    reportLevel = 'low'
}

javadoc {
    failOnError = false
}

dependencies {
    // Spring Boot, some Apache Commons libs and JDBC Drivers.
}

sourceSets {
    unitTest {
        resources {
            srcDir "resources"
        }
    }
    /*generated {
        java.srcDir "${buildDir}/src/generated/java"
    }*/
}

compileJava.options.compilerArgs += ["-proc:none"]

Avengers Assemble! I need help, please.

Robert Lohr
  • 635
  • 8
  • 21

2 Answers2

6

I had hoped that there's someone out there that knows a solution to this problem that retains the plugin, but, as is apparent, there is not. I found a different way by not relying on the plugin, doing a bit more work myself.

My solution is based on this answer.

  • The plugin was removed.
  • Compile dependency to Hibernate's jpamodelgen.
  • Defined a variable generatedSourcesDir...
  • ... that is added to the main java source-set.
  • Created a custom delete task removeGeneratedMetamodel that is required or otherwise there'll be odd compiler errors like this one.
  • Added some options to compileJava that generates the metamodel.
  • Added dependency to removeGeneratedMetamodel to compileJava and clean tasks.

Here's the build.gradle.

buildscript {
    ext {
        springBootVersion = '1.4.2.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'findbugs'
apply plugin: 'jacoco'

jar {
    baseName = 'services'
    version = '1.0.0'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

findbugs {
    ignoreFailures = true
    reportLevel = 'low'
}

javadoc {
    failOnError = false
}

dependencies {
    // Spring Boot, some Apache Commons libs and JDBC Drivers.

    compile group: 'org.hibernate', name: 'hibernate-jpamodelgen', version: '5.0.11.Final'
}

ext {
    generatedSourcesDir = file("src/main/java/generated")
}

sourceSets {
    unitTest {
        resources {
            srcDir "resources"
        }
    }
    main {
        java {
            srcDir 'src/main/java'
            srcDir generatedSourcesDir
        }
    }
}

task removeGeneratedMetamodel(type: Delete) {
    generatedSourcesDir.deleteDir()
}

compileJava {
    doFirst {
        generatedSourcesDir.mkdirs()
    }
    dependsOn(removeGeneratedMetamodel)
    options.compilerArgs += ['-s', generatedSourcesDir]
}

clean.dependsOn(removeGeneratedMetamodel)
Community
  • 1
  • 1
Robert Lohr
  • 635
  • 8
  • 21
0

I had the same error.

I added the

compile group: 'javax.persistence', name: 'persistence-api', version: '1.0.2'

dependency, and that resolved the issue. I found this solution here.

SiGe
  • 341
  • 6
  • 18