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 java.io.File; 023import java.io.IOException; 024import java.io.Reader; 025 026import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout; 027import org.apache.maven.model.Profile; 028import org.apache.maven.project.DefaultProjectBuilder; 029import org.apache.maven.project.DefaultProjectBuildingRequest; 030import org.apache.maven.project.ProjectBuilder; 031import org.apache.maven.project.ProjectBuildingRequest; 032import org.apache.maven.project.harness.PomTestWrapper; 033import org.apache.maven.repository.RepositorySystem; 034import org.apache.maven.repository.internal.MavenRepositorySystemUtils; 035import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader; 036import org.codehaus.plexus.ContainerConfiguration; 037import org.codehaus.plexus.PlexusConstants; 038import org.codehaus.plexus.PlexusTestCase; 039import org.codehaus.plexus.util.IOUtil; 040import org.codehaus.plexus.util.ReaderFactory; 041import org.codehaus.plexus.util.xml.pull.XmlPullParserException; 042import org.eclipse.aether.DefaultRepositorySystemSession; 043import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; 044import org.eclipse.aether.repository.LocalRepository; 045 046public class PomConstructionWithSettingsTest 047 extends PlexusTestCase 048{ 049 private static final String BASE_DIR = "src/test"; 050 051 private static final String BASE_POM_DIR = BASE_DIR + "/resources-settings"; 052 053 private DefaultProjectBuilder projectBuilder; 054 055 private RepositorySystem repositorySystem; 056 057 private File testDirectory; 058 059 @Override 060 protected void customizeContainerConfiguration( ContainerConfiguration containerConfiguration ) 061 { 062 super.customizeContainerConfiguration( containerConfiguration ); 063 containerConfiguration.setAutoWiring( true ); 064 containerConfiguration.setClassPathScanning( PlexusConstants.SCANNING_INDEX ); 065 } 066 067 protected void setUp() 068 throws Exception 069 { 070 testDirectory = new File( getBasedir(), BASE_POM_DIR ); 071 projectBuilder = (DefaultProjectBuilder) lookup( ProjectBuilder.class ); 072 repositorySystem = lookup( RepositorySystem.class ); 073 } 074 075 @Override 076 protected void tearDown() 077 throws Exception 078 { 079 projectBuilder = null; 080 081 super.tearDown(); 082 } 083 084 public void testSettingsNoPom() 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 /**MNG-4107 */ 091 public void testPomAndSettingsInterpolation() throws Exception 092 { 093 PomTestWrapper pom = buildPom( "test-pom-and-settings-interpolation" ); 094 assertEquals( "applied", pom.getValue( "properties/settingsProfile" ) ); 095 assertEquals( "applied", pom.getValue( "properties/pomProfile" ) ); 096 assertEquals( "settings", pom.getValue( "properties/pomVsSettings" ) ); 097 assertEquals( "settings", pom.getValue( "properties/pomVsSettingsInterpolated" ) ); 098 } 099 100 /**MNG-4107 */ 101 public void testRepositories() throws Exception 102 { 103 PomTestWrapper pom = buildPom( "repositories" ); 104 assertEquals( "maven-core-it-0", pom.getValue( "repositories[1]/id" ) ); 105 } 106 107 private PomTestWrapper buildPom( String pomPath ) 108 throws Exception 109 { 110 File pomFile = new File( testDirectory + File.separator + pomPath, "pom.xml" ); 111 File settingsFile = new File( testDirectory + File.separator + pomPath, "settings.xml" ); 112 Settings settings = readSettingsFile( settingsFile ); 113 114 ProjectBuildingRequest config = new DefaultProjectBuildingRequest(); 115 116 for ( org.apache.maven.settings.Profile rawProfile : settings.getProfiles() ) 117 { 118 Profile profile = SettingsUtils.convertFromSettingsProfile( rawProfile ); 119 config.addProfile( profile ); 120 } 121 122 String localRepoUrl = 123 System.getProperty( "maven.repo.local", System.getProperty( "user.home" ) + "/.m2/repository" ); 124 localRepoUrl = "file://" + localRepoUrl; 125 config.setLocalRepository( repositorySystem.createArtifactRepository( "local", localRepoUrl, 126 new DefaultRepositoryLayout(), null, null ) ); 127 config.setActiveProfileIds( settings.getActiveProfiles() ); 128 129 DefaultRepositorySystemSession repoSession = MavenRepositorySystemUtils.newSession(); 130 LocalRepository localRepo = new LocalRepository( config.getLocalRepository().getBasedir() ); 131 repoSession.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().newInstance( repoSession, localRepo ) ); 132 config.setRepositorySession( repoSession ); 133 134 return new PomTestWrapper( pomFile, projectBuilder.build( pomFile, config ).getProject() ); 135 } 136 137 private static Settings readSettingsFile( File settingsFile ) 138 throws IOException, XmlPullParserException 139 { 140 Settings settings = null; 141 142 Reader reader = null; 143 144 try 145 { 146 reader = ReaderFactory.newXmlReader( settingsFile ); 147 148 SettingsXpp3Reader modelReader = new SettingsXpp3Reader(); 149 150 settings = modelReader.read( reader ); 151 } 152 finally 153 { 154 IOUtil.close( reader ); 155 } 156 157 return settings; 158 } 159}