Skip to content

[MNG-5913] Allow defining aliases for existing server configurations in settings.xml #2346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: maven-3.9.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions maven-settings-builder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ under the License.
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-sec-dispatcher</artifactId>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.27.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.building.FileSource;
import org.apache.maven.building.Source;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.TrackableBase;
import org.apache.maven.settings.io.SettingsParseException;
Expand Down Expand Up @@ -91,10 +94,10 @@ public SettingsBuildingResult build(SettingsBuildingRequest request) throws Sett

Source globalSettingsSource =
getSettingsSource(request.getGlobalSettingsFile(), request.getGlobalSettingsSource());
Settings globalSettings = readSettings(globalSettingsSource, request, problems);
Settings globalSettings = readSettings(globalSettingsSource, problems);

Source userSettingsSource = getSettingsSource(request.getUserSettingsFile(), request.getUserSettingsSource());
Settings userSettings = readSettings(userSettingsSource, request, problems);
Settings userSettings = readSettings(userSettingsSource, problems);

settingsMerger.merge(userSettings, globalSettings, TrackableBase.GLOBAL_LEVEL);

Expand Down Expand Up @@ -139,8 +142,7 @@ private Source getSettingsSource(File settingsFile, Source settingsSource) {
return null;
}

private Settings readSettings(
Source settingsSource, SettingsBuildingRequest request, DefaultSettingsProblemCollector problems) {
private Settings readSettings(Source settingsSource, DefaultSettingsProblemCollector problems) {
if (settingsSource == null) {
return new Settings();
}
Expand Down Expand Up @@ -180,11 +182,26 @@ private Settings readSettings(
return new Settings();
}

settings.setServers(serversByIds(settings.getServers()));
settingsValidator.validate(settings, problems);

return settings;
}

private List<Server> serversByIds(List<Server> servers) {
return servers.stream()
.flatMap(server -> Stream.concat(
Stream.of(server), server.getIds().stream().map(id -> serverAlias(server, id))))
.collect(Collectors.toList());
}

private Server serverAlias(Server server, String id) {
Server serverClone = server.clone();
serverClone.setId(id);
serverClone.setIds(Collections.emptyList());
return serverClone;
}

private Settings interpolate(
Settings settings, SettingsBuildingRequest request, SettingsProblemCollector problems) {
StringWriter writer = new StringWriter(1024 * 4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,105 @@
package org.apache.maven.settings.building;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import junit.framework.TestCase;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Benjamin Bentmann
*/
public class DefaultSettingsBuilderFactoryTest extends TestCase {
public class DefaultSettingsBuilderFactoryTest {

private File getSettings(String name) {
return new File("src/test/resources/settings/factory/" + name + ".xml").getAbsoluteFile();
}

public void testCompleteWiring() throws Exception {
private SettingsBuildingResult execute(String settingsName) throws Exception {
Properties properties = new Properties();
properties.setProperty("user.home", "/home/user");

SettingsBuilder builder = new DefaultSettingsBuilderFactory().newInstance();
assertNotNull(builder);
assertThat(builder).isNotNull();

DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
request.setSystemProperties(System.getProperties());
request.setUserSettingsFile(getSettings("simple"));
request.setSystemProperties(properties);
request.setUserSettingsFile(getSettings(settingsName));

SettingsBuildingResult result = builder.build(request);
assertNotNull(result);
assertNotNull(result.getEffectiveSettings());
return assertThat(result).isNotNull().actual();
}

@Test
public void testCompleteWiring() throws Exception {
Settings settings = assertThat(execute("simple"))
.extracting(SettingsBuildingResult::getEffectiveSettings)
.actual();

assertThat(settings.getLocalRepository())
.satisfiesAnyOf(
repo -> assertThat(repo).isEqualTo("/home/user/.m2/repository"),
repo -> assertThat(repo).endsWith("\\home\\user\\.m2\\repository"));
}

@Test
public void testSettingsWithServers() throws Exception {
Settings settings = assertThat(execute("settings-servers-1"))
.extracting(SettingsBuildingResult::getEffectiveSettings)
.actual();

assertThat(settings.getServers())
.hasSize(2)
.usingRecursiveFieldByFieldElementComparator()
.containsExactlyInAnyOrder(
aServer("server-1", "username1", "password1"), aServer("server-2", "username2", "password2"));
}

@Test
public void testSettingsWithServersAndAliases() throws Exception {
Settings settings = assertThat(execute("settings-servers-2"))
.extracting(SettingsBuildingResult::getEffectiveSettings)
.actual();

assertThat(settings.getServers())
.hasSize(6)
.usingRecursiveFieldByFieldElementComparator()
.containsExactlyInAnyOrder(
aServer("server-1", "username1", "password1", Arrays.asList("server-11", "server-12")),
aServer("server-11", "username1", "password1"),
aServer("server-12", "username1", "password1"),
aServer("server-2", "username2", "password2", Collections.singletonList("server-21")),
aServer("server-21", "username2", "password2"),
aServer("server-3", "username3", "password3"));
}

private Server aServer(String id, String username, String password) {
Server server = new Server();
server.setId(id);
server.setUsername(username);
server.setPassword(password);
return server;
}

private Server aServer(String id, String username, String password, List<String> ids) {
Server server = aServer(id, username, password);
server.setIds(ids);
return server;
}

@Test
public void testSettingsWithDuplicateServersIds() throws Exception {
SettingsBuildingResult result = execute("settings-servers-3");

assertThat(result.getProblems())
.hasSize(1)
.extracting(SettingsProblem::getMessage)
.containsExactly("'servers.server.id' must be unique but found duplicate server with id server-2");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

<settings>
<localRepository>${user.home}/.m2/repository</localRepository>
<servers>
<server>
<id>server-1</id>
<username>username1</username>
<password>password1</password>
</server>
<server>
<id>server-2</id>
<username>username2</username>
<password>password2</password>
</server>
</servers>

</settings>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

<settings>
<localRepository>${user.home}/.m2/repository</localRepository>
<servers>
<server>
<id>server-1</id>
<ids>
<id>server-11</id>
<id>server-12</id>
</ids>
<username>username1</username>
<password>password1</password>
</server>
<server>
<id>server-2</id>
<ids>
<id>server-21</id>
</ids>
<username>username2</username>
<password>password2</password>
</server>
<server>
<id>server-3</id>
<username>username3</username>
<password>password3</password>
</server>
</servers>

</settings>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

<settings>
<localRepository>${user.home}/.m2/repository</localRepository>
<servers>
<server>
<id>server-1</id>
<ids>
<id>server-2</id>
</ids>
<username>username1</username>
<password>password1</password>
</server>
<server>
<id>server-2</id>
<username>username2</username>
<password>password2</password>
</server>
</servers>

</settings>
2 changes: 1 addition & 1 deletion maven-settings/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ under the License.
<groupId>org.codehaus.modello</groupId>
<artifactId>modello-maven-plugin</artifactId>
<configuration>
<version>1.2.0</version>
<version>1.3.0</version>
<models>
<model>src/main/mdo/settings.mdo</model>
</models>
Expand Down
13 changes: 12 additions & 1 deletion maven-settings/src/main/mdo/settings.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,18 @@
]]>
</description>
</field>
</fields>
<field>
<name>ids</name>
<version>1.3.0+</version>
<description>
List of additional ids for server.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the listed ids have separate entries? Are they merged? Which ones takes precedence?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good one.

</description>
<association>
<type>String</type>
<multiplicity>*</multiplicity>
</association>
</field>
</fields>
</class>
<class>
<name>Mirror</name>
Expand Down