You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by GitBox <gi...@apache.org> on 2022/12/12 08:33:06 UTC

[GitHub] [maven] cstamas commented on a diff in pull request #907: [MNG-7622] Maven Transformation and Consumer POM

cstamas commented on code in PR #907:
URL: https://github.com/apache/maven/pull/907#discussion_r1045528456


##########
maven-core/src/main/java/org/apache/maven/internal/transformation/TransformedArtifact.java:
##########
@@ -0,0 +1,146 @@
+/*
+ * 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;
+
+import static java.util.Objects.requireNonNull;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.security.MessageDigest;
+import java.util.function.BiConsumer;
+import java.util.function.Supplier;
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.DefaultArtifact;
+import org.apache.maven.artifact.handler.ArtifactHandler;
+
+/**
+ * Transformed artifact is derived with some transformation from source artifact.
+ *
+ * @since TBD
+ */
+public abstract class TransformedArtifact extends DefaultArtifact {
+
+    private final OnChangeTransformer onChangeTransformer;
+
+    public TransformedArtifact(
+            Artifact source,
+            Supplier<Path> sourcePathProvider,
+            String classifier,
+            String extension,
+            Path targetPath,
+            BiConsumer<Path, Path> transformerConsumer) {
+        super(
+                source.getGroupId(),
+                source.getArtifactId(),
+                source.getVersionRange(),
+                source.getScope(),
+                extension,
+                classifier,
+                new TransformedArtifactHandler(
+                        classifier, extension, source.getArtifactHandler().getPackaging()));
+        this.onChangeTransformer =
+                new OnChangeTransformer(sourcePathProvider, targetPath, TransformedArtifact::sha1, transformerConsumer);
+    }
+
+    @Override
+    public boolean isResolved() {
+        return getFile() != null;
+    }
+
+    @Override
+    public void setFile(File file) {
+        throw new IllegalStateException("transformed artifact file cannot be set");
+    }
+
+    @Override
+    public File getFile() {
+        Path result = onChangeTransformer.get();
+        if (result == null) {
+            return null;
+        }
+        return result.toFile();
+    }
+
+    private static final int BUFFER_SIZE = 8192;
+
+    private static String sha1(Path path) {
+        try {
+            MessageDigest md = MessageDigest.getInstance("SHA-1");
+            try (InputStream fis = new BufferedInputStream(Files.newInputStream(path), BUFFER_SIZE)) {

Review Comment:
   Um, I don't think so.... Files.newInputStream returns _unbuffered_ stream, unless am mistaken...



-- 
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-unsubscribe@maven.apache.org

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