You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by cs...@apache.org on 2024/08/30 12:19:52 UTC

(maven) branch model-transformer created (now 7e28099936)

This is an automated email from the ASF dual-hosted git repository.

cstamas pushed a change to branch model-transformer
in repository https://gitbox.apache.org/repos/asf/maven.git


      at 7e28099936 Provide SPI to hook into build model transformation.

This branch includes the following new commits:

     new 7e28099936 Provide SPI to hook into build model transformation.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(maven) 01/01: Provide SPI to hook into build model transformation.

Posted by cs...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

cstamas pushed a commit to branch model-transformer
in repository https://gitbox.apache.org/repos/asf/maven.git

commit 7e2809993667c675815be781be96283713bed936
Author: Tamas Cservenak <ta...@cservenak.net>
AuthorDate: Fri Aug 30 14:18:51 2024 +0200

    Provide SPI to hook into build model transformation.
---
 .../org/apache/maven/api/spi/ModelTransformer.java | 49 ++++++++++++++++
 .../maven/api/spi/ModelTransformerException.java   | 66 ++++++++++++++++++++++
 .../internal/impl/model/BuildModelTransformer.java | 15 +++++
 3 files changed, 130 insertions(+)

diff --git a/api/maven-api-spi/src/main/java/org/apache/maven/api/spi/ModelTransformer.java b/api/maven-api-spi/src/main/java/org/apache/maven/api/spi/ModelTransformer.java
new file mode 100644
index 0000000000..55cf669dac
--- /dev/null
+++ b/api/maven-api-spi/src/main/java/org/apache/maven/api/spi/ModelTransformer.java
@@ -0,0 +1,49 @@
+/*
+ * 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.model.Model;
+import org.apache.maven.api.services.ModelTransformerContext;
+
+import java.nio.file.Path;
+
+/**
+ * The {@code ModelTransformer} interface is used to transform {@link Model}s parsed (currently in early stage).
+ * This allows plugging in additional functionalities like enriching model or inlining.
+ *
+ * @since 4.0.0
+ */
+@Experimental
+@Consumer
+public interface ModelTransformer extends SpiService {
+    /**
+     * Transform the raw model obtained previously by a previous call to model parser (just parsed from input file or stream).
+     *
+     * @param context the transformer context, never {@code null}
+     * @param model the model, never {@code null}
+     * @param path the POM path being transformed, never {@code null}
+     * @param builder the model builder, never {@code null}
+     * @throws ModelTransformerException if the model cannot be transformed
+     */
+    @Nonnull
+    void transformFileModel(ModelTransformerContext context, Model model, Path path, Model.Builder builder) throws ModelTransformerException;
+}
diff --git a/api/maven-api-spi/src/main/java/org/apache/maven/api/spi/ModelTransformerException.java b/api/maven-api-spi/src/main/java/org/apache/maven/api/spi/ModelTransformerException.java
new file mode 100644
index 0000000000..ee888b44d5
--- /dev/null
+++ b/api/maven-api-spi/src/main/java/org/apache/maven/api/spi/ModelTransformerException.java
@@ -0,0 +1,66 @@
+/*
+ * 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 {
+
+    /**
+     * The one-based index of the line containing the error.
+     */
+    private final int lineNumber;
+
+    /**
+     * The one-based index of the column containing the error.
+     */
+    private final int columnNumber;
+
+    public ModelTransformerException() {
+        this(null, null);
+    }
+
+    public ModelTransformerException(String message) {
+        this(message, null);
+    }
+
+    public ModelTransformerException(String message, Throwable cause) {
+        this(message, -1, -1, cause);
+    }
+
+    public ModelTransformerException(String message, int lineNumber, int columnNumber, Throwable cause) {
+        super(message, cause);
+        this.lineNumber = lineNumber;
+        this.columnNumber = columnNumber;
+    }
+
+    public ModelTransformerException(Throwable cause) {
+        this(null, cause);
+    }
+
+    public int getLineNumber() {
+        return lineNumber;
+    }
+
+    public int getColumnNumber() {
+        return columnNumber;
+    }
+}
diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/BuildModelTransformer.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/BuildModelTransformer.java
index c1ff4d8efa..bb0da84e97 100644
--- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/BuildModelTransformer.java
+++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/BuildModelTransformer.java
@@ -24,9 +24,11 @@ import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
 
+import org.apache.maven.api.di.Inject;
 import org.apache.maven.api.di.Named;
 import org.apache.maven.api.di.Singleton;
 import org.apache.maven.api.model.Dependency;
@@ -43,6 +45,12 @@ import org.apache.maven.api.services.ModelTransformerContext;
 @Named
 @Singleton
 public class BuildModelTransformer implements ModelTransformer {
+    private final Map<String, org.apache.maven.api.spi.ModelTransformer> modelTransformers;
+
+    @Inject
+    public BuildModelTransformer(Map<String, org.apache.maven.api.spi.ModelTransformer> modelTransformers) {
+        this.modelTransformers = modelTransformers;
+    }
 
     @Override
     public Model transform(ModelTransformerContext context, Model model, Path path) {
@@ -50,9 +58,16 @@ public class BuildModelTransformer implements ModelTransformer {
         handleParent(context, model, path, builder);
         handleReactorDependencies(context, model, path, builder);
         handleCiFriendlyVersion(context, model, path, builder);
+        handleSpiModelTransformers(context, model, path, builder);
         return builder.build();
     }
 
+    void handleSpiModelTransformers(ModelTransformerContext context, Model model, Path path, Model.Builder builder) {
+        for (Map.Entry<String, org.apache.maven.api.spi.ModelTransformer> entry : modelTransformers.entrySet()) {
+            entry.getValue().transformFileModel(context, model, path, builder);
+        }
+    }
+
     //
     // Infer parent information
     //