Skip to content

Commit 625b456

Browse files
authored
[MNG-8232] Introduce ModelTransformer (#1702)
1 parent bcd5d9c commit 625b456

File tree

7 files changed

+153
-4
lines changed

7 files changed

+153
-4
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818
*/
1919
package org.apache.maven.api.services;
2020

21+
import org.apache.maven.api.annotations.Experimental;
22+
2123
/**
24+
* Exception thrown when a {@link ModelTransformer} fails.
2225
*
2326
* @since 4.0.0
2427
*/
28+
@Experimental
2529
public class ModelTransformerException extends MavenException {
2630

2731
public ModelTransformerException(Exception e) {
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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.spi;
20+
21+
import org.apache.maven.api.annotations.Consumer;
22+
import org.apache.maven.api.annotations.Experimental;
23+
import org.apache.maven.api.annotations.Nonnull;
24+
import org.apache.maven.api.di.Named;
25+
import org.apache.maven.api.model.Model;
26+
import org.apache.maven.api.services.ModelTransformerException;
27+
28+
/**
29+
* Marker interface for model transformers.
30+
*
31+
* @since 4.0.0
32+
*/
33+
@Experimental
34+
@Consumer
35+
@Named
36+
public interface ModelTransformer extends SpiService {
37+
38+
/**
39+
* Apply a transformation on the file model.
40+
*
41+
* This method will be called on each file model being loaded,
42+
* just before validation.
43+
*
44+
* @param model the input model
45+
* @return the transformed model, or the input model if no transformation is needed
46+
* @throws ModelTransformerException
47+
*/
48+
@Nonnull
49+
default Model transformFileModel(@Nonnull Model model) throws ModelTransformerException {
50+
return model;
51+
}
52+
53+
/**
54+
* Apply a transformation on the raw models.
55+
*
56+
* This method will be called on each raw model being loaded,
57+
* just before validation.
58+
*
59+
* @param model the input model
60+
* @return the transformed model, or the input model if no transformation is needed
61+
* @throws ModelTransformerException
62+
*/
63+
@Nonnull
64+
default Model transformRawModel(@Nonnull Model model) throws ModelTransformerException {
65+
return model;
66+
}
67+
68+
/**
69+
* Apply a transformation on the effective models.
70+
*
71+
* This method will be called on each effective model being loaded,
72+
* just before validation.
73+
*
74+
* @param model the input model
75+
* @return the transformed model, or the input model if no transformation is needed
76+
* @throws ModelTransformerException
77+
*/
78+
@Nonnull
79+
default Model transformEffectiveModel(@Nonnull Model model) throws ModelTransformerException {
80+
return model;
81+
}
82+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.spi;
20+
21+
import org.apache.maven.api.annotations.Experimental;
22+
import org.apache.maven.api.services.MavenException;
23+
24+
@Experimental
25+
public class ModelTransformerException extends MavenException {
26+
27+
public ModelTransformerException() {
28+
this(null, null);
29+
}
30+
31+
public ModelTransformerException(String message) {
32+
this(message, null);
33+
}
34+
35+
public ModelTransformerException(Throwable cause) {
36+
this(null, cause);
37+
}
38+
39+
public ModelTransformerException(String message, Throwable cause) {
40+
super(message, cause);
41+
}
42+
}

maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ public class DefaultModelBuilder implements ModelBuilder {
145145
private final ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator;
146146
private final ModelTransformer transformer;
147147
private final ModelVersionParser versionParser;
148+
private final List<org.apache.maven.api.spi.ModelTransformer> transformers;
148149

149150
@SuppressWarnings("checkstyle:ParameterNumber")
150151
@Inject
@@ -166,7 +167,8 @@ public DefaultModelBuilder(
166167
PluginConfigurationExpander pluginConfigurationExpander,
167168
ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator,
168169
ModelTransformer transformer,
169-
ModelVersionParser versionParser) {
170+
ModelVersionParser versionParser,
171+
List<org.apache.maven.api.spi.ModelTransformer> transformers) {
170172
this.modelProcessor = modelProcessor;
171173
this.modelValidator = modelValidator;
172174
this.modelNormalizer = modelNormalizer;
@@ -185,6 +187,7 @@ public DefaultModelBuilder(
185187
this.profileActivationFilePathInterpolator = profileActivationFilePathInterpolator;
186188
this.transformer = transformer;
187189
this.versionParser = versionParser;
190+
this.transformers = transformers;
188191
}
189192

190193
@Override
@@ -590,6 +593,10 @@ private ModelBuilderResult build(
590593
resultModel = pluginConfigurationExpander.expandPluginConfiguration(resultModel, request, problems);
591594
}
592595

596+
for (var transformer : transformers) {
597+
resultModel = transformer.transformEffectiveModel(resultModel);
598+
}
599+
593600
result.setEffectiveModel(resultModel);
594601

595602
// effective model validation
@@ -803,6 +810,10 @@ private Model doReadFileModel(
803810
}
804811
}
805812

813+
for (var transformer : transformers) {
814+
model = transformer.transformFileModel(model);
815+
}
816+
806817
problems.setSource(model);
807818
modelValidator.validateFileModel(model, request, problems);
808819
if (hasFatalErrors(problems)) {
@@ -845,6 +856,10 @@ private ModelData doReadRawModel(
845856
rawModel = rawModel.withModelVersion(namespace.substring(NAMESPACE_PREFIX.length()));
846857
}
847858

859+
for (var transformer : transformers) {
860+
rawModel = transformer.transformRawModel(rawModel);
861+
}
862+
848863
modelValidator.validateRawModel(rawModel, request, problems);
849864

850865
if (hasFatalErrors(problems)) {

maven-api-impl/src/test/java/org/apache/maven/internal/impl/standalone/RepositorySystemSupplier.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,8 @@ protected ModelBuilder createModelBuilder() {
10591059
new DefaultPluginConfigurationExpander(),
10601060
new ProfileActivationFilePathInterpolator(new DefaultPathTranslator(), new DefaultRootLocator()),
10611061
new BuildModelTransformer(),
1062-
new DefaultModelVersionParser(getVersionScheme()));
1062+
new DefaultModelVersionParser(getVersionScheme()),
1063+
List.of());
10631064
}
10641065

10651066
private RepositorySystem repositorySystem;

maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ class DefaultConsumerPomBuilder implements ConsumerPomBuilder {
138138
@Inject
139139
private ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator;
140140

141+
@Inject
142+
private List<org.apache.maven.api.spi.ModelTransformer> transformers;
143+
141144
Logger logger = LoggerFactory.getLogger(getClass());
142145

143146
@Override
@@ -193,7 +196,8 @@ public List<Profile> getActiveProfiles(
193196
pluginConfigurationExpander,
194197
profileActivationFilePathInterpolator,
195198
modelTransformer,
196-
versionParser);
199+
versionParser,
200+
transformers);
197201
InternalSession iSession = InternalSession.from(session);
198202
ModelBuilderRequest.ModelBuilderRequestBuilder request = ModelBuilderRequest.builder();
199203
request.projectBuild(true);

maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenRepositorySystemSupplier.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,8 @@ protected ModelBuilder createModelBuilder() {
10611061
new DefaultPluginConfigurationExpander(),
10621062
new ProfileActivationFilePathInterpolator(new DefaultPathTranslator(), new DefaultRootLocator()),
10631063
new BuildModelTransformer(),
1064-
new DefaultModelVersionParser(getVersionScheme()));
1064+
new DefaultModelVersionParser(getVersionScheme()),
1065+
List.of());
10651066
}
10661067

10671068
private RepositorySystem repositorySystem;

0 commit comments

Comments
 (0)