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