Pankraz76 commented on code in PR #2380:
URL: https://github.com/apache/maven/pull/2380#discussion_r2107732740


##########
impl/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/TransformerSupport.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * 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.internal.transformation.impl;
+
+import javax.xml.stream.XMLStreamException;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Writer;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import org.apache.maven.api.model.Model;
+import org.apache.maven.api.services.ModelBuilderException;
+import org.apache.maven.internal.transformation.PomArtifactTransformer;
+import org.apache.maven.model.v4.MavenStaxReader;
+import org.apache.maven.model.v4.MavenStaxWriter;
+import org.apache.maven.project.MavenProject;
+import org.eclipse.aether.RepositorySystemSession;
+import org.eclipse.aether.deployment.DeployRequest;
+import org.eclipse.aether.installation.InstallRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Support class.
+ */
+abstract class TransformerSupport implements PomArtifactTransformer {
+    protected final Logger logger = LoggerFactory.getLogger(getClass());
+
+    @Override
+    public InstallRequest remapInstallArtifacts(RepositorySystemSession 
session, InstallRequest request) {
+        return request;
+    }
+
+    @Override
+    public DeployRequest remapDeployArtifacts(RepositorySystemSession session, 
DeployRequest request) {
+        return request;
+    }
+
+    @Override
+    public void injectTransformedArtifacts(RepositorySystemSession session, 
MavenProject project) throws IOException {}
+
+    @Override
+    public void transform(MavenProject project, RepositorySystemSession 
session, Path src, Path tgt)
+            throws ModelBuilderException, XMLStreamException, IOException {
+        throw new IllegalStateException("This transformer does not use this 
call.");
+    }
+
+    protected static final String NAMESPACE_FORMAT = 
"http://maven.apache.org/POM/%s";;

Review Comment:
   consider grouping all fields like we would do for constructors: 
https://checkstyle.sourceforge.io/checks/coding/constructorsdeclarationgrouping.html



##########
api/maven-api-core/src/main/java/org/apache/maven/api/services/ModelBuilder.java:
##########
@@ -29,7 +29,7 @@ public interface ModelBuilder extends Service {
 
     String MODEL_VERSION_4_1_0 = "4.1.0";
 
-    List<String> ALL_KNOWN_MODEL_VERSIONS = List.of(MODEL_VERSION_4_0_0, 
MODEL_VERSION_4_1_0);

Review Comment:
   yes, all is kind of implied.



##########
impl/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/TransformerSupport.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * 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.internal.transformation.impl;
+
+import javax.xml.stream.XMLStreamException;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Writer;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import org.apache.maven.api.model.Model;
+import org.apache.maven.api.services.ModelBuilderException;
+import org.apache.maven.internal.transformation.PomArtifactTransformer;
+import org.apache.maven.model.v4.MavenStaxReader;
+import org.apache.maven.model.v4.MavenStaxWriter;
+import org.apache.maven.project.MavenProject;
+import org.eclipse.aether.RepositorySystemSession;
+import org.eclipse.aether.deployment.DeployRequest;
+import org.eclipse.aether.installation.InstallRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Support class.
+ */
+abstract class TransformerSupport implements PomArtifactTransformer {
+    protected final Logger logger = LoggerFactory.getLogger(getClass());
+
+    @Override
+    public InstallRequest remapInstallArtifacts(RepositorySystemSession 
session, InstallRequest request) {
+        return request;
+    }
+
+    @Override
+    public DeployRequest remapDeployArtifacts(RepositorySystemSession session, 
DeployRequest request) {
+        return request;
+    }
+
+    @Override
+    public void injectTransformedArtifacts(RepositorySystemSession session, 
MavenProject project) throws IOException {}
+
+    @Override
+    public void transform(MavenProject project, RepositorySystemSession 
session, Path src, Path tgt)
+            throws ModelBuilderException, XMLStreamException, IOException {
+        throw new IllegalStateException("This transformer does not use this 
call.");
+    }
+
+    protected static final String NAMESPACE_FORMAT = 
"http://maven.apache.org/POM/%s";;
+
+    protected static final String SCHEMA_LOCATION_FORMAT = 
"https://maven.apache.org/xsd/maven-%s.xsd";;
+
+    protected Model read(Path src) throws IOException, XMLStreamException {
+        MavenStaxReader reader = new MavenStaxReader();
+        try (InputStream is = Files.newInputStream(src)) {
+            return reader.read(is, false, null);
+        }

Review Comment:
   ```suggestion
           try (InputStream is = Files.newInputStream(src)) {
               return new MavenStaxReader().read(is, false, null);
           }
   ```
   could inline.



##########
impl/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/PomInlinerTransformer.java:
##########
@@ -40,28 +41,45 @@
  */
 @Singleton
 @Named
-class PomInlinerTransformer implements PomArtifactTransformer {
-    @Override
-    public InstallRequest remapInstallArtifacts(RepositorySystemSession 
session, InstallRequest request) {
-        return request;
-    }
-
-    @Override
-    public DeployRequest remapDeployArtifacts(RepositorySystemSession session, 
DeployRequest request) {
-        return request;
-    }
+class PomInlinerTransformer extends TransformerSupport {
+    protected static final Pattern PLACEHOLDER_EXPRESSION = 
Pattern.compile("\\$\\{(.+?)}");
 
     @Override
-    public void injectTransformedArtifacts(RepositorySystemSession session, 
MavenProject currentProject)
-            throws IOException {
+    public void injectTransformedArtifacts(RepositorySystemSession session, 
MavenProject project) throws IOException {
         if (!Features.consumerPom(session.getConfigProperties())) {
-            // TODO
+            try {
+                Model model = read(project.getFile().toPath());
+                String version = model.getVersion();
+                String newVersion = version;
+                int lastEnd = -1;
+                if (version != null) {
+                    Matcher m = PLACEHOLDER_EXPRESSION.matcher(version.trim());
+                    while (m.find()) {
+                        String property = m.group(1);
+                        if 
(!session.getConfigProperties().containsKey(property)) {
+                            throw new IllegalArgumentException("Cannot inline 
property " + property);
+                        }
+                        String propertyValue =
+                                (String) 
session.getConfigProperties().get(property);
+                        if (lastEnd < 0) {
+                            newVersion = version.substring(0, m.start());
+                        } else {
+                            newVersion += version.substring(lastEnd, 
m.start());
+                        }
+                        lastEnd = m.end();
+                        newVersion += propertyValue;
+                    }
+                    if (!Objects.equals(version, newVersion)) {
+                        model = model.withVersion(newVersion);
+                        Path tmpPom = Files.createTempFile(
+                                project.getArtifactId() + "-" + 
project.getVersion() + "-", ".xml");
+                        write(model, tmpPom);
+                        project.setFile(tmpPom.toFile());
+                    }
+                }

Review Comment:
   ```suggestion
                       handleVersion(session, project, version, -1, version, 
model);
   ```
   could give dedicated attention to this concern. Operating on all the fields 
is kind of smell also.
   
   ```java
       private static void handleVersion(RepositorySystemSession session, 
MavenProject project, String version, int lastEnd, String newVersion, Model 
model) throws IOException {
           Matcher m = PLACEHOLDER_EXPRESSION.matcher(version.trim());
           while (m.find()) {
               String property = m.group(1);
               if (!session.getConfigProperties().containsKey(property)) {
                   throw new IllegalArgumentException("Cannot inline property " 
+ property);
               }
               String propertyValue =
                       (String) session.getConfigProperties().get(property);
               if (lastEnd < 0) {
                   newVersion = version.substring(0, m.start());
               } else {
                   newVersion += version.substring(lastEnd, m.start());
               }
               lastEnd = m.end();
               newVersion += propertyValue;
           }
           if (!Objects.equals(version, newVersion)) {
               model = model.withVersion(newVersion);
               Path tmpPom = Files.createTempFile(
                       project.getArtifactId() + "-" + project.getVersion() + 
"-", ".xml");
               write(model, tmpPom);
               project.setFile(tmpPom.toFile());
           }
       }
   ```



##########
impl/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/TransformerSupport.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * 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.internal.transformation.impl;
+
+import javax.xml.stream.XMLStreamException;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Writer;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import org.apache.maven.api.model.Model;
+import org.apache.maven.api.services.ModelBuilderException;
+import org.apache.maven.internal.transformation.PomArtifactTransformer;
+import org.apache.maven.model.v4.MavenStaxReader;
+import org.apache.maven.model.v4.MavenStaxWriter;
+import org.apache.maven.project.MavenProject;
+import org.eclipse.aether.RepositorySystemSession;
+import org.eclipse.aether.deployment.DeployRequest;
+import org.eclipse.aether.installation.InstallRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Support class.
+ */
+abstract class TransformerSupport implements PomArtifactTransformer {
+    protected final Logger logger = LoggerFactory.getLogger(getClass());
+
+    @Override
+    public InstallRequest remapInstallArtifacts(RepositorySystemSession 
session, InstallRequest request) {
+        return request;
+    }
+
+    @Override
+    public DeployRequest remapDeployArtifacts(RepositorySystemSession session, 
DeployRequest request) {
+        return request;
+    }
+
+    @Override
+    public void injectTransformedArtifacts(RepositorySystemSession session, 
MavenProject project) throws IOException {}
+
+    @Override
+    public void transform(MavenProject project, RepositorySystemSession 
session, Path src, Path tgt)
+            throws ModelBuilderException, XMLStreamException, IOException {
+        throw new IllegalStateException("This transformer does not use this 
call.");
+    }
+
+    protected static final String NAMESPACE_FORMAT = 
"http://maven.apache.org/POM/%s";;
+
+    protected static final String SCHEMA_LOCATION_FORMAT = 
"https://maven.apache.org/xsd/maven-%s.xsd";;
+
+    protected Model read(Path src) throws IOException, XMLStreamException {
+        MavenStaxReader reader = new MavenStaxReader();
+        try (InputStream is = Files.newInputStream(src)) {
+            return reader.read(is, false, null);
+        }
+    }
+
+    protected void write(Model model, Path dest) throws IOException, 
XMLStreamException {
+        String version = model.getModelVersion();
+        Files.createDirectories(dest.getParent());
+        try (Writer w = Files.newBufferedWriter(dest)) {
+            MavenStaxWriter writer = new MavenStaxWriter();
+            writer.setNamespace(String.format(NAMESPACE_FORMAT, version));
+            writer.setSchemaLocation(String.format(SCHEMA_LOCATION_FORMAT, 
version));
+            writer.setAddLocationInformation(false);
+            writer.write(w, model);
+        }
+    }

Review Comment:
   ```suggestion
           Files.createDirectories(dest.getParent());
           try (Writer w = Files.newBufferedWriter(dest)) {
               write(model.getModelVersion(), w, new MavenStaxWriter())
           }
       }
   ```
   could delegate as well.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@maven.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to