Skip to content

Commit 7230d9e

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 ffd30a3 commit 7230d9e

File tree

15 files changed

+535
-17
lines changed

15 files changed

+535
-17
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@
181181
</field>
182182
<field xdoc.separator="blank">
183183
<name>repositories</name>
184+
<!-- 2.0.0+ ? -->
184185
<version>1.3.0+</version>
185186
<description>
186187
The lists of the remote repositories.
@@ -192,6 +193,7 @@
192193
</field>
193194
<field>
194195
<name>pluginRepositories</name>
196+
<!-- 2.0.0+ ? -->
195197
<version>1.3.0+</version>
196198
<description>
197199
The lists of the remote repositories for discovering plugins.
@@ -529,6 +531,17 @@
529531
Extra configuration for the transport layer.
530532
</description>
531533
</field>
534+
<field>
535+
<name>ids</name>
536+
<version>1.3.0+</version>
537+
<description>
538+
List of additional ids for server.
539+
</description>
540+
<association>
541+
<type>String</type>
542+
<multiplicity>*</multiplicity>
543+
</association>
544+
</field>
532545
</fields>
533546
</class>
534547
<class>

compat/maven-settings-builder/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ under the License.
7878
<artifactId>junit-jupiter-api</artifactId>
7979
<scope>test</scope>
8080
</dependency>
81+
<dependency>
82+
<groupId>org.assertj</groupId>
83+
<artifactId>assertj-core</artifactId>
84+
<scope>test</scope>
85+
</dependency>
8186
</dependencies>
8287

8388
<build>

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
import java.io.IOException;
2727
import java.io.StringReader;
2828
import java.io.StringWriter;
29+
import java.util.ArrayList;
2930
import java.util.Collections;
3031
import java.util.List;
3132
import java.util.Map;
3233

3334
import org.apache.maven.building.FileSource;
3435
import org.apache.maven.building.Source;
36+
import org.apache.maven.settings.Server;
3537
import org.apache.maven.settings.Settings;
3638
import org.apache.maven.settings.TrackableBase;
3739
import org.apache.maven.settings.io.SettingsParseException;
@@ -181,6 +183,7 @@ private Settings readSettings(
181183
return new Settings();
182184
}
183185

186+
settings.setServers(serversByIds(settings.getServers()));
184187
settingsValidator.validate(settings, problems);
185188

186189
return settings;
@@ -251,4 +254,24 @@ public Object execute(String expression, Object value) {
251254

252255
return result;
253256
}
257+
258+
private List<Server> serversByIds(List<Server> servers) {
259+
260+
if (servers.stream().allMatch(server -> server.getIds().isEmpty())) {
261+
return servers;
262+
}
263+
264+
ArrayList<Server> result = new ArrayList<>(servers);
265+
266+
servers.stream().filter(server -> !server.getIds().isEmpty()).forEach(server -> server.getIds()
267+
.forEach(id -> result.add(newServer(server, id))));
268+
return result;
269+
}
270+
271+
private Server newServer(Server server, String id) {
272+
return new Server(org.apache.maven.api.settings.Server.newBuilder(server.getDelegate(), false)
273+
.id(id)
274+
.ids(Collections.emptyList())
275+
.build());
276+
}
254277
}

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

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

2121
import java.io.File;
22+
import java.util.List;
23+
import java.util.Properties;
2224

25+
import org.apache.maven.api.settings.Server;
26+
import org.apache.maven.settings.Settings;
27+
import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;
2328
import org.junit.jupiter.api.Test;
2429

25-
import static org.junit.jupiter.api.Assertions.assertNotNull;
30+
import static org.assertj.core.api.Assertions.assertThat;
2631

2732
/**
2833
*/
@@ -32,17 +37,115 @@ private File getSettings(String name) {
3237
return new File("src/test/resources/settings/factory/" + name + ".xml").getAbsoluteFile();
3338
}
3439

35-
@Test
36-
void testCompleteWiring() throws Exception {
40+
private org.apache.maven.settings.Server asServer(Server delegate) {
41+
return new org.apache.maven.settings.Server(delegate);
42+
}
43+
44+
SettingsBuildingResult execute(String settingsName) throws Exception {
45+
Properties properties = new Properties();
46+
properties.setProperty("user.home", "/home/user");
47+
3748
SettingsBuilder builder = new DefaultSettingsBuilderFactory().newInstance();
38-
assertNotNull(builder);
49+
assertThat(builder).isNotNull();
3950

4051
DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
41-
request.setSystemProperties(System.getProperties());
42-
request.setUserSettingsFile(getSettings("simple"));
52+
request.setSystemProperties(properties);
53+
request.setUserSettingsFile(getSettings(settingsName));
4354

4455
SettingsBuildingResult result = builder.build(request);
45-
assertNotNull(result);
46-
assertNotNull(result.getEffectiveSettings());
56+
return assertThat(result).isNotNull().actual();
57+
}
58+
59+
@Test
60+
void testCompleteWiring() throws Exception {
61+
Settings settings = assertThat(execute("simple"))
62+
.extracting(SettingsBuildingResult::getEffectiveSettings)
63+
.actual();
64+
65+
assertThat(settings.getLocalRepository()).isEqualTo("/home/user/.m2/repository");
66+
}
67+
68+
@Test
69+
void testSettingsWithServers() throws Exception {
70+
Settings settings = assertThat(execute("settings-servers-1"))
71+
.extracting(SettingsBuildingResult::getEffectiveSettings)
72+
.actual();
73+
74+
assertThat(settings.getLocalRepository()).isEqualTo("/home/user/.m2/repository");
75+
76+
assertThat(settings.getDelegate().getServers())
77+
.hasSize(2)
78+
.usingRecursiveFieldByFieldElementComparator(RecursiveComparisonConfiguration.builder()
79+
.withIgnoredFields("locations")
80+
.build())
81+
.containsExactlyInAnyOrder(
82+
Server.newBuilder()
83+
.id("server-1")
84+
.username("username1")
85+
.password("password1")
86+
.build(),
87+
Server.newBuilder()
88+
.id("server-2")
89+
.username("username2")
90+
.password("password2")
91+
.build());
92+
}
93+
94+
@Test
95+
void testSettingsWithServersAndAliases() throws Exception {
96+
Settings settings = assertThat(execute("settings-servers-2"))
97+
.extracting(SettingsBuildingResult::getEffectiveSettings)
98+
.actual();
99+
100+
assertThat(settings.getLocalRepository()).isEqualTo("/home/user/.m2/repository");
101+
102+
assertThat(settings.getDelegate().getServers())
103+
.hasSize(6)
104+
.usingRecursiveFieldByFieldElementComparator(RecursiveComparisonConfiguration.builder()
105+
.withIgnoredFields("locations")
106+
.build())
107+
.containsExactlyInAnyOrder(
108+
Server.newBuilder()
109+
.id("server-1")
110+
.username("username1")
111+
.password("password1")
112+
.ids(List.of("server-11", "server-12"))
113+
.build(),
114+
Server.newBuilder()
115+
.id("server-11")
116+
.username("username1")
117+
.password("password1")
118+
.build(),
119+
Server.newBuilder()
120+
.id("server-12")
121+
.username("username1")
122+
.password("password1")
123+
.build(),
124+
Server.newBuilder()
125+
.id("server-2")
126+
.username("username2")
127+
.password("password2")
128+
.ids(List.of("server-21"))
129+
.build(),
130+
Server.newBuilder()
131+
.id("server-21")
132+
.username("username2")
133+
.password("password2")
134+
.build(),
135+
Server.newBuilder()
136+
.id("server-3")
137+
.username("username3")
138+
.password("password3")
139+
.build());
140+
}
141+
142+
@Test
143+
void testSettingsWithDuplicateServersIds() throws Exception {
144+
SettingsBuildingResult result = execute("settings-servers-3");
145+
146+
assertThat(result.getProblems())
147+
.hasSize(1)
148+
.extracting(SettingsProblem::getMessage)
149+
.containsExactly("'servers.server.id' must be unique but found duplicate server with id server-2");
47150
}
48151
}
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>

compat/maven-settings/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ under the License.
7575
<model>src/main/mdo/settings.mdo</model>
7676
</models>
7777
<params>
78+
<!-- 1.3.0 ? -->
7879
<param>forcedIOModelVersion=1.2.0</param>
7980
<param>packageModelV3=org.apache.maven.settings</param>
8081
<param>packageModelV4=org.apache.maven.api.settings</param>

0 commit comments

Comments
 (0)