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

asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ant.git


The following commit(s) were added to refs/heads/master by this push:
     new 580e0e15e 17781 Introduce a new attribtue for the "record" task to 
allow creating the recorder's file within the basedir of the project
580e0e15e is described below

commit 580e0e15efa15176526ab0bb21544efcfd13768c
Author: Jaikiran Pai <[email protected]>
AuthorDate: Sun Jul 26 13:05:06 2026 +0530

    17781 Introduce a new attribtue for the "record" task to allow creating the 
recorder's file within the basedir of the project
---
 WHATSNEW                                           | 10 +++
 manual/Tasks/recorder.html                         | 13 ++-
 src/etc/testcases/taskdefs/recorder3.xml           | 47 ++++++++++
 .../org/apache/tools/ant/taskdefs/Recorder.java    | 39 ++++++++-
 .../tools/ant/taskdefs/RecorderBaseDirTest.java    | 99 ++++++++++++++++++++++
 5 files changed, 205 insertions(+), 3 deletions(-)

diff --git a/WHATSNEW b/WHATSNEW
index 7b1e535bf..1f719b63c 100644
--- a/WHATSNEW
+++ b/WHATSNEW
@@ -22,6 +22,16 @@ Other changes:
    Based on a patch used by the Debian Ant package maintainers.
    Part of Bugzilla Report 61269
 
+ * The "record" task now has a new "relativeToBaseDir" attribute,
+   which can be set to "yes" or "no", to control where the
+   recorder's file gets created. In the absence of this attribute,
+   if the "name" of the recorder was a relative path, then the
+   recorder would create the file in the current working directory
+   of the process. With this new attribute, the recorder can be
+   configured to create that file in the basedir of the project.
+   Bugzilla Report 17781
+
+
 Fixed bugs:
 -----------
 
diff --git a/manual/Tasks/recorder.html b/manual/Tasks/recorder.html
index d0f4c37d0..25760637e 100644
--- a/manual/Tasks/recorder.html
+++ b/manual/Tasks/recorder.html
@@ -48,7 +48,9 @@ <h3>Parameters</h3>
   </tr>
   <tr>
     <td>name</td>
-    <td>The name of the file this logger is associated with.</td>
+    <td>The file path to which this recorder will record to. This can either 
be a absolute path or
+        a relative path.
+    </td>
     <td>Yes</td>
   </tr>
   <tr>
@@ -80,6 +82,15 @@ <h3>Parameters</h3>
     </td>
     <td>No; default is <q>info</q></td>
   </tr>
+  <tr>
+    <td>relativeToBaseDir</td>
+    <td>If <q>name</q> is a relative file path then setting this to <q>yes</q> 
will treat the
+        file path as relative to the basedir of the project. A value of 
<q>no</q>, will treat
+        the relative file path as relative to the current working directory of 
the process.
+        <em>Since Ant 1.10.18</em>
+    </td>
+    <td>No; defaults to <q>no</q></td>
+  </tr>
 </table>
 
 <h3>Examples</h3>
diff --git a/src/etc/testcases/taskdefs/recorder3.xml 
b/src/etc/testcases/taskdefs/recorder3.xml
new file mode 100644
index 000000000..addf96c3c
--- /dev/null
+++ b/src/etc/testcases/taskdefs/recorder3.xml
@@ -0,0 +1,47 @@
+<?xml version="1.0"?>
+<!--
+  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
+
+      https://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.
+-->
+<project name="recorder-basedir-test" default="all" basedir=".">
+
+    <import file="../buildfiletest-base.xml"/>
+
+    <target name="setUp">
+        <mkdir dir="${output}"/>
+    </target>
+
+    <!-- uses a relative path for the "name" with "relativeToBaseDir" set to 
true and expects
+         the recorder to create the file under the basedir -->
+    <target name="basedir-relative">
+        <record name="recorded-out-1.txt" relativeToBaseDir="yes" 
action="start"/>
+        <echo message="some message1"/>
+        <record name="recorded-out-1.txt" relativeToBaseDir="yes" 
action="stop"/>
+    </target>
+
+    <!-- uses an absolute path for the "name" and expects that the 
"relativeToBaseDir"
+         attribute plays no role -->
+    <target name="absolute-path">
+        <mkdir dir="${output}/recorded"/>
+        <echo message="record name ${output}/recorded"/>
+        <!-- ${output} value is an absolute path -->
+        <record name="${output}/recorded/recorded-out-2.txt" 
relativeToBaseDir="yes"
+                action="start"/>
+        <echo message="some message1"/>
+        <record name="${output}/recorded/recorded-out-2.txt" 
relativeToBaseDir="yes"
+                action="stop"/>
+    </target>
+
+</project>
diff --git a/src/main/org/apache/tools/ant/taskdefs/Recorder.java 
b/src/main/org/apache/tools/ant/taskdefs/Recorder.java
index 9512abf21..64e268cdb 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Recorder.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Recorder.java
@@ -17,6 +17,8 @@
  */
 package org.apache.tools.ant.taskdefs;
 
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.Hashtable;
 import java.util.Map;
 
@@ -67,7 +69,9 @@ public class Recorder extends Task implements 
SubBuildListener {
     /** Strip task banners if true.  */
     private boolean emacsMode = false;
     /** The list of recorder entries. */
-    private static Map<String, RecorderEntry> recorderEntries = new 
Hashtable<>();
+    private static final Map<String, RecorderEntry> recorderEntries = new 
Hashtable<>();
+
+    private boolean relativeToBaseDir = false;
 
     //////////////////////////////////////////////////////////////////////
     // CONSTRUCTORS / INITIALIZERS
@@ -146,6 +150,24 @@ public class Recorder extends Task implements 
SubBuildListener {
         loglevel = level.getLevel();
     }
 
+    /**
+     * If this attribute is set to {@code true} and the {@code name} of the 
recorder is a relative
+     * path then the file path is considered relative to the
+     * {@linkplain Project#getBaseDir() project's basedir}. If this attribute 
is {@code false}
+     * and if the {@code name} of the recorder is a relative path, then the 
file path is considered
+     * relative to the current directory of the process.
+     * <p>
+     * The value of this attribute plays no role if the {@code name} of the 
recorder is
+     * an absolute path.
+     *
+     * @param relativeToBaseDir true if the recorder file is relative to 
{@code basedir},
+     *                          false otherwise
+     * @since Ant 1.10.18
+     */
+    public void setRelativeToBaseDir(final boolean relativeToBaseDir) {
+        this.relativeToBaseDir = relativeToBaseDir;
+    }
+
     //////////////////////////////////////////////////////////////////////
     // CORE / MAIN BODY
 
@@ -161,8 +183,21 @@ public class Recorder extends Task implements 
SubBuildListener {
         getProject().log("setting a recorder for name " + filename,
             Project.MSG_DEBUG);
 
+        final String filePath;
+        final boolean isAbsolutePath = Paths.get(filename).isAbsolute();
+        if (isAbsolutePath) {
+            filePath = filename;
+        } else {
+            if (relativeToBaseDir) {
+                final Path baseDir = getProject().getBaseDir().toPath();
+                filePath = baseDir.resolve(filename).toString();
+            } else {
+                final Path currentDir = Paths.get("");
+                filePath = currentDir.resolve(filename).toString();
+            }
+        }
         // get the recorder entry
-        RecorderEntry recorder = getRecorder(filename, getProject());
+        RecorderEntry recorder = getRecorder(filePath, getProject());
         // set the values on the recorder
         recorder.setMessageOutputLevel(loglevel);
         recorder.setEmacsMode(emacsMode);
diff --git 
a/src/tests/junit/org/apache/tools/ant/taskdefs/RecorderBaseDirTest.java 
b/src/tests/junit/org/apache/tools/ant/taskdefs/RecorderBaseDirTest.java
new file mode 100644
index 000000000..f915b5183
--- /dev/null
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/RecorderBaseDirTest.java
@@ -0,0 +1,99 @@
+/*
+ *  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
+ *
+ *      https://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.tools.ant.taskdefs;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.tools.ant.BuildFileRule;
+import org.apache.tools.ant.util.FileUtils;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests the "record" task's "relativeToBaseDir" behaviour
+ */
+public class RecorderBaseDirTest {
+
+    @Rule
+    public final BuildFileRule buildRule = new BuildFileRule();
+
+    private static final String REC_IN = "recorder/";
+
+    /**
+     * Utilities used for file operations
+     */
+    private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
+
+    @Before
+    public void setUp() {
+        buildRule.configureProject("src/etc/testcases/taskdefs/recorder3.xml");
+        // configure a custom basedir for the tests
+        final String testTmpDir = 
buildRule.getProject().getProperty("java.io.tmpdir");
+        buildRule.getProject().setBaseDir(new File(testTmpDir));
+        System.out.println("Set basedir to " + testTmpDir + " for project "
+                + buildRule.getProject().getName());
+
+        buildRule.executeTarget("setUp");
+    }
+
+    /**
+     * Resolves the given {@code fileName} relative to the {@code 
src/etc/testcases/taskdefs/}
+     * directory.
+     *
+     * @param fileName The file name
+     * @return the resolved file path
+     */
+    private static File resolveFile(final String fileName) {
+        return FILE_UTILS.resolveFile(new File("src/etc/testcases/taskdefs/"), 
fileName);
+    }
+
+    /**
+     * Verify that if the "name" of the "record" task is a relative path then 
setting
+     * "relativeToBaseDir" to "true" creates the file under the basedir.
+     */
+    @Test
+    public void testBaseDirRelative() throws IOException {
+        buildRule.executeTarget("basedir-relative");
+        final File expected = resolveFile(REC_IN + "rectest1.result");
+        // expect the file to be generated in the basedir
+        final File actual = new File(buildRule.getProject().getBaseDir(), 
"recorded-out.txt");
+        assertTrue("content mismatch in files \"" + expected + "\" and \"" + 
actual + "\"",
+                FILE_UTILS.contentEquals(expected, actual, true));
+    }
+
+    /**
+     * Verify that if the "name" of the "record" task is an absolute path then 
setting
+     * "relativeToBaseDir" to "true" plays no role in where the file is 
created.
+     */
+    @Test
+    public void testAbsolutePath() throws IOException {
+        buildRule.executeTarget("absolute-path");
+        final File expected = resolveFile(REC_IN + "rectest1.result");
+        // expect the file to be generated at the absolute path used in the 
target
+        final File actual = buildRule.getOutputDir().toPath()
+                .resolve("recorded")
+                .resolve("recorded-out-2.txt")
+                .toFile();
+        assertTrue("content mismatch in files \"" + expected + "\" and \"" + 
actual + "\"",
+                FILE_UTILS.contentEquals(expected, actual, true));
+    }
+}

Reply via email to