spotless icon indicating copy to clipboard operation
spotless copied to clipboard

Allow loading of FormatterProperties from resource

Open philipp94831 opened this issue 10 months ago • 2 comments

Currently it is only possible to load FormatterProperties from a File (e.g., XML in the case of eclipse) or String-based properties (https://github.com/diffplug/spotless/blob/4dd0095437906713fc6bad3a2e160646e11b419a/lib/src/main/java/com/diffplug/spotless/FormatterProperties.java).

However, we would like to ship our XML codestyle definition as a resource file. This cannot be loaded by spotless unless we create a temporary file. Alternatively we would need to manually convert the XML to String-based properties and basically copy the behavior of XMLParser.

Therefore, it would be greate to be able to load FormatterProperties from an input stream (by specifying the file type) or even directly from a resource identifier

philipp94831 avatar Feb 21 '25 09:02 philipp94831

For Gradle, I recommend https://github.com/diffplug/blowdryer. I don't know enough about Maven to make any recommendations here.

nedtwigg avatar Feb 23 '25 20:02 nedtwigg

Maven is straightforward (although it would have been useful to have this in the documentation) :

<plugin>
  <groupId>com.diffplug.spotless</groupId>
  <artifactId>spotless-maven-plugin</artifactId>
  <version>${spotless-maven-plugin.version}</version>
  <dependencies>
    <dependency>
      <groupId>org.jboss.pnc</groupId>
      <artifactId>ide-config</artifactId>
      <version>1.1.0</version>
    </dependency>
  </dependencies>
  <configuration>
    <java>
      <removeUnusedImports/>
      <importOrder>
        <file>java-import-order.txt</file>
      </importOrder>
      <eclipse>
        <file>java-formatter.xml</file>
      </eclipse>
      <lineEndings>UNIX</lineEndings>
    </java>
  </configuration>

For Gradle, while #2343 helped a lot, https://github.com/diffplug/spotless/pull/2361 might solve your use-case ? (then the resources could be loaded from a resource using a TextResource).

rnc avatar Feb 26 '25 14:02 rnc

With the advent of #2361 and the release of 7.2.1 its possible for Gradle (kotlin) to do something like:

    val spotlessConfig by configurations.creating
    dependencies {
        spotlessConfig("org.jboss.pnc:ide-config:1.1.0")
    }

    spotless {
        java {
            removeUnusedImports()
            importOrder(resources.text.fromArchiveEntry(spotlessConfig, "java-import-order.txt").asString())
            eclipse().configXml(resources.text.fromArchiveEntry(spotlessConfig, "java-formatter.xml").asString())
        }
    }

@nedtwigg Would it be helpful to add something like these examples to the documentation?

rnc avatar Aug 06 '25 09:08 rnc

docs such as that would be very helpful! Looks like this issue has been resolved.

nedtwigg avatar Sep 24 '25 09:09 nedtwigg

@nedtwigg I think I may have confused the situation ; I meant to submit new documentation :-) ; as a potential PR I've submitted #2643 to give an example in both the maven and gradle plugin READMEs.

rnc avatar Sep 25 '25 08:09 rnc