Skip to content

Commit

Permalink
Fill proxy from settings.xml for each remote Maven repository (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
baldimir authored and mariofusco committed Jun 11, 2019
1 parent 40dced0 commit 39f5541
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ private static RemoteRepository.Builder toRemoteRepositoryBuilder( Settings sett
.addPassword( server.getPassword() )
.build() );
}
if (settings.getActiveProxy() != null) {
remoteBuilder.setProxy(getActiveAetherProxyFromSettings(settings));
}
return remoteBuilder;

}
Expand Down Expand Up @@ -202,6 +205,29 @@ private ArtifactRepository toArtifactRepository( RemoteRepository remoteReposito
artifactRepository.setAuthentication( new Authentication( server.getUsername(),
server.getPassword() ) );
}
if (settings.getActiveProxy() != null) {
artifactRepository.setProxy(getActiveMavenProxyFromSettings(settings));
}
return artifactRepository;
}

private static org.eclipse.aether.repository.Proxy getActiveAetherProxyFromSettings(final Settings settings) {
return new org.eclipse.aether.repository.Proxy(settings.getActiveProxy().getProtocol(),
settings.getActiveProxy().getHost(),
settings.getActiveProxy().getPort(),
new AuthenticationBuilder()
.addUsername(settings.getActiveProxy().getUsername())
.addPassword(settings.getActiveProxy().getPassword())
.build());
}

private static org.apache.maven.repository.Proxy getActiveMavenProxyFromSettings(final Settings settings) {
final org.apache.maven.repository.Proxy mavenProxy = new org.apache.maven.repository.Proxy();
mavenProxy.setProtocol(settings.getActiveProxy().getProtocol());
mavenProxy.setHost(settings.getActiveProxy().getHost());
mavenProxy.setPort(settings.getActiveProxy().getPort());
mavenProxy.setUserName(settings.getActiveProxy().getUsername());
mavenProxy.setPassword(settings.getActiveProxy().getPassword());
return mavenProxy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,46 @@
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class MavenRepositoryTest {

@Test
public void testMirrors() {
MavenRepository repo = new MavenRepositoryMock( Aether.getAether());
Collection<RemoteRepository> remoteRepos = repo.getRemoteRepositoriesForRequest();
MavenRepositoryMock.setCustomSettingsFileName("settings_with_mirror.xml");
final MavenRepository repo = new MavenRepositoryMock(Aether.getAether());
final Collection<RemoteRepository> remoteRepos = repo.getRemoteRepositoriesForRequest();
assertEquals(2, remoteRepos.size());
for (RemoteRepository remoteRepo : remoteRepos) {
for (final RemoteRepository remoteRepo : remoteRepos) {
assertTrue(remoteRepo.getId().equals("qa") ||
remoteRepo.getId().equals("foo"));
}
}

@Test
public void testProxy() {
MavenRepositoryMock.setCustomSettingsFileName("settings_custom.xml");
final MavenRepository repo = new MavenRepositoryMock(Aether.getAether());
final Collection<RemoteRepository> remoteRepos = repo.getRemoteRepositoriesForRequest();
assertEquals(5, remoteRepos.size());
for (RemoteRepository remoteRepository : remoteRepos) {
if (remoteRepository.getId().equals("test-server")) {
assertNotNull(remoteRepository.getProxy());
}
}
}

public static class MavenRepositoryMock extends MavenRepository {
protected MavenRepositoryMock(Aether aether) {

private static String customSettingsFileName;

// This is needed to do like this, because the repository is initialized by calling parent constructor with super().
public static void setCustomSettingsFileName(final String customSettingsFileNameParam) {
customSettingsFileName = customSettingsFileNameParam;
}

protected MavenRepositoryMock(final Aether aether) {
super(aether);
}

Expand All @@ -57,17 +80,17 @@ protected MavenRepositoryConfiguration getMavenRepositoryConfiguration() {
}

private Settings getMavenSettings() {
String path = getClass().getResource(".").toString().substring("file:".length());
File testSettingsFile = new File(path + "settings_with_mirror.xml");
final String path = getClass().getResource(".").toString().substring("file:".length());
final File testSettingsFile = new File(path + customSettingsFileName);
assertTrue(testSettingsFile.exists());

SettingsBuilder settingsBuilder = new DefaultSettingsBuilderFactory().newInstance();
DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
final SettingsBuilder settingsBuilder = new DefaultSettingsBuilderFactory().newInstance();
final DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
request.setUserSettingsFile( testSettingsFile );

try {
return settingsBuilder.build( request ).getEffectiveSettings();
} catch ( SettingsBuildingException e ) {
} catch ( final SettingsBuildingException e ) {
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>test-server</id>
<username>testuser</username>
<password>testpassword</password>
</server>
</servers>
<profiles>
<profile>
<id>jboss</id>
<repositories>
<repository>
<id>test-server</id>
<name>Test repository</name>
<url>file://home/testuser/.m2/repository/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>test-server</id>
<name>Test repository</name>
<url>file://home/testuser/.m2/repository/</url>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>jboss</activeProfile>
</activeProfiles>
<proxies>
<proxy>
<active>true</active>
<protocol>http</protocol>
<host>localhost</host>
<port>3128</port>
</proxy>
</proxies>
</settings>

0 comments on commit 39f5541

Please sign in to comment.