#How to create a custom Maven goal to Combine Spotless Apply and Compile?

1 messages · Page 1 of 1 (latest)

south jewel
#

I'm using Spotless to format code, but after making changes, mvn compile fails due to Spotless errors, requiring me to run mvn spotless:apply first. Can I create a custom Maven goal to combine spotless:apply and compile?

opal depotBOT
#

<@&987246554085740594> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
I'm using Spotless to format code, but after making changes, mvn compile fails due to Spotless errors, requiring me to run mvn spotless:apply first. Can I create a custom Maven goal to combine spotless:apply and compile?

To create a custom Maven goal to combine Spotless Apply and Compile, you can use the maven-antrun-plugin to execute the Spotless Apply goal before the Compile goal. Here's an example configuration that you can add to your pom.xml file:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-antrun-plugin</artifactId>
      <version>1.8</version>
      <executions>
        <execution>
          <id>spotless-apply-before-compile</id>
          <phase>compile</phase>
          <goals>
            <goal>run</goal>
          </goals>
          <configuration>
            <target>
              <echo message="Running Spotless Apply"/>
              <!-- Run Spotless Apply -->
              ...
            </target>
          </configuration>
        </execution>
      </executions>
    </plugin>

    <!-- Add other plugins as needed -->

  </plugins>

  <!-- Add other build configurations as needed -->

</build>

Useful links:

left sail
#
    <execution>
        <id>spotless-apply</id>
        <phase>process-sources</phase>
        <goals>
            <goal>apply</goal>
        </goals>
    </execution>

Heres part of my config for spotless as an example

<plugin>
                <groupId>com.diffplug.spotless</groupId>
                <artifactId>spotless-maven-plugin</artifactId>
                <version>${spotless.version}</version>
                <executions>
                    <execution>
                        <id>spotless-apply</id>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>apply</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>spotless-check</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
south jewel
#

Thanks for the reply

left sail
#

process-sources runs before compile so should be good

south jewel
#

Got it but this may not be helpful since i dnt want to force spotless:apply in install

#

I just need to define a simple goal that do 2 things : spotless apply + mvn compile

#

I think that doing this mvn spotless:apply compile is valid

#

But yet im looking to define a custim keyword as goal like compileAndFormat

#

I saw such in Gradle tasks but i dunno if it's possible with maven

left sail
#

I usually just make a run configuration for that

#

Just from searching a little, trying to make a custom maven goal in that way seems complicated