[MNG-8232] Introduce ModelTransformer (#1702)

This commit is contained in:
Guillaume Nodet 2024-09-12 08:15:53 +02:00 committed by GitHub
parent bcd5d9c9f9
commit 625b4561f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 153 additions and 4 deletions

View File

@ -18,10 +18,14 @@
*/
package org.apache.maven.api.services;
import org.apache.maven.api.annotations.Experimental;
/**
* Exception thrown when a {@link ModelTransformer} fails.
*
* @since 4.0.0
*/
@Experimental
public class ModelTransformerException extends MavenException {
public ModelTransformerException(Exception e) {

View File

@ -0,0 +1,82 @@
/*
* 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.
*/
package org.apache.maven.api.spi;
import org.apache.maven.api.annotations.Consumer;
import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.di.Named;
import org.apache.maven.api.model.Model;
import org.apache.maven.api.services.ModelTransformerException;
/**
* Marker interface for model transformers.
*
* @since 4.0.0
*/
@Experimental
@Consumer
@Named
public interface ModelTransformer extends SpiService {
/**
* Apply a transformation on the file model.
*
* This method will be called on each file model being loaded,
* just before validation.
*
* @param model the input model
* @return the transformed model, or the input model if no transformation is needed
* @throws ModelTransformerException
*/
@Nonnull
default Model transformFileModel(@Nonnull Model model) throws ModelTransformerException {
return model;
}
/**
* Apply a transformation on the raw models.
*
* This method will be called on each raw model being loaded,
* just before validation.
*
* @param model the input model
* @return the transformed model, or the input model if no transformation is needed
* @throws ModelTransformerException
*/
@Nonnull
default Model transformRawModel(@Nonnull Model model) throws ModelTransformerException {
return model;
}
/**
* Apply a transformation on the effective models.
*
* This method will be called on each effective model being loaded,
* just before validation.
*
* @param model the input model
* @return the transformed model, or the input model if no transformation is needed
* @throws ModelTransformerException
*/
@Nonnull
default Model transformEffectiveModel(@Nonnull Model model) throws ModelTransformerException {
return model;
}
}

View File

@ -0,0 +1,42 @@
/*
* 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.
*/
package org.apache.maven.api.spi;
import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.services.MavenException;
@Experimental
public class ModelTransformerException extends MavenException {
public ModelTransformerException() {
this(null, null);
}
public ModelTransformerException(String message) {
this(message, null);
}
public ModelTransformerException(Throwable cause) {
this(null, cause);
}
public ModelTransformerException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -145,6 +145,7 @@ public class DefaultModelBuilder implements ModelBuilder {
private final ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator;
private final ModelTransformer transformer;
private final ModelVersionParser versionParser;
private final List<org.apache.maven.api.spi.ModelTransformer> transformers;
@SuppressWarnings("checkstyle:ParameterNumber")
@Inject
@ -166,7 +167,8 @@ public class DefaultModelBuilder implements ModelBuilder {
PluginConfigurationExpander pluginConfigurationExpander,
ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator,
ModelTransformer transformer,
ModelVersionParser versionParser) {
ModelVersionParser versionParser,
List<org.apache.maven.api.spi.ModelTransformer> transformers) {
this.modelProcessor = modelProcessor;
this.modelValidator = modelValidator;
this.modelNormalizer = modelNormalizer;
@ -185,6 +187,7 @@ public class DefaultModelBuilder implements ModelBuilder {
this.profileActivationFilePathInterpolator = profileActivationFilePathInterpolator;
this.transformer = transformer;
this.versionParser = versionParser;
this.transformers = transformers;
}
@Override
@ -590,6 +593,10 @@ public class DefaultModelBuilder implements ModelBuilder {
resultModel = pluginConfigurationExpander.expandPluginConfiguration(resultModel, request, problems);
}
for (var transformer : transformers) {
resultModel = transformer.transformEffectiveModel(resultModel);
}
result.setEffectiveModel(resultModel);
// effective model validation
@ -803,6 +810,10 @@ public class DefaultModelBuilder implements ModelBuilder {
}
}
for (var transformer : transformers) {
model = transformer.transformFileModel(model);
}
problems.setSource(model);
modelValidator.validateFileModel(model, request, problems);
if (hasFatalErrors(problems)) {
@ -845,6 +856,10 @@ public class DefaultModelBuilder implements ModelBuilder {
rawModel = rawModel.withModelVersion(namespace.substring(NAMESPACE_PREFIX.length()));
}
for (var transformer : transformers) {
rawModel = transformer.transformRawModel(rawModel);
}
modelValidator.validateRawModel(rawModel, request, problems);
if (hasFatalErrors(problems)) {

View File

@ -1059,7 +1059,8 @@ public class RepositorySystemSupplier implements Supplier<RepositorySystem> {
new DefaultPluginConfigurationExpander(),
new ProfileActivationFilePathInterpolator(new DefaultPathTranslator(), new DefaultRootLocator()),
new BuildModelTransformer(),
new DefaultModelVersionParser(getVersionScheme()));
new DefaultModelVersionParser(getVersionScheme()),
List.of());
}
private RepositorySystem repositorySystem;

View File

@ -138,6 +138,9 @@ class DefaultConsumerPomBuilder implements ConsumerPomBuilder {
@Inject
private ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator;
@Inject
private List<org.apache.maven.api.spi.ModelTransformer> transformers;
Logger logger = LoggerFactory.getLogger(getClass());
@Override
@ -193,7 +196,8 @@ class DefaultConsumerPomBuilder implements ConsumerPomBuilder {
pluginConfigurationExpander,
profileActivationFilePathInterpolator,
modelTransformer,
versionParser);
versionParser,
transformers);
InternalSession iSession = InternalSession.from(session);
ModelBuilderRequest.ModelBuilderRequestBuilder request = ModelBuilderRequest.builder();
request.projectBuild(true);

View File

@ -1061,7 +1061,8 @@ public class MavenRepositorySystemSupplier implements Supplier<RepositorySystem>
new DefaultPluginConfigurationExpander(),
new ProfileActivationFilePathInterpolator(new DefaultPathTranslator(), new DefaultRootLocator()),
new BuildModelTransformer(),
new DefaultModelVersionParser(getVersionScheme()));
new DefaultModelVersionParser(getVersionScheme()),
List.of());
}
private RepositorySystem repositorySystem;