View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.release.phase;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.nio.file.Files;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import org.apache.maven.model.Model;
29  import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
30  import org.apache.maven.project.MavenProject;
31  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
32  
33  /**
34   * @author Edwin Punzalan
35   */
36  abstract class AbstractBackupPomsPhaseTest {
37      private final String pomFilename = "pom.xml";
38  
39      protected final String releaseBackupSuffix = ".releaseBackup";
40  
41      protected List<MavenProject> getReactorProjects(String projectPath) throws Exception {
42          List<MavenProject> reactorProjects = new ArrayList<>();
43  
44          File pomFile = new File(projectPath, pomFilename);
45  
46          MavenProject mainProject = createMavenProject(pomFile);
47  
48          reactorProjects.add(mainProject);
49  
50          for (String module : mainProject.getModel().getModules()) {
51              File modulePom = new File(projectPath + "/" + module, pomFilename);
52  
53              MavenProject subproject = createMavenProject(modulePom);
54  
55              reactorProjects.add(subproject);
56          }
57  
58          return reactorProjects;
59      }
60  
61      private MavenProject createMavenProject(File pomFile) throws IOException, XmlPullParserException {
62          MavenXpp3Reader reader = new MavenXpp3Reader();
63          try (InputStream in = Files.newInputStream(pomFile.toPath())) {
64              Model model = reader.read(in);
65              MavenProject project = new MavenProject(model);
66              project.setFile(pomFile);
67              return project;
68          }
69      }
70  }