View Javadoc
1   /*
2    * SPDX-License-Identifier: Apache-2.0
3    * See LICENSE file for details.
4    *
5    * Copyright 2025-2026 Hazendaz
6    * Copyright 2011-2025 Acegi Technology Pty Limited.
7    */
8   package au.com.acegi.xmlformat;
9   
10  import static au.com.acegi.xmlformat.FormatUtil.needsFormatting;
11  import static org.apache.maven.plugins.annotations.LifecyclePhase.PROCESS_SOURCES;
12  
13  import java.io.File;
14  import java.io.IOException;
15  
16  import org.apache.maven.plugin.MojoExecutionException;
17  import org.apache.maven.plugin.logging.Log;
18  import org.apache.maven.plugins.annotations.Mojo;
19  import org.dom4j.DocumentException;
20  
21  /**
22   * Finds the XML files in a project and only check them: no files are changed, but the build will fail if any file does
23   * not follow the formatting conventions.
24   */
25  @Mojo(name = "xml-check", defaultPhase = PROCESS_SOURCES, threadSafe = true)
26  public final class XmlCheckPlugin extends AbstractXmlPlugin {
27  
28      @Override
29      protected boolean processFile(final File input, final XmlOutputFormat fmt) throws DocumentException, IOException {
30          final boolean needsFormatting = needsFormatting(input, fmt);
31          final Log log = getLog();
32          if (needsFormatting && log.isErrorEnabled()) {
33              log.error("[xml-check] Needs formatting:" + input);
34          } else if (log.isDebugEnabled()) {
35              log.debug("[xml-check] Correctly formatted: " + input);
36          }
37          return needsFormatting;
38      }
39  
40      @Override
41      protected void afterAllProcessed(final boolean neededFormatting) throws MojoExecutionException {
42          if (neededFormatting) {
43              throw new MojoExecutionException(
44                      "[xml-check] At least one XML file needs formatting, see the error logs above)");
45          }
46      }
47  }