Skip to content

Commit 3e53a6e

Browse files
[MNG-5913] Allow defining aliases for existing server configurations in settings.xml
Add the next tag ids to the server in settings.xml Additional server will be created in memory by SettingsBuilder, so the generated configuration should be transparent to other as Settings#getServers() will return complete list.
1 parent 6559599 commit 3e53a6e

File tree

8 files changed

+252
-14
lines changed

8 files changed

+252
-14
lines changed

maven-settings-builder/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ under the License.
6363
<groupId>org.codehaus.plexus</groupId>
6464
<artifactId>plexus-sec-dispatcher</artifactId>
6565
</dependency>
66+
67+
<dependency>
68+
<groupId>org.assertj</groupId>
69+
<artifactId>assertj-core</artifactId>
70+
<version>3.27.3</version>
71+
<scope>test</scope>
72+
</dependency>
6673
</dependencies>
6774

6875
<build>

maven-settings-builder/src/main/java/org/apache/maven/settings/building/DefaultSettingsBuilder.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@
2929
import java.util.Collections;
3030
import java.util.List;
3131
import java.util.Map;
32+
import java.util.stream.Collectors;
33+
import java.util.stream.Stream;
3234

3335
import org.apache.maven.building.FileSource;
3436
import org.apache.maven.building.Source;
37+
import org.apache.maven.settings.Server;
3538
import org.apache.maven.settings.Settings;
3639
import org.apache.maven.settings.TrackableBase;
3740
import org.apache.maven.settings.io.SettingsParseException;
@@ -91,10 +94,10 @@ public SettingsBuildingResult build(SettingsBuildingRequest request) throws Sett
9194

9295
Source globalSettingsSource =
9396
getSettingsSource(request.getGlobalSettingsFile(), request.getGlobalSettingsSource());
94-
Settings globalSettings = readSettings(globalSettingsSource, request, problems);
97+
Settings globalSettings = readSettings(globalSettingsSource, problems);
9598

9699
Source userSettingsSource = getSettingsSource(request.getUserSettingsFile(), request.getUserSettingsSource());
97-
Settings userSettings = readSettings(userSettingsSource, request, problems);
100+
Settings userSettings = readSettings(userSettingsSource, problems);
98101

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

@@ -139,8 +142,7 @@ private Source getSettingsSource(File settingsFile, Source settingsSource) {
139142
return null;
140143
}
141144

142-
private Settings readSettings(
143-
Source settingsSource, SettingsBuildingRequest request, DefaultSettingsProblemCollector problems) {
145+
private Settings readSettings(Source settingsSource, DefaultSettingsProblemCollector problems) {
144146
if (settingsSource == null) {
145147
return new Settings();
146148
}
@@ -180,11 +182,26 @@ private Settings readSettings(
180182
return new Settings();
181183
}
182184

185+
settings.setServers(serversByIds(settings.getServers()));
183186
settingsValidator.validate(settings, problems);
184187

185188
return settings;
186189
}
187190

191+
private List<Server> serversByIds(List<Server> servers) {
192+
return servers.stream()
193+
.flatMap(server -> Stream.concat(
194+
Stream.of(server), server.getIds().stream().map(id -> serverAlias(server, id))))
195+
.collect(Collectors.toList());
196+
}
197+
198+
private Server serverAlias(Server server, String id) {
199+
Server serverClone = server.clone();
200+
serverClone.setId(id);
201+
serverClone.setIds(Collections.emptyList());
202+
return serverClone;
203+
}
204+
188205
private Settings interpolate(
189206
Settings settings, SettingsBuildingRequest request, SettingsProblemCollector problems) {
190207
StringWriter writer = new StringWriter(1024 * 4);

maven-settings-builder/src/test/java/org/apache/maven/settings/building/DefaultSettingsBuilderFactoryTest.java

Lines changed: 85 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,105 @@
1919
package org.apache.maven.settings.building;
2020

2121
import java.io.File;
22+
import java.util.Arrays;
23+
import java.util.Collections;
24+
import java.util.List;
25+
import java.util.Properties;
2226

23-
import junit.framework.TestCase;
27+
import org.apache.maven.settings.Server;
28+
import org.apache.maven.settings.Settings;
29+
import org.junit.Test;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
2432

2533
/**
2634
* @author Benjamin Bentmann
2735
*/
28-
public class DefaultSettingsBuilderFactoryTest extends TestCase {
36+
public class DefaultSettingsBuilderFactoryTest {
2937

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

34-
public void testCompleteWiring() throws Exception {
42+
private SettingsBuildingResult execute(String settingsName) throws Exception {
43+
Properties properties = new Properties();
44+
properties.setProperty("user.home", "/home/user");
45+
3546
SettingsBuilder builder = new DefaultSettingsBuilderFactory().newInstance();
36-
assertNotNull(builder);
47+
assertThat(builder).isNotNull();
3748

3849
DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
39-
request.setSystemProperties(System.getProperties());
40-
request.setUserSettingsFile(getSettings("simple"));
50+
request.setSystemProperties(properties);
51+
request.setUserSettingsFile(getSettings(settingsName));
4152

4253
SettingsBuildingResult result = builder.build(request);
43-
assertNotNull(result);
44-
assertNotNull(result.getEffectiveSettings());
54+
return assertThat(result).isNotNull().actual();
55+
}
56+
57+
@Test
58+
public void testCompleteWiring() throws Exception {
59+
Settings settings = assertThat(execute("simple"))
60+
.extracting(SettingsBuildingResult::getEffectiveSettings)
61+
.actual();
62+
63+
assertThat(settings.getLocalRepository())
64+
.satisfiesAnyOf(
65+
repo -> assertThat(repo).isEqualTo("/home/user/.m2/repository"),
66+
repo -> assertThat(repo).endsWith("\\home\\user\\.m2\\repository"));
67+
}
68+
69+
@Test
70+
public void testSettingsWithServers() throws Exception {
71+
Settings settings = assertThat(execute("settings-servers-1"))
72+
.extracting(SettingsBuildingResult::getEffectiveSettings)
73+
.actual();
74+
75+
assertThat(settings.getServers())
76+
.hasSize(2)
77+
.usingRecursiveFieldByFieldElementComparator()
78+
.containsExactlyInAnyOrder(
79+
aServer("server-1", "username1", "password1"), aServer("server-2", "username2", "password2"));
80+
}
81+
82+
@Test
83+
public void testSettingsWithServersAndAliases() throws Exception {
84+
Settings settings = assertThat(execute("settings-servers-2"))
85+
.extracting(SettingsBuildingResult::getEffectiveSettings)
86+
.actual();
87+
88+
assertThat(settings.getServers())
89+
.hasSize(6)
90+
.usingRecursiveFieldByFieldElementComparator()
91+
.containsExactlyInAnyOrder(
92+
aServer("server-1", "username1", "password1", Arrays.asList("server-11", "server-12")),
93+
aServer("server-11", "username1", "password1"),
94+
aServer("server-12", "username1", "password1"),
95+
aServer("server-2", "username2", "password2", Collections.singletonList("server-21")),
96+
aServer("server-21", "username2", "password2"),
97+
aServer("server-3", "username3", "password3"));
98+
}
99+
100+
private Server aServer(String id, String username, String password) {
101+
Server server = new Server();
102+
server.setId(id);
103+
server.setUsername(username);
104+
server.setPassword(password);
105+
return server;
106+
}
107+
108+
private Server aServer(String id, String username, String password, List<String> ids) {
109+
Server server = aServer(id, username, password);
110+
server.setIds(ids);
111+
return server;
112+
}
113+
114+
@Test
115+
public void testSettingsWithDuplicateServersIds() throws Exception {
116+
SettingsBuildingResult result = execute("settings-servers-3");
117+
118+
assertThat(result.getProblems())
119+
.hasSize(1)
120+
.extracting(SettingsProblem::getMessage)
121+
.containsExactly("'servers.server.id' must be unique but found duplicate server with id server-2");
45122
}
46123
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
Licensed to the Apache Software Foundation (ASF) under one
5+
or more contributor license agreements. See the NOTICE file
6+
distributed with this work for additional information
7+
regarding copyright ownership. The ASF licenses this file
8+
to you under the Apache License, Version 2.0 (the
9+
"License"); you may not use this file except in compliance
10+
with the License. You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing,
15+
software distributed under the License is distributed on an
16+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
KIND, either express or implied. See the License for the
18+
specific language governing permissions and limitations
19+
under the License.
20+
-->
21+
22+
<settings>
23+
<localRepository>${user.home}/.m2/repository</localRepository>
24+
<servers>
25+
<server>
26+
<id>server-1</id>
27+
<username>username1</username>
28+
<password>password1</password>
29+
</server>
30+
<server>
31+
<id>server-2</id>
32+
<username>username2</username>
33+
<password>password2</password>
34+
</server>
35+
</servers>
36+
37+
</settings>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
Licensed to the Apache Software Foundation (ASF) under one
5+
or more contributor license agreements. See the NOTICE file
6+
distributed with this work for additional information
7+
regarding copyright ownership. The ASF licenses this file
8+
to you under the Apache License, Version 2.0 (the
9+
"License"); you may not use this file except in compliance
10+
with the License. You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing,
15+
software distributed under the License is distributed on an
16+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
KIND, either express or implied. See the License for the
18+
specific language governing permissions and limitations
19+
under the License.
20+
-->
21+
22+
<settings>
23+
<localRepository>${user.home}/.m2/repository</localRepository>
24+
<servers>
25+
<server>
26+
<id>server-1</id>
27+
<ids>
28+
<id>server-11</id>
29+
<id>server-12</id>
30+
</ids>
31+
<username>username1</username>
32+
<password>password1</password>
33+
</server>
34+
<server>
35+
<id>server-2</id>
36+
<ids>
37+
<id>server-21</id>
38+
</ids>
39+
<username>username2</username>
40+
<password>password2</password>
41+
</server>
42+
<server>
43+
<id>server-3</id>
44+
<username>username3</username>
45+
<password>password3</password>
46+
</server>
47+
</servers>
48+
49+
</settings>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
Licensed to the Apache Software Foundation (ASF) under one
5+
or more contributor license agreements. See the NOTICE file
6+
distributed with this work for additional information
7+
regarding copyright ownership. The ASF licenses this file
8+
to you under the Apache License, Version 2.0 (the
9+
"License"); you may not use this file except in compliance
10+
with the License. You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing,
15+
software distributed under the License is distributed on an
16+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
KIND, either express or implied. See the License for the
18+
specific language governing permissions and limitations
19+
under the License.
20+
-->
21+
22+
<settings>
23+
<localRepository>${user.home}/.m2/repository</localRepository>
24+
<servers>
25+
<server>
26+
<id>server-1</id>
27+
<ids>
28+
<id>server-2</id>
29+
</ids>
30+
<username>username1</username>
31+
<password>password1</password>
32+
</server>
33+
<server>
34+
<id>server-2</id>
35+
<username>username2</username>
36+
<password>password2</password>
37+
</server>
38+
</servers>
39+
40+
</settings>

maven-settings/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ under the License.
4444
<groupId>org.codehaus.modello</groupId>
4545
<artifactId>modello-maven-plugin</artifactId>
4646
<configuration>
47-
<version>1.2.0</version>
47+
<version>1.3.0</version>
4848
<models>
4949
<model>src/main/mdo/settings.mdo</model>
5050
</models>

maven-settings/src/main/mdo/settings.mdo

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,18 @@
581581
]]>
582582
</description>
583583
</field>
584-
</fields>
584+
<field>
585+
<name>ids</name>
586+
<version>1.3.0+</version>
587+
<description>
588+
List of additional ids for server.
589+
</description>
590+
<association>
591+
<type>String</type>
592+
<multiplicity>*</multiplicity>
593+
</association>
594+
</field>
595+
</fields>
585596
</class>
586597
<class>
587598
<name>Mirror</name>

0 commit comments

Comments
 (0)