ProjectHelper.java

  1. package com.soebes.itf.jupiter.maven;

  2. /*
  3.  * Licensed to the Apache Software Foundation (ASF) under one
  4.  * or more contributor license agreements.  See the NOTICE file
  5.  * distributed with this work for additional information
  6.  * regarding copyright ownership.  The ASF licenses this file
  7.  * to you under the Apache License, Version 2.0 (the
  8.  * "License"); you may not use this file except in compliance
  9.  * with the License.  You may obtain a copy of the License at
  10.  *
  11.  *  http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing,
  14.  * software distributed under the License is distributed on an
  15.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16.  * KIND, either express or implied.  See the License for the
  17.  * specific language governing permissions and limitations
  18.  * under the License.
  19.  */

  20. import org.apache.maven.model.Model;
  21. import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
  22. import org.apiguardian.api.API;
  23. import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.nio.file.Files;
  27. import java.nio.file.Path;

  28. import static org.apiguardian.api.API.Status.EXPERIMENTAL;

  29. /**
  30.  * @author Karl Heinz Marbaise
  31.  */
  32. @API(status = EXPERIMENTAL, since = "0.1.0")
  33. public final class ProjectHelper {

  34.   private ProjectHelper() {
  35.     // intentionally private.
  36.   }

  37.   /**
  38.    * @param inputStream The stream where to read the {@code pom.xml} from.
  39.    * @return The {@link Model}
  40.    */
  41.   public static Model readProject(InputStream inputStream) {
  42.     MavenXpp3Reader mavenXpp3Reader = new MavenXpp3Reader();
  43.     try {
  44.       return mavenXpp3Reader.read(inputStream);
  45.     } catch (XmlPullParserException | IOException e) {
  46.       throw new IllegalStateException("Failed to read pom.xml", e);
  47.     }
  48.   }

  49.   /**
  50.    * @param pomFile The directory where to read the {@code pom.xml} from.
  51.    * @return The {@link Model}
  52.    * @since 0.12.0 Method added.
  53.    */
  54.   @API(status = EXPERIMENTAL, since = "0.12.0")
  55.   public static Model readProject(Path pomFile) {
  56.     try (InputStream is = Files.newInputStream(pomFile)) {
  57.       return readProject(is);
  58.     } catch (IOException e) {
  59.       throw new IllegalStateException("Failed to read pom.xml", e);
  60.     }
  61.   }
  62. }