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.PlexusTestCase; 038import org.codehaus.plexus.util.IOUtil; 039import org.codehaus.plexus.util.ReaderFactory; 040import org.codehaus.plexus.util.xml.pull.XmlPullParserException; 041import org.eclipse.aether.DefaultRepositorySystemSession; 042import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; 043import org.eclipse.aether.repository.LocalRepository; 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 } 064 065 protected void setUp() 066 throws Exception 067 { 068 testDirectory = new File( getBasedir(), BASE_POM_DIR ); 069 projectBuilder = (DefaultProjectBuilder) lookup( ProjectBuilder.class ); 070 repositorySystem = lookup( RepositorySystem.class ); 071 } 072 073 @Override 074 protected void tearDown() 075 throws Exception 076 { 077 projectBuilder = null; 078 079 super.tearDown(); 080 } 081 082 public void testSettingsNoPom() throws Exception 083 { 084 PomTestWrapper pom = buildPom( "settings-no-pom" ); 085 assertEquals( "local-profile-prop-value", pom.getValue( "properties/local-profile-prop" ) ); 086 } 087 088 /**MNG-4107 */ 089 public void testPomAndSettingsInterpolation() throws Exception 090 { 091 PomTestWrapper pom = buildPom( "test-pom-and-settings-interpolation" ); 092 assertEquals( "applied", pom.getValue( "properties/settingsProfile" ) ); 093 assertEquals( "applied", pom.getValue( "properties/pomProfile" ) ); 094 assertEquals( "settings", pom.getValue( "properties/pomVsSettings" ) ); 095 assertEquals( "settings", pom.getValue( "properties/pomVsSettingsInterpolated" ) ); 096 } 097 098 /**MNG-4107 */ 099 public void testRepositories() throws Exception 100 { 101 PomTestWrapper pom = buildPom( "repositories" ); 102 assertEquals( "maven-core-it-0", pom.getValue( "repositories[1]/id" ) ); 103 } 104 105 private PomTestWrapper buildPom( String pomPath ) 106 throws Exception 107 { 108 File pomFile = new File( testDirectory + File.separator + pomPath, "pom.xml" ); 109 File settingsFile = new File( testDirectory + File.separator + pomPath, "settings.xml" ); 110 Settings settings = readSettingsFile( settingsFile ); 111 112 ProjectBuildingRequest config = new DefaultProjectBuildingRequest(); 113 114 for ( org.apache.maven.settings.Profile rawProfile : settings.getProfiles() ) 115 { 116 Profile profile = SettingsUtils.convertFromSettingsProfile( rawProfile ); 117 config.addProfile( profile ); 118 } 119 120 String localRepoUrl = 121 System.getProperty( "maven.repo.local", System.getProperty( "user.home" ) + "/.m2/repository" ); 122 localRepoUrl = "file://" + localRepoUrl; 123 config.setLocalRepository( repositorySystem.createArtifactRepository( "local", localRepoUrl, 124 new DefaultRepositoryLayout(), null, null ) ); 125 config.setActiveProfileIds( settings.getActiveProfiles() ); 126 127 DefaultRepositorySystemSession repoSession = MavenRepositorySystemUtils.newSession(); 128 LocalRepository localRepo = new LocalRepository( config.getLocalRepository().getBasedir() ); 129 repoSession.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().newInstance( repoSession, localRepo ) ); 130 config.setRepositorySession( repoSession ); 131 132 return new PomTestWrapper( pomFile, projectBuilder.build( pomFile, config ).getProject() ); 133 } 134 135 private static Settings readSettingsFile( File settingsFile ) 136 throws IOException, XmlPullParserException 137 { 138 Settings settings = null; 139 140 Reader reader = null; 141 142 try 143 { 144 reader = ReaderFactory.newXmlReader( settingsFile ); 145 146 SettingsXpp3Reader modelReader = new SettingsXpp3Reader(); 147 148 settings = modelReader.read( reader ); 149 } 150 finally 151 { 152 IOUtil.close( reader ); 153 } 154 155 return settings; 156 } 157}