Annotation Processors can't find classes

I’m converting a project from maven to gradle, and we currently have a custom annotation processor that was written in java and maven. It seems that neither the gradle-apt-plugin nor palantir’s processor plugin can seem to find certain classes. i’m receiving this error when gradle tries to compile the project that depends on the annotation processor.

* What went wrong:
Execution failed for task ':lp-server-common:compileJava'.
> java.lang.NoClassDefFoundError: org/apache/velocity/context/Context

This is due to my annotation processor depending on Velocity, but it already has it in it’s pom.xml, so I’m not sure why it’s failing in my gradle build.

Here is my top level pom:

I think this has something to do with how I’m configuring the plugins, I’ve got a multimodule project with the top pom.

plugins {
    id "net.ltgt.apt" version "0.13"
    id "org.jetbrains.kotlin.jvm" version "1.1.51"
}

allprojects {
    apply plugin: 'maven'
    apply plugin: 'java'
    apply plugin: 'kotlin'
    apply plugin: 'net.ltgt.apt'

    group = 'com.company.server'
    version = '0.4.0-SNAPSHOT'

    repositories {
        mavenLocal()
        maven {
            credentials {
                username 'name'
                password 'password'
            }
            url "http://blah/repository-maven-snapshots-group"
        }
    }

    dependencies {
        apt 'com.company.annotations:annotation-processors:1.1.0'
        compile group: 'com.company.loanplatform', name: 'lp-common', version: '0.4.0-SNAPSHOT'
        compile group: 'com.company.thirdparty.generated.stuff', name: 'stuff-mismo231', version: '2.3.1'
        compile group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib', version: '1.1.51'
        compile group: 'org.testng', name: 'testng', version: '6.11'
        compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.11'
        testCompile group: 'com.nhaarman', name: 'mockito-kotlin', version: '1.5.0'
        testCompile group: 'org.mockito', name: 'mockito-core', version: '2.10.0'
        testCompile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.4.7'
        testCompile group: 'org.jetbrains.kotlin', name: 'kotlin-test-junit', version: '1.1.51'
        testCompile group: 'org.jetbrains.spek', name: 'spek-api', version: '1.1.5'
        testCompile group: 'org.jetbrains.spek', name: 'spek-junit-platform-engine', version: '1.1.5'
        testCompile group: 'org.spockframework', name: 'spock-core', version: '1.0-groovy-2.4'
        testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.0'
    }
}

subprojects {
    apply plugin: 'java'
    apply plugin: 'net.ltgt.apt'

    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    tasks.withType(JavaCompile) {
        options.encoding = 'UTF-8'
    }

    test.useTestNG()
}

and here’s the pom of the failing module in question.

plugins {
    id "org.jetbrains.kotlin.jvm"
    id "net.ltgt.apt"
}
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'net.ltgt.apt'
//apply plugin: 'org.inferred.processors'

group = 'com.company.blah.server.common'
description = 'description'
repositories {
    mavenLocal()
    maven {
        credentials {
            username 'user'
            password 'password'
        }
        url "http://blah/repository/maven-snapshots-group"
    }
}

dependencies {
    apt group: 'com.company.annotations', name: 'annotation-processors', version: '1.1.0'
    compile(group: 'com.company.blah.calculator', name: 'calculator-common', version: '0.1.0') {
        exclude(module: 'hibernate-jpa-2.0-api')
    }
    compile group: 'com.company.thirdparty.generated.stuff', name: 'stuff-cff11', version: '1.1'
    compile group: 'com.company.thirdparty.generated.stuff', name: 'stuff-mismo231', version: '2.3.1'

    compile group: 'com.datapublica.pg', name: 'hibernate-json', version: '0.4'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.8.0'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.8.9'
    compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.8.9'
    compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: '2.8.9'
    compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-parameter-names', version: '2.8.9'
    compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-kotlin', version: '2.8.9'
    compile group: 'com.google.guava', name: 'guava', version: '19.0'
    compile group: 'org.jvnet.jaxb2_commons', name: 'jaxb2-basics', version: '0.6.4'
    compile group: 'org.reflections', name: 'reflections', version: '0.9.10'
    compile group: 'org.mapstruct', name: 'mapstruct-jdk8', version: '1.2.0-PromonTech'
    compile group: 'org.mapstruct', name: 'mapstruct-processor', version: '1.2.0-PromonTech'
    compile group: 'io.swagger', name: 'swagger-annotations', version: '1.5.10'
    compile group: 'javax.validation', name: 'validation-api', version: '1.1.0.Final'
    compile(group: 'org.apache.commons', name: 'commons-io', version: '1.3.2') {
        exclude(module: 'commons-logging')
    }
    compile(group: 'org.apache.commons', name: 'commons-lang3', version: '3.5') {
        exclude(module: 'commons-logging')
    }
    compile group: 'org.apache.velocity', name: 'velocity', version: '1.7'
    compile group: 'org.axonframework', name: 'axon-spring', version: '3.0.6'
    compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.5.Final'
    compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.2.5.Final'
    compile group: 'org.hibernate.common', name: 'hibernate-commons-annotations', version: '5.0.1.Final'
    compile group: 'org.joda', name: 'joda-money', version: '0.11'
    compile group: 'org.postgresql', name: 'postgresql', version: '9.4.1212.jre7'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version: '1.5.6.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '1.5.6.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-hateoas', version: '1.5.6.RELEASE'
    compile group: 'org.springframework.security', name: 'spring-security-core', version: '4.2.3.RELEASE'
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compileOnly group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
}

Update 20/11/17:

That error seemed to occur due to gradle not including optional dependencies per https://stackoverflow.com/a/26220205/1394698

I had to modify the pom of the annotation project in order for gradle to find Velocity, but I’m still having issues with gradle finding my annotation class @Id which is declared in my annotation-processor project. It manifests itself with this error for any class that attempts to include my Annotation

/Users/tylerthrailkill/Documents/dev/code/server/server-common/src/main/java/com/company/decision/common/WorkItemId.java:13: error: package com.company.annotations does not exist
import com.company.annotations.Id;
                                 ^
/Users/tylerthrailkill/Documents/dev/code/server/server-common/src/main/java/com/company/decision/common/WorkItemId.java:19: error: cannot find symbol
@Id
 ^
  symbol: class Id

When using net.ltgt.apt, I believe you need both:

apt group: 'com.company.annotations', name: 'annotation-processors', version: '1.1.0'
compileOnly group: 'com.company.annotations', name: 'annotation-processors', version: '1.1.0'

On the other hand, I believe the palantir plugin automatically adds anything from the configuration for the annotations to the compile classpath as well.

Fantastic! That worked! Thank you very much! Wonder why it wasn’t working with palantir’s plugin though :confused: