8000 [MNG-8558] Switch toolchain support for v4 API (#2084) · apache/maven@5b77395 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5b77395

Browse files
authored
[MNG-8558] Switch toolchain support for v4 API (#2084)
1 parent f2bc813 commit 5b77395

File tree

42 files changed

+893
-707
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+893
-707
lines changed

api/maven-api-core/src/main/java/org/apache/maven/api/Session.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.apache.maven.api.services.DependencyCoordinatesFactory;
3535
import org.apache.maven.api.services.VersionResolverException;
3636
import org.apache.maven.api.settings.Settings;
37+
import org.apache.maven.api.toolchain.ToolchainModel;
3738

3839
/**
3940
* The session to install / deploy / resolve artifacts and dependencies.
@@ -60,6 +61,14 @@ public interface Session extends ProtoSession {
6061
@Nonnull
6162
Settings getSettings();
6263

64+
/**
65+
* Retrieves toolchain models that have been explicitly configured.
66+
*
67+
* @return the toolchain models
68+
*/
69+
@Nonnull
70+
Collection<ToolchainModel> getToolchains();
71+
6372
/**
6473
* Retrieves the local repository associated with this session.
6574
*

api/maven-api-core/src/main/java/org/apache/maven/api/Toolchain.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Map;
2222

2323
import org.apache.maven.api.annotations.Experimental;
24+
import org.apache.maven.api.toolchain.ToolchainModel;
2425

2526
/**
2627
* Represents a toolchain in the Maven build system.
@@ -70,6 +71,13 @@ public interface Toolchain {
7071
*/
7172
String getType();
7273

74+
/**
75+
* Gets the underlying toolchain model.
76+
*
77+
* @return the toolchain model
78+
*/
79+
ToolchainModel getModel();
80+
7381
/**
7482
* Gets the platform tool executable.
7583
*

api/maven-api-core/src/main/java/org/apache/maven/api/services/ToolchainFactory.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,41 @@
1818
*/
1919
package org.apache.maven.api.services;
2020

21+
import java.util.Optional;
22+
23+
import org.apache.maven.api.Toolchain;
2124
import org.apache.maven.api.annotations.Consumer;
2225
import org.apache.maven.api.annotations.Experimental;
26+
import org.apache.maven.api.annotations.Nonnull;
27+
import org.apache.maven.api.toolchain.ToolchainModel;
2328

2429
/**
30+
* Factory interface for creating toolchain instances from configuration models.
31+
*
32+
* <p>This factory is responsible for instantiating concrete toolchain implementations
33+
* based on toolchain model configurations or default settings.</p>
34+
*
2535
* @since 4.0.0
2636
*/
2737
@Experimental
2838
@Consumer
2939
public interface ToolchainFactory {
30-
// TODO: implement ToolchainFactory
40+
/**
41+
* Creates a toolchain instance from the provided model configuration.
42+
*
43+
* @param model The toolchain configuration model
44+
* @return A configured toolchain instance
45+
* @throws ToolchainFactoryException if toolchain creation fails
46+
*/
47+
@Nonnull
48+
Toolchain createToolchain(@Nonnull ToolchainModel model) throws ToolchainFactoryException;
49+
50+
/**
51+
* Creates a default toolchain instance using system defaults.
52+
*
53+
* @return Optional containing the default toolchain if available
54+
* @throws ToolchainFactoryException if default toolchain creation fails
55+
*/
56+
@Nonnull
57+
Optional<Toolchain> createDefaultToolchain() throws ToolchainFactoryException;
3158
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.api.services;
20+
21+
/**
22+
* Exception thrown when toolchain factory operations fail.
23+
*
24+
* <p>This exception wraps errors that occur during toolchain creation or initialization.</p>
25+
*/
26+
public class ToolchainFactoryException extends MavenException {
27+
28+
public ToolchainFactoryException(String message) {
29+
super(message);
30+
}
31+
32+
public ToolchainFactoryException(String message, Throwable cause) {
33+
super(message, cause);
34+
}
35+
36+
public ToolchainFactoryException(Throwable cause) {
37+
super(cause);
38+
}
39+
}

api/maven-api-core/src/main/java/org/apache/maven/api/services/ToolchainManager.java

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,50 +29,63 @@
2929
import org.apache.maven.api.annotations.Nonnull;
3030

3131
/**
32-
* Service to manage {@link Toolchain}s.
32+
* Service interface for managing Maven toolchains, which provide abstraction for different
33+
* build tools and environments.
34+
*
35+
* <p>A toolchain represents a specific build tool configuration (e.g., JDK, compiler) that can be
36+
* used during the Maven build process. This service allows for retrieving, storing, and managing
37+
* these toolchains.</p>
3338
*
3439
* @since 4.0.0
3540
*/
3641
@Experimental
3742
public interface ToolchainManager extends Service {
3843

3944
/**
45+
* Retrieves toolchains matching the specified type and requirements.
4046
*
41-
* @param session
42-
* @param type
43-
* @param requirements
44-
* @return the selected {@link Toolchain}s
45-
* @throws ToolchainManagerException if an exception occurs
47+
* @param session The Maven session context
48+
* @param type The type of toolchain (e.g., "jdk", "compiler")
49+
* @param requirements Key-value pairs specifying toolchain requirements (e.g., "version": "11")
50+
* @return List of matching toolchains, never null
51+
* @throws ToolchainManagerException if toolchain retrieval fails
4652
*/
4753
@Nonnull
4854
List<Toolchain> getToolchains(@Nonnull Session session, String type, Map<String, String> requirements);
4955

5056
/**
57+
* Retrieves all toolchains of the specified type without additional requirements.
5158
*
52-
* @param session
53-
* @param type
54-
* @return the selected {@link Toolchain}
55-
* @throws ToolchainManagerException if an exception occurs
59+
* @param session The Maven session context
60+
* @param type The type of toolchain to retrieve
61+
* @return List of matching toolchains, never null
62+
* @throws ToolchainManagerException if toolchain retrieval fails
5663
*/
5764
@Nonnull
58-
Optional<Toolchain> getToolchainFromBuildContext(@Nonnull Session session, String type)
59-
throws ToolchainManagerException;
65+
default List<Toolchain> getToolchains(@Nonnull Session session, @Nonnull String type)
66+
throws ToolchainManagerException {
67+
return getToolchains(session, type, null);
68+
}
6069

6170
/**
71+
* Retrieves the currently active toolchain from the build context.
6272
*
63-
* @param session
64-
* @param type
65-
* @return the selected {@link Toolchain}s
66-
* @throws ToolchainManagerException if an exception occurs
73+
* @param session The Maven session context
74+
* @param type The type of toolchain to retrieve
75+
* @return Optional containing the toolchain if found
76+
* @throws ToolchainManagerException if toolchain retrieval fails
6777
*/
6878
@Nonnull
69-
List<Toolchain> getToolchainsForType(@Nonnull Session session, String type) throws ToolchainManagerException;
79+
Optional<Toolchain> getToolchainFromBuildContext(@Nonnull Session session, @Nonnull String type)
80+
throws ToolchainManagerException;
7081

7182
/**
83+
* Stores a toolchain in the build context for later retrieval.
7284
*
73-
* @param session
74-
* @param toolchain
75-
* @throws ToolchainManagerException if an exception occurs
85+
* @param session The Maven session context
86+
* @param toolchain The toolchain to store
87+
* @throws ToolchainManagerException if storing the toolchain fails
7688
*/
77-
void storeToolchainToBuildContext(@Nonnull Session session, Toolchain toolchain) throws ToolchainManagerException;
89+
void storeToolchainToBuildContext(@Nonnull Session session, @Nonnull Toolchain toolchain)
90+
throws ToolchainManagerException;
7891
}
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.toolchain;
20+
21+
import java.util.HashMap;
22+
import java.util.List;
23+
import java.util.Map;
24+
import java.util.Optional;
25+
26+
import org.apache.maven.api.annotations.Nonnull;
27+
import org.apache.maven.api.di.Inject;
28+
import org.apache.maven.api.di.Named;
29+
import org.apache.maven.api.di.Singleton;
30+
import org.apache.maven.api.services.Lookup;
31+
import org.apache.maven.api.services.ToolchainFactoryException;
32+
import org.apache.maven.execution.MavenSession;
33+
import org.apache.maven.impl.MappedList;
34+
import org.apache.maven.toolchain.model.ToolchainModel;
35+
import org.slf4j.Logger;
36+
37+
@Named
38+
@Singleton
39+
public class DefaultToolchainManager implements ToolchainManager, ToolchainManagerPrivate {
40+
41+
private final Lookup lookup;
42+
private final Logger logger;
43+
44+
@Inject
45+
public DefaultToolchainManager(Lookup lookup) {
46+
this(lookup, null);
47+
}
48+
49+
protected DefaultToolchainManager(Lookup lookup, Logger logger) {
50+
this.lookup = lookup;
51+
this.logger = logger;
52+
}
53+
54+
private org.apache.maven.impl.DefaultToolchainManager getDelegate() {
55+
return getToolchainManager(lookup, logger);
56+
}
57+
58+
private org.apache.maven.impl.DefaultToolchainManager getToolchainManager(Lookup lookup, Logger logger) {
59+
return getToolchainManager(
60+
lookup.lookupMap(ToolchainFactory.class),
61+
lookup.lookupMap(org.apache.maven.api.services.ToolchainFactory.class),
62+
logger);
63+
}
64+
65+
private org.apache.maven.impl.DefaultToolchainManager getToolchainManager(
66+
Map<String, ToolchainFactory> v3Factories,
67+
Map<String, org.apache.maven.api.services.ToolchainFactory> v4Factories,
68+
Logger logger) {
69+
Map<String, org.apache.maven.api.services.ToolchainFactory> allFactories = new HashMap<>();
70+
for (Map.Entry<String, ToolchainFactory> entry : v3Factories.entrySet()) {
71+
ToolchainFactory v3Factory = entry.getValue();
72+
allFactories.put(entry.getKey(), new org.apache.maven.api.services.ToolchainFactory() {
73+
@Nonnull
74+
@Override
75+
public org.apache.maven.api.Toolchain createToolchain(
76+
@Nonnull org.apache.maven.api.toolchain.ToolchainModel model) throws ToolchainFactoryException {
77+
try {
78+
return getToolchainV4(v3Factory.createToolchain(new ToolchainModel(model)));
79+
} catch (MisconfiguredToolchainException e) {
80+
throw new RuntimeException(e);
81+
}
82+
}
83+
84+
@Nonnull
85+
@Override
86+
public Optional<org.apache.maven.api.Toolchain> createDefaultToolchain()
87+
throws ToolchainFactoryException {
88+
return Optional.ofNullable(v3Factory.createDefaultToolchain())
89+
.map(DefaultToolchainManager.this::getToolchainV4);
90+
}
91+
});
92+
}
93+
allFactories.putAll(v4Factories);
94+
return new org.apache.maven.impl.DefaultToolchainManager(allFactories, logger) {};
95+
}
96+
97+
@Override
98+
public Toolchain getToolchainFromBuildContext(String type, MavenSession session) {
99+
return getDelegate()
100+
.getToolchainFromBuildContext(session.getSession(), type)
101+
.map(this::getToolchainV3)
102+
.orElse(null);
103+
}
104+
105+
@Override
106+
public List<Toolchain> getToolchains(MavenSession session, String type, Map<String, String> requirements) {
107+
return new MappedList<>(
108+
getDelegate().getToolchains(session.getSession(), type, requirements), this::getToolchainV3);
109+
}
110+
111+
@Override
112+
public ToolchainPrivate[] getToolchainsForType(String type, MavenSession session)
113+
throws MisconfiguredToolchainException {
114+
try {
115+
List<org.apache.maven.api.Toolchain> toolchains = getDelegate().getToolchains(session.getSession(), type);
116+
return toolchains.stream().map(this::getToolchainV3).toArray(ToolchainPrivate[]::new);
117+
} catch (org.apache.maven.api.services.ToolchainManagerException e) {
118+
throw new MisconfiguredToolchainException(e.getMessage(), e);
119+
}
120+
}
121+
122+
@Override
123+
public void storeToolchainToBuildContext(ToolchainPrivate toolchain, MavenSession session) {
124+
org.apache.maven.api.Toolchain tc = getToolchainV4(toolchain);
125+
getDelegate().storeToolchainToBuildContext(session.getSession(), tc);
126+
}
127+
128+
private org.apache.maven.api.Toolchain getToolchainV4(ToolchainPrivate toolchain) {
129+
return toolchain instanceof ToolchainWrapperV3 v3tc ? v3tc.delegate : new ToolchainWrapperV4(toolchain);
130+
}
131+
132+
private ToolchainPrivate getToolchainV3(org.apache.maven.api.Toolchain toolchain) {
133+
return toolchain instanceof ToolchainWrapperV4 v3tc ? v3tc.delegate : new ToolchainWrapperV3(toolchain);
134+
}
135+
136+
private record ToolchainWrapperV4(ToolchainPrivate delegate) implements org.apache.maven.api.Toolchain {
137+
138+
@Override
139+
public String getType() {
140+
return delegate.getType();
141+
}
142+
143+
@Override
144+
public String findTool(String toolName) {
145+
return delegate.findTool(toolName);
146+
}
147+
148+
@Override
149+
public org.apache.maven.api.toolchain.ToolchainModel getModel() {
150+
return delegate.getModel().getDelegate();
151+
}
152+
153+
@Override
154+
public boolean matchesRequirements(Map<String, String> requirements) {
155+
return delegate.matchesRequirements(requirements);
156+
}
157+
}
158+
159+
private record ToolchainWrapperV3(org.apache.maven.api.Toolchain delegate) implements Toolchain, ToolchainPrivate {
160+
161+
@Override
162+
public String getType() {
163+
return delegate.getType();
164+
}
165+
166+
@Override
167+
public String findTool(String toolName) {
168+
return delegate.findTool(toolName);
169+
}
170+
171+
@Override
172+
public boolean matchesRequirements(Map<String, String> requirements) {
173+
return delegate.matchesRequirements(requirements);
174+
}
175+
176+
@Override
177+
public ToolchainModel getModel() {
178+
return new ToolchainModel(delegate.getModel());
179+
}
180+
}
181+
}

0 commit comments

Comments
 (0)
0