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
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
12 changes: 12 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,18 @@
Extra configuration for the transport layer.
</description>
</field>
<field>
<name>aliases</name>
<version>1.3.0+</version>
Comment thread
slawekjaranowski marked this conversation as resolved.
<description>List of additional server aliases. For each alias, an additional server entry will be generated
with the same configuration as the original one, but with the ID replaced by the alias
and with an empty aliases list.
This is useful when the same credentials should be used for multiple servers.</description>
<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 @@ -26,12 +26,15 @@
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
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 @@ -182,6 +185,7 @@ private Settings readSettings(
}

settingsValidator.validate(settings, problems);
settings.setServers(new ArrayList<>(serversByIds(settings.getServers())));

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

return result;
}

private List<Server> serversByIds(List<Server> servers) {
return servers.stream()
.flatMap(server -> Stream.concat(
Comment thread
slawekjaranowski marked this conversation as resolved.
Stream.of(server), server.getAliases().stream().map(id -> serverAlias(server, id))))
.toList();
}

private Server serverAlias(Server server, String id) {
Comment thread
slawekjaranowski marked this conversation as resolved.
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.

@slawekjaranowski slawekjaranowski Apr 14, 2026

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.

@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

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.

@michael-o any other remarks as implementation in modelo is not easy

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,23 @@ public void validate(Settings settings, SettingsProblemCollector problems) {
"must be unique but found duplicate server with id " + server.getId());
}
}

for (int i = 0; i < servers.size(); i++) {
Server server = servers.get(i);
for (int a = 0; a < server.getAliases().size(); a++) {
String alias = server.getAliases().get(a);
validateStringNotEmpty(
problems, "servers.server[" + i + "].aliases[" + a + "]", alias, server.getId());
if (!serverIds.add(alias)) {
addViolation(
problems,
Severity.WARNING,
"servers.server[" + i + "].aliases[" + a + "]",
server.getId(),
"must be unique across all server ids and aliases but found duplicate alias " + alias);
}
}
}
}

List<Mirror> mirrors = settings.getMirrors();
Expand Down
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(server12.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[0].aliases[0]' for server-1 must be unique across all server ids and aliases but found duplicate alias server-2",
problems.get(0).getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,58 @@ void testValidateProxy() throws Exception {
assertContains(problems.messages.get(0), "'proxies.proxy.host' for default is missing");
}

@Test
void testValidateServerIdAlias() {
Settings settings = new Settings();
Server server = new Server();
server.setId("server-1");
server.addAlias("server-1");
settings.addServer(server);

SimpleProblemCollector problems = new SimpleProblemCollector();
validator.validate(settings, problems);
assertEquals(1, problems.messages.size());
assertContains(
problems.messages.get(0),
"'servers.server[0].aliases[0]' for server-1 must be unique across all server ids and aliases but found duplicate alias server-1");
}

@Test
void testMultipleUsageOfAliases() {
Settings settings = new Settings();

Server server1 = new Server();
server1.setId("server-1");
server1.addAlias("alias-1");
settings.addServer(server1);

Server server2 = new Server();
server2.setId("server-2");
server2.addAlias("alias-1");
settings.addServer(server2);

SimpleProblemCollector problems = new SimpleProblemCollector();
validator.validate(settings, problems);
assertEquals(1, problems.messages.size());
assertContains(
problems.messages.get(0),
"'servers.server[1].aliases[0]' for server-2 must be unique across all server ids and aliases but found duplicate alias alias-1");
}

@Test
void testValidateServerIdAliasesWithEmptyValue() {
Settings settings = new Settings();
Server server = new Server();
server.setId("server-1");
server.addAlias("");
settings.addServer(server);

SimpleProblemCollector problems = new SimpleProblemCollector();
validator.validate(settings, problems);
assertEquals(1, problems.messages.size());
assertContains(problems.messages.get(0), "'servers.server[0].aliases[0]' for server-1 is missing");
}

private static class SimpleProblemCollector implements SettingsProblemCollector {

List<String> messages = new ArrayList<>();
Expand Down
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>
Loading
Loading