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