1+ buildscript {
2+
3+ dependencies {
4+ classpath ' org.eclipse.jdt:org.eclipse.jdt.core:3.35.0'
5+ classpath ' org.eclipse.platform:org.eclipse.text:3.14.0'
6+ }
7+ }
8+ import groovy.xml.XmlSlurper
9+ import org.eclipse.jdt.core.ToolFactory
10+ import org.eclipse.jdt.core.formatter.CodeFormatter
11+ import org.eclipse.jface.text.Document
12+
113plugins {
214 id ' eclipse'
315 id ' java'
@@ -7,16 +19,36 @@ plugins {
719 id ' com.diffplug.spotless' version ' 6.25.0'
820}
921
22+
1023spotless {
1124 java {
1225 lineEndings = com.diffplug.spotless.LineEnding . UNIX
13- eclipse(' 4.26' )
26+ custom(' eclipseFormatterDirect' ) { String source ->
27+ def configFile = new File (projectDir, ' config/eclipse-formatter.xml' )
28+ def options = new HashMap<String , String > ()
29+ def xml = new XmlSlurper (). parse(configFile)
30+ xml. profile. setting. each { setting ->
31+ options[setting. @id. text()] = setting. @value. text()
32+ }
33+ def formatter = ToolFactory . createCodeFormatter(options, ToolFactory . M_FORMAT_EXISTING )
34+ def edit = formatter. format(
35+ CodeFormatter . K_COMPILATION_UNIT ,
36+ source, 0 , source. length(), 0 , System . lineSeparator()
37+ )
38+ if (edit == null ) {
39+ throw new GradleException (" Eclipse formatter returned null — source may have a syntax error" )
40+ }
41+ def doc = new Document (source)
42+ edit. apply(doc)
43+ doc. get()
44+ }
1445 removeUnusedImports()
1546 trimTrailingWhitespace()
1647 endWithNewline()
1748 }
1849}
1950
51+
2052java {
2153 sourceCompatibility = JavaVersion . VERSION_25
2254 targetCompatibility = JavaVersion . VERSION_25
0 commit comments