Skip to content
Open
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
11 changes: 11 additions & 0 deletions api/maven-api-settings/src/main/mdo/settings.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,17 @@
Extra configuration for the transport layer.
</description>
</field>
<field>
<name>ids</name>
<version>1.3.0+</version>
<description>
List of additional ids for server.
</description>
<association>
<type>String</type>
<multiplicity>*</multiplicity>
</association>
</field>
</fields>
</class>
<class>
Expand Down
5 changes: 5 additions & 0 deletions compat/maven-settings-builder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ under the License.
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
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 @@ -181,6 +183,7 @@ private Settings readSettings(
return new Settings();
}

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

return settings;
Expand Down Expand Up @@ -251,4 +254,18 @@ public Object execute(String expression, Object value) {

return result;
}

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))))
.toList();
}

private Server serverAlias(Server server, String id) {
return new Server(org.apache.maven.api.settings.Server.newBuilder(server.getDelegate(), true)
.id(id)
.ids(List.of())
.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@
package org.apache.maven.settings.building;

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

import org.apache.maven.api.settings.Server;
import org.apache.maven.settings.Settings;
import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.assertj.core.api.Assertions.assertThat;

/**
*/
Expand All @@ -32,17 +37,110 @@ private File getSettings(String name) {
return new File("src/test/resources/settings/factory/" + name + ".xml").getAbsoluteFile();
}

@Test
void testCompleteWiring() throws Exception {
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
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
void testSettingsWithServers() throws Exception {
Settings settings = assertThat(execute("settings-servers-1"))
.extracting(SettingsBuildingResult::getEffectiveSettings)
.actual();

assertThat(settings.getDelegate().getServers())
.hasSize(2)
.usingRecursiveFieldByFieldElementComparator(RecursiveComparisonConfiguration.builder()
.withIgnoredFields("locations")
.build())
.containsExactlyInAnyOrder(
Server.newBuilder()
.id("server-1")
.username("username1")
.password("password1")
.build(),
Server.newBuilder()
.id("server-2")
.username("username2")
.password("password2")
.build());
}

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

assertThat(settings.getDelegate().getServers())
.hasSize(6)
.usingRecursiveFieldByFieldElementComparator(RecursiveComparisonConfiguration.builder()
.withIgnoredFields("locations")
.build())
.containsExactlyInAnyOrder(
Server.newBuilder()
.id("server-1")
.username("username1")
.password("password1")
.ids(List.of("server-11", "server-12"))
.build(),
Server.newBuilder()
.id("server-11")
.username("username1")
.password("password1")
.build(),
Server.newBuilder()
.id("server-12")
.username("username1")
.password("password1")
.build(),
Server.newBuilder()
.id("server-2")
.username("username2")
.password("password2")
.ids(List.of("server-21"))
.build(),
Server.newBuilder()
.id("server-21")
.username("username2")
.password("password2")
.build(),
Server.newBuilder()
.id("server-3")
.username("username3")
.password("password3")
.build());
}

@Test
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 compat/maven-settings/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ under the License.
<model>src/main/mdo/settings.mdo</model>
</models>
<params>
<param>forcedIOModelVersion=1.2.0</param>
<param>forcedIOModelVersion=1.3.0</param>
<param>packageModelV3=org.apache.maven.settings</param>
<param>packageModelV4=org.apache.maven.api.settings</param>
<param>packageToolV4=org.apache.maven.settings.v4</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;

import org.apache.maven.api.Constants;
import org.apache.maven.api.ProtoSession;
Expand Down Expand Up @@ -62,7 +63,6 @@

/**
* Builds the effective settings from a user settings file and/or a global settings file.
*
*/
@Named
public class DefaultSettingsBuilder implements SettingsBuilder {
Expand Down Expand Up @@ -203,6 +203,10 @@ private Settings readSettings(
settings = interpolate(settings, request, problems);
settings = decrypt(settingsSource, settings, request, problems);

if (!isProjectSettings) {
settings = settings.withServers(serversByIds(settings.getServers()));
}

settingsValidator.validate(settings, isProjectSettings, problems);

if (isProjectSettings) {
Expand All @@ -228,6 +232,17 @@ private Settings readSettings(
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))))
.toList();
}

private Server serverAlias(Server server, String id) {
return Server.newBuilder(server, true).id(id).ids(List.of()).build();
}

private Settings interpolate(
Settings settings, SettingsBuilderRequest request, ProblemCollector<BuilderProblem> problems) {
UnaryOperator<String> src;
Expand Down Expand Up @@ -332,7 +347,6 @@ public org.apache.maven.api.model.Profile convert(Profile profile) {

/**
* Collects the output of the settings builder.
*
*/
static class DefaultSettingsBuilderResult implements SettingsBuilderResult {

Expand Down
Loading