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.settings;
20  
21  import javax.inject.Inject;
22  import javax.xml.stream.XMLStreamException;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.nio.file.Files;
28  
29  import org.apache.maven.MavenTestHelper;
30  import org.apache.maven.api.settings.InputSource;
31  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
32  import org.apache.maven.bridge.MavenRepositorySystem;
33  import org.apache.maven.model.Profile;
34  import org.apache.maven.project.DefaultProjectBuilder;
35  import org.apache.maven.project.DefaultProjectBuildingRequest;
36  import org.apache.maven.project.ProjectBuildingRequest;
37  import org.apache.maven.project.harness.PomTestWrapper;
38  import org.apache.maven.settings.v4.SettingsStaxReader;
39  import org.codehaus.plexus.testing.PlexusTest;
40  import org.eclipse.aether.DefaultRepositorySystemSession;
41  import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
42  import org.eclipse.aether.repository.LocalRepository;
43  import org.junit.jupiter.api.BeforeEach;
44  import org.junit.jupiter.api.Test;
45  
46  import static org.codehaus.plexus.testing.PlexusExtension.getBasedir;
47  import static org.junit.jupiter.api.Assertions.assertEquals;
48  
49  @PlexusTest
50  class PomConstructionWithSettingsTest {
51      private static final String BASE_DIR = "src/test";
52  
53      private static final String BASE_POM_DIR = BASE_DIR + "/resources-settings";
54  
55      @Inject
56      private DefaultProjectBuilder projectBuilder;
57  
58      @Inject
59      private MavenRepositorySystem repositorySystem;
60  
61      private File testDirectory;
62  
63      @BeforeEach
64      void setUp() throws Exception {
65          testDirectory = new File(getBasedir(), BASE_POM_DIR);
66      }
67  
68      @Test
69      void testSettingsNoPom() throws Exception {
70          PomTestWrapper pom = buildPom("settings-no-pom");
71          assertEquals("local-profile-prop-value", pom.getValue("properties/local-profile-prop"));
72      }
73  
74      /**
75       * MNG-4107
76       */
77      @Test
78      void testPomAndSettingsInterpolation() throws Exception {
79          PomTestWrapper pom = buildPom("test-pom-and-settings-interpolation");
80          assertEquals("applied", pom.getValue("properties/settingsProfile"));
81          assertEquals("applied", pom.getValue("properties/pomProfile"));
82          assertEquals("settings", pom.getValue("properties/pomVsSettings"));
83          assertEquals("settings", pom.getValue("properties/pomVsSettingsInterpolated"));
84      }
85  
86      /**
87       * MNG-4107
88       */
89      @Test
90      void testRepositories() throws Exception {
91          PomTestWrapper pom = buildPom("repositories");
92          assertEquals("maven-core-it-0", pom.getValue("repositories[1]/id"));
93      }
94  
95      private PomTestWrapper buildPom(String pomPath) throws Exception {
96          File pomFile = new File(testDirectory + File.separator + pomPath, "pom.xml");
97          File settingsFile = new File(testDirectory + File.separator + pomPath, "settings.xml");
98          Settings settings = readSettingsFile(settingsFile);
99  
100         ProjectBuildingRequest config = new DefaultProjectBuildingRequest();
101 
102         for (org.apache.maven.settings.Profile rawProfile : settings.getProfiles()) {
103             Profile profile = SettingsUtils.convertFromSettingsProfile(rawProfile);
104             config.addProfile(profile);
105         }
106 
107         String localRepoUrl =
108                 System.getProperty("maven.repo.local", System.getProperty("user.home") + "/.m2/repository");
109         localRepoUrl = "file://" + localRepoUrl;
110         config.setLocalRepository(repositorySystem.createArtifactRepository(
111                 "local", localRepoUrl, new DefaultRepositoryLayout(), null, null));
112         config.setActiveProfileIds(settings.getActiveProfiles());
113 
114         DefaultRepositorySystemSession repoSession = MavenTestHelper.createSession(repositorySystem);
115         LocalRepository localRepo =
116                 new LocalRepository(config.getLocalRepository().getBasedir());
117         repoSession.setLocalRepositoryManager(
118                 new SimpleLocalRepositoryManagerFactory().newInstance(repoSession, localRepo));
119         config.setRepositorySession(repoSession);
120 
121         return new PomTestWrapper(pomFile, projectBuilder.build(pomFile, config).getProject());
122     }
123 
124     private static Settings readSettingsFile(File settingsFile) throws IOException, XMLStreamException {
125         try (InputStream is = Files.newInputStream(settingsFile.toPath())) {
126             SettingsStaxReader reader = new SettingsStaxReader();
127             InputSource source = new InputSource(settingsFile.toString());
128             return new Settings(reader.read(is, true, source));
129         }
130     }
131 }