elharo commented on code in PR #386:
URL: 
https://github.com/apache/maven-antrun-plugin/pull/386#discussion_r3846532736


##########
src/main/java/org/apache/maven/plugins/antrun/AntRunMojo.java:
##########
@@ -443,6 +439,37 @@ public void copyProperties(MavenProject mavenProject, 
Project antProject) {
         antProject.setProperty(versionsPropertyName, 
versionsBuffer.toString());
     }
 
+    /**
+     * Registers one <code>${propertyPrefix}${dependencyConflictId}</code> 
property per dependency,
+     * holding the path to that dependency's artifact file.
+     * <p>
+     * An artifact's file may be <code>null</code> when it was never resolved, 
for instance under
+     * partial or offline resolution. Such dependencies are skipped with a 
warning naming them,
+     * rather than aborting the build: the property is simply absent, so a 
build that never
+     * references it still runs.
+     *
+     * @param depArtifacts the project's dependency artifacts, may be null
+     * @param antProject the Ant project to set properties on, not null
+     */
+    void setDependencyFileProperties(Set<Artifact> depArtifacts, Project 
antProject) {
+        if (depArtifacts == null) {
+            return;
+        }
+
+        for (Artifact artifact : depArtifacts) {
+            String propName = artifact.getDependencyConflictId();
+
+            File artifactFile = artifact.getFile();
+            if (artifactFile == null) {
+                getLog().warn("Not setting property \"" + propertyPrefix + 
propName + "\": dependency "
+                        + artifact.getId() + " has no resolved artifact 
file.");
+                continue;

Review Comment:
   you can avoid continue with an else block



##########
src/test/java/org/apache/maven/plugins/antrun/AntRunMojoDependencyPropertiesTest.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.plugins.antrun;
+
+import java.io.File;
+import java.lang.reflect.Field;
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.DefaultArtifact;
+import org.apache.maven.artifact.handler.DefaultArtifactHandler;
+import org.apache.maven.artifact.versioning.VersionRange;
+import org.apache.tools.ant.Project;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+/**
+ * A dependency whose artifact was never resolved has a null file. Registering 
the
+ * per-dependency path properties must not fall over on it.
+ */
+class AntRunMojoDependencyPropertiesTest {
+
+    /**
+     * execute() defaults propertyPrefix to "" before any of this runs; a 
directly
+     * constructed mojo has not been through that, so mirror it here.
+     */
+    private static AntRunMojo mojoWithEmptyPrefix() throws Exception {
+        AntRunMojo mojo = new AntRunMojo(null);
+        Field prefix = AntRunMojo.class.getDeclaredField("propertyPrefix");
+        prefix.setAccessible(true);
+        prefix.set(mojo, "");
+        return mojo;
+    }
+
+    private static Artifact artifact(String artifactId, File file) {
+        Artifact artifact = new DefaultArtifact(
+                "org.example",
+                artifactId,
+                VersionRange.createFromVersion("1.0"),
+                "compile",
+                "jar",
+                null,
+                new DefaultArtifactHandler("jar"));
+        artifact.setFile(file);
+        return artifact;
+    }
+
+    @Test
+    void unresolvedDependencyIsSkippedInsteadOfThrowing() throws Exception {
+        Set<Artifact> artifacts = new LinkedHashSet<>();
+        artifacts.add(artifact("unresolved", null));
+
+        Project antProject = new Project();
+        AntRunMojo mojo = mojoWithEmptyPrefix();
+
+        assertDoesNotThrow(() -> mojo.setDependencyFileProperties(artifacts, 
antProject));
+        assertNull(
+                antProject.getProperty("org.example:unresolved:jar"),
+                "no path property should be set for a dependency with no 
artifact file");
+    }
+
+    @Test
+    void resolvedDependenciesStillGetTheirPathProperty() throws Exception {
+        File resolved = new File("target", "resolved-1.0.jar");
+
+        Set<Artifact> artifacts = new LinkedHashSet<>();
+        artifacts.add(artifact("unresolved", null));
+        artifacts.add(artifact("resolved", resolved));
+
+        Project antProject = new Project();
+        AntRunMojo mojo = mojoWithEmptyPrefix();
+
+        mojo.setDependencyFileProperties(artifacts, antProject);
+
+        // The unresolved artifact is listed first, so this also proves one bad
+        // dependency no longer prevents the rest from being registered.

Review Comment:
   delete "no longer"



##########
src/main/java/org/apache/maven/plugins/antrun/AntRunMojo.java:
##########
@@ -443,6 +439,37 @@ public void copyProperties(MavenProject mavenProject, 
Project antProject) {
         antProject.setProperty(versionsPropertyName, 
versionsBuffer.toString());
     }
 
+    /**
+     * Registers one <code>${propertyPrefix}${dependencyConflictId}</code> 
property per dependency,
+     * holding the path to that dependency's artifact file.
+     * <p>
+     * An artifact's file may be <code>null</code> when it was never resolved, 
for instance under
+     * partial or offline resolution. Such dependencies are skipped with a 
warning naming them,
+     * rather than aborting the build: the property is simply absent, so a 
build that never
+     * references it still runs.
+     *
+     * @param depArtifacts the project's dependency artifacts, may be null
+     * @param antProject the Ant project to set properties on, not null
+     */
+    void setDependencyFileProperties(Set<Artifact> depArtifacts, Project 
antProject) {

Review Comment:
   this should really be private



##########
src/main/java/org/apache/maven/plugins/antrun/AntRunMojo.java:
##########
@@ -443,6 +439,37 @@ public void copyProperties(MavenProject mavenProject, 
Project antProject) {
         antProject.setProperty(versionsPropertyName, 
versionsBuffer.toString());
     }
 
+    /**
+     * Registers one <code>${propertyPrefix}${dependencyConflictId}</code> 
property per dependency,
+     * holding the path to that dependency's artifact file.
+     * <p>
+     * An artifact's file may be <code>null</code> when it was never resolved, 
for instance under
+     * partial or offline resolution. Such dependencies are skipped with a 
warning naming them,

Review Comment:
   run-on sentence



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to