Skip to content

Enabling PojoBuilder for Eclipse Using Gradle 6.5.1

Michael Karneim edited this page Oct 16, 2021 · 1 revision

This Gradle script improves the output of the eclipse Gradle task or Gradle 6.5.1.

It automatically adds the PojoBuilder code generator to the "Java Compiler / Annotation Processing / Factory Path" tree node.

apply plugin: 'java'
apply plugin: 'eclipse'

repositories {    
    mavenCentral()
}

dependencies {
    annotationProcessor 'net.karneim:pojobuilder:4.2.3'      
    compileOnly 'net.karneim:pojobuilder:4.2.3:annotations'    

    // other dependencies...
    // implementation 'org.slf4j:slf4j-api:1.7.25'
    // implementation 'com.google.guava:guava:31.0.1-jre'
}

ext.aptFolder = '.apt_generated'

// Enable annotation processing in Eclipse
eclipse.jdt.file.withProperties { props ->
  props['org.eclipse.jdt.core.compiler.processAnnotations'] = 'enabled'
}

task eclipseApt doLast {
  File jdtAptCorePrefs = file('.settings/org.eclipse.jdt.apt.core.prefs')
  jdtAptCorePrefs.parentFile.mkdirs()
  jdtAptCorePrefs.text = """\
  eclipse.preferences.version=1
  org.eclipse.jdt.apt.aptEnabled=true
  org.eclipse.jdt.apt.genSrcDir=${aptFolder}
  org.eclipse.jdt.apt.reconcileEnabled=true
  """.stripIndent()
}
tasks.eclipse.dependsOn eclipseApt

task eclipseFactorypath doLast {
  file('.factorypath').withWriter {
    Configuration aptEclipse = configurations.create('aptEclipse')
    sourceSets.each { SourceSet sourceSet ->
      aptEclipse.extendsFrom configurations.getByName(sourceSet.annotationProcessorConfigurationName)
    }
    new groovy.xml.MarkupBuilder(it).factorypath {
      aptEclipse.each { File dependency ->
        factorypathentry(kind: 'EXTJAR', id: dependency.absolutePath, enabled: true, runInBatchMode: false)
      }
    }
  }
}
tasks.eclipse.dependsOn eclipseFactorypath

eclipseClasspath.doFirst {
  file(aptFolder).mkdirs()
}