Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-8232] Introduce ModelTransformer #1702

Merged
merged 1 commit into from
Sep 12, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Member

Choose a reason for hiding this comment

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

LGTM


/**
* 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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -166,7 +167,8 @@ public DefaultModelBuilder(
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;
Expand All @@ -185,6 +187,7 @@ public DefaultModelBuilder(
this.profileActivationFilePathInterpolator = profileActivationFilePathInterpolator;
this.transformer = transformer;
this.versionParser = versionParser;
this.transformers = transformers;
}

@Override
Expand Down Expand Up @@ -590,6 +593,10 @@ private ModelBuilderResult build(
resultModel = pluginConfigurationExpander.expandPluginConfiguration(resultModel, request, problems);
}

for (var transformer : transformers) {
resultModel = transformer.transformEffectiveModel(resultModel);
}

result.setEffectiveModel(resultModel);

// effective model validation
Expand Down Expand Up @@ -803,6 +810,10 @@ private Model doReadFileModel(
}
}

for (var transformer : transformers) {
model = transformer.transformFileModel(model);
}

problems.setSource(model);
modelValidator.validateFileModel(model, request, problems);
if (hasFatalErrors(problems)) {
Expand Down Expand Up @@ -845,6 +856,10 @@ private ModelData doReadRawModel(
rawModel = rawModel.withModelVersion(namespace.substring(NAMESPACE_PREFIX.length()));
}

for (var transformer : transformers) {
rawModel = transformer.transformRawModel(rawModel);
}

modelValidator.validateRawModel(rawModel, request, problems);

if (hasFatalErrors(problems)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,8 @@ protected ModelBuilder createModelBuilder() {
new DefaultPluginConfigurationExpander(),
new ProfileActivationFilePathInterpolator(new DefaultPathTranslator(), new DefaultRootLocator()),
new BuildModelTransformer(),
new DefaultModelVersionParser(getVersionScheme()));
new DefaultModelVersionParser(getVersionScheme()),
List.of());
}

private RepositorySystem repositorySystem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -193,7 +196,8 @@ public List<Profile> getActiveProfiles(
pluginConfigurationExpander,
profileActivationFilePathInterpolator,
modelTransformer,
versionParser);
versionParser,
transformers);
InternalSession iSession = InternalSession.from(session);
ModelBuilderRequest.ModelBuilderRequestBuilder request = ModelBuilderRequest.builder();
request.projectBuild(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,8 @@ protected ModelBuilder createModelBuilder() {
new DefaultPluginConfigurationExpander(),
new ProfileActivationFilePathInterpolator(new DefaultPathTranslator(), new DefaultRootLocator()),
new BuildModelTransformer(),
new DefaultModelVersionParser(getVersionScheme()));
new DefaultModelVersionParser(getVersionScheme()),
List.of());
}

private RepositorySystem repositorySystem;
Expand Down