Skip to content

Enabling PojoBuilder for Eclipse Using Gradle 4

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

This script improves the output of the Gradle 'eclipse' plugin.

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

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

ext { 
  eclipseAptFolder = '.apt_generated'
  eclipseSettingsDir = file('.settings')
}

repositories {
    mavenCentral()
}

configurations {
    codeGeneration
}

dependencies {
    codeGeneration 'net.karneim:pojobuilder:4.+'
    compile 'net.karneim:pojobuilder:4.+:annotations'
}
compileJava.classpath += configurations.codeGeneration

eclipse {
  jdt.file.withProperties { 
    it['org.eclipse.jdt.core.compiler.processAnnotations'] = 'enabled'
  }
}

tasks.eclipseJdt {
  doFirst {
    def aptPrefs = 
      file("${eclipseSettingsDir}/org.eclipse.jdt.apt.core.prefs")
    aptPrefs.parentFile.mkdirs()
    
    aptPrefs.text = """\
    eclipse.preferences.version=1
    org.eclipse.jdt.apt.aptEnabled=true
    org.eclipse.jdt.apt.genSrcDir=${eclipseAptFolder}
    org.eclipse.jdt.apt.reconcileEnabled=true
    """.stripIndent()
  
    file('.factorypath').withWriter {
      new groovy.xml.MarkupBuilder(it).'factorypath' {
        project.configurations.codeGeneration.each { dep->
          factorypathentry(
            kind:'EXTJAR',
            id:dep.absolutePath,
            enabled:true,
            runInBatchMode:false
          )
        }
      }
    }
  }
}

Just invoke it with gradle cleanEclipse eclipse.