View Javadoc
1   /*
2    * XML Format Maven Plugin (https://github.com/acegi/xml-format-maven-plugin)
3    *
4    * Copyright 2011-2025 Acegi Technology Pty Limited.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package au.com.acegi.xmlformat;
19  
20  import static au.com.acegi.xmlformat.FormatUtil.needsFormatting;
21  import static org.apache.maven.plugins.annotations.LifecyclePhase.PROCESS_SOURCES;
22  
23  import java.io.File;
24  import java.io.IOException;
25  
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugin.logging.Log;
28  import org.apache.maven.plugins.annotations.Mojo;
29  import org.dom4j.DocumentException;
30  
31  /**
32   * Finds the XML files in a project and only check them: no files are changed, but the build will fail if any file does
33   * not follow the formatting conventions.
34   */
35  @Mojo(name = "xml-check", defaultPhase = PROCESS_SOURCES, threadSafe = true)
36  public final class XmlCheckPlugin extends AbstractXmlPlugin {
37  
38      @Override
39      protected boolean processFile(final File input, final XmlOutputFormat fmt) throws DocumentException, IOException {
40          final boolean needsFormatting = needsFormatting(input, fmt);
41          final Log log = getLog();
42          if (needsFormatting && log.isErrorEnabled()) {
43              log.error("[xml-check] Needs formatting:" + input);
44          } else if (log.isDebugEnabled()) {
45              log.debug("[xml-check] Correctly formatted: " + input);
46          }
47          return needsFormatting;
48      }
49  
50      @Override
51      protected void afterAllProcessed(final boolean neededFormatting) throws MojoExecutionException {
52          if (neededFormatting) {
53              throw new MojoExecutionException(
54                      "[xml-check] At least one XML file needs formatting, see the error logs above)");
55          }
56      }
57  }