Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
3 changes: 3 additions & 0 deletions api/maven-api-settings/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ under the License.
<groupId>org.codehaus.modello</groupId>
<artifactId>modello-maven-plugin</artifactId>
<configuration>
<pluralExceptions>
<aliases>alias</aliases>
</pluralExceptions>
<version>2.0.0</version>
<velocityBasedir>${project.basedir}/../../src/mdo</velocityBasedir>
<models>
Expand Down
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>aliases</name>
<version>1.3.0+</version>
<description>
List of additional server aliases. For each alias, an additional server will be generated by copying the entire configuration, but using a different ID.
This is useful when the same credentials should be used for multiple servers. </description>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Trailing whitespace

<association>
<type>String</type>
<multiplicity>*</multiplicity>
</association>
</field>
</fields>
</class>
<class>
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.getAliases().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)
.aliases(List.of())
.build());
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks very ugly. It is the clien't responsibility to traverse the Settings object, no?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It is the core of change.

Based on additional tag - aliases we create a next server instance in memory on server list.
So all others plugins tools will see destination list of servers original declared in settings and created based on aliases tag.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I do remember that Modello allows to add Java code to Settings. Why can't we inject/improve the code in the model directly?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

so you propose to override a getServers method directly in modelo?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, correct.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

ok, I will check if it will be easier?

Copy link
Copy Markdown
Member Author

@slawekjaranowski slawekjaranowski Apr 14, 2026

Choose a reason for hiding this comment

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

@michael-o
It will be impossible (in easy way) to implement in code settings.mdo, we use velocity templates to generate code for 4.x and for 3.x.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Share the spot where this is done. Modello made it possible :-(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Share the spot where this is done. Modello made it possible :-(

There is two templates for generating output from modello files:
https://github.com/apache/maven/blob/master/src/mdo/model.vm
https://github.com/apache/maven/blob/master/src/mdo/model-v3.vm

I see some of code in settings.mdo so will look again

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't see a way to provide such code in settings.mdo

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@
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.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
*/
Expand All @@ -32,17 +38,101 @@ 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);

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 result;
}

@Test
void testCompleteWiring() throws Exception {
Settings settings = execute("simple").getEffectiveSettings();

String localRepository = settings.getLocalRepository();
assertTrue(localRepository.equals("/home/user/.m2/repository")
|| localRepository.endsWith("\\home\\user\\.m2\\repository"));
}

@Test
void testSettingsWithServers() throws Exception {
Settings settings = execute("settings-servers-1").getEffectiveSettings();

List<Server> servers = settings.getDelegate().getServers();
assertEquals(2, servers.size());

Server server1 = getServerById(servers, "server-1");
assertEquals("username1", server1.getUsername());
assertEquals("password1", server1.getPassword());

Server server2 = getServerById(servers, "server-2");
assertEquals("username2", server2.getUsername());
assertEquals("password2", server2.getPassword());
}

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

List<Server> servers = settings.getDelegate().getServers();
assertEquals(6, servers.size());

Server server1 = getServerById(servers, "server-1");
assertEquals("username1", server1.getUsername());
assertEquals("password1", server1.getPassword());
assertEquals(List.of("server-11", "server-12"), server1.getAliases());

Server server11 = getServerById(servers, "server-11");
assertEquals("username1", server11.getUsername());
assertEquals("password1", server11.getPassword());
assertTrue(server11.getAliases().isEmpty());

Server server12 = getServerById(servers, "server-12");
assertEquals("username1", server12.getUsername());
assertEquals("password1", server12.getPassword());
assertTrue(server11.getAliases().isEmpty());

Server server2 = getServerById(servers, "server-2");
assertEquals("username2", server2.getUsername());
assertEquals("password2", server2.getPassword());
assertEquals(List.of("server-21"), server2.getAliases());

Server server21 = getServerById(servers, "server-21");
assertEquals("username2", server21.getUsername());
assertEquals("password2", server21.getPassword());
assertTrue(server21.getAliases().isEmpty());

Server server3 = getServerById(servers, "server-3");
assertEquals("username3", server3.getUsername());
assertEquals("password3", server3.getPassword());
assertTrue(server3.getAliases().isEmpty());
}

private Server getServerById(List<Server> servers, String id) {
return servers.stream()
.filter(s -> s.getId().equals(id))
.findFirst()
.orElseThrow(
() -> new IllegalStateException("Server with id " + id + " not found on list: " + servers));
}

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

List<SettingsProblem> problems = result.getProblems();
assertEquals(1, problems.size());
assertEquals(
"'servers.server.id' must be unique but found duplicate server with id server-2",
problems.get(0).getMessage());
}
}
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>
<aliases>
<alias>server-11</alias>
<alias>server-12</alias>
</aliases>
<username>username1</username>
<password>password1</password>
</server>
<server>
<id>server-2</id>
<aliases>
<alias>server-21</alias>
</aliases>
<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>
<aliases>
<alias>server-2</alias>
</aliases>
<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.getAliases().stream().map(id -> serverAlias(server, id))))
.toList();
}

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

private Settings interpolate(
Settings settings, SettingsBuilderRequest request, ProblemCollector<BuilderProblem> problems) {
UnaryOperator<String> src;
Expand Down Expand Up @@ -348,7 +363,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