001package org.apache.maven.settings; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout; 023import org.apache.maven.model.Profile; 024import org.apache.maven.project.DefaultProjectBuilder; 025import org.apache.maven.project.DefaultProjectBuildingRequest; 026import org.apache.maven.project.ProjectBuilder; 027import org.apache.maven.project.ProjectBuildingRequest; 028import org.apache.maven.project.harness.PomTestWrapper; 029import org.apache.maven.repository.RepositorySystem; 030import org.apache.maven.repository.internal.MavenRepositorySystemUtils; 031import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader; 032import org.codehaus.plexus.ContainerConfiguration; 033import org.codehaus.plexus.PlexusConstants; 034import org.codehaus.plexus.PlexusTestCase; 035import org.codehaus.plexus.util.ReaderFactory; 036import org.codehaus.plexus.util.xml.pull.XmlPullParserException; 037import org.eclipse.aether.DefaultRepositorySystemSession; 038import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; 039import org.eclipse.aether.repository.LocalRepository; 040 041import java.io.File; 042import java.io.IOException; 043import java.io.Reader; 044 045public class PomConstructionWithSettingsTest 046 extends PlexusTestCase 047{ 048 private static final String BASE_DIR = "src/test"; 049 050 private static final String BASE_POM_DIR = BASE_DIR + "/resources-settings"; 051 052 private DefaultProjectBuilder projectBuilder; 053 054 private RepositorySystem repositorySystem; 055 056 private File testDirectory; 057 058 @Override 059 protected void customizeContainerConfiguration( ContainerConfiguration containerConfiguration ) 060 { 061 super.customizeContainerConfiguration( containerConfiguration ); 062 containerConfiguration.setAutoWiring( true ); 063 containerConfiguration.setClassPathScanning( PlexusConstants.SCANNING_INDEX ); 064 } 065 066 protected void setUp() 067 throws Exception 068 { 069 testDirectory = new File( getBasedir(), BASE_POM_DIR ); 070 projectBuilder = (DefaultProjectBuilder) lookup( ProjectBuilder.class ); 071 repositorySystem = lookup( RepositorySystem.class ); 072 } 073 074 @Override 075 protected void tearDown() 076 throws Exception 077 { 078 projectBuilder = null; 079 080 super.tearDown(); 081 } 082 083 public void testSettingsNoPom() 084 throws Exception 085 { 086 PomTestWrapper pom = buildPom( "settings-no-pom" ); 087 assertEquals( "local-profile-prop-value", pom.getValue( "properties/local-profile-prop" ) ); 088 } 089 090 /** 091 * MNG-4107 092 */ 093 public void testPomAndSettingsInterpolation() 094 throws Exception 095 { 096 PomTestWrapper pom = buildPom( "test-pom-and-settings-interpolation" ); 097 assertEquals( "applied", pom.getValue( "properties/settingsProfile" ) ); 098 assertEquals( "applied", pom.getValue( "properties/pomProfile" ) ); 099 assertEquals( "settings", pom.getValue( "properties/pomVsSettings" ) ); 100 assertEquals( "settings", pom.getValue( "properties/pomVsSettingsInterpolated" ) ); 101 } 102 103 /** 104 * MNG-4107 105 */ 106 public void testRepositories() 107 throws Exception 108 { 109 PomTestWrapper pom = buildPom( "repositories" ); 110 assertEquals( "maven-core-it-0", pom.getValue( "repositories[1]/id" ) ); 111 } 112 113 private PomTestWrapper buildPom( String pomPath ) 114 throws Exception 115 { 116 File pomFile = new File( testDirectory + File.separator + pomPath, "pom.xml" ); 117 File settingsFile = new File( testDirectory + File.separator + pomPath, "settings.xml" ); 118 Settings settings = readSettingsFile( settingsFile ); 119 120 ProjectBuildingRequest config = new DefaultProjectBuildingRequest(); 121 122 for ( org.apache.maven.settings.Profile rawProfile : settings.getProfiles() ) 123 { 124 Profile profile = SettingsUtils.convertFromSettingsProfile( rawProfile ); 125 config.addProfile( profile ); 126 } 127 128 String localRepoUrl = 129 System.getProperty( "maven.repo.local", System.getProperty( "user.home" ) + "/.m2/repository" ); 130 localRepoUrl = "file://" + localRepoUrl; 131 config.setLocalRepository( 132 repositorySystem.createArtifactRepository( "local", localRepoUrl, new DefaultRepositoryLayout(), null, 133 null ) ); 134 config.setActiveProfileIds( settings.getActiveProfiles() ); 135 136 DefaultRepositorySystemSession repoSession = MavenRepositorySystemUtils.newSession(); 137 LocalRepository localRepo = new LocalRepository( config.getLocalRepository().getBasedir() ); 138 repoSession.setLocalRepositoryManager( 139 new SimpleLocalRepositoryManagerFactory().newInstance( repoSession, localRepo ) ); 140 config.setRepositorySession( repoSession ); 141 142 return new PomTestWrapper( pomFile, projectBuilder.build( pomFile, config ).getProject() ); 143 } 144 145 private static Settings readSettingsFile( File settingsFile ) 146 throws IOException, XmlPullParserException 147 { 148 Settings settings = null; 149 150 try ( Reader reader = ReaderFactory.newXmlReader( settingsFile ) ) 151 { 152 SettingsXpp3Reader modelReader = new SettingsXpp3Reader(); 153 154 settings = modelReader.read( reader ); 155 } 156 return settings; 157 } 158}