HOWTO
- https://gemini.google.com/app
- https://ant-contrib.sourceforge.net/
- https://ant-contrib.sourceforge.net/tasks/tasks/for.html
- Get antcontrib.jar via Maven https://mvnrepository.com/artifact/ant-contrib/ant-contrib/1.0b3
- https://stackoverflow.com/questions/3056395/how-can-i-iterate-over-properties-from-a-file
- https://stackoverflow.com/questions/7102793/how-to-put-a-newline-in-ant-property
- AntContrib propertyselector https://ant-contrib.sourceforge.net/tasks/tasks/propertyselector.html
GitHub
- Get antcontrib.jar https://github.com/dveselka/java-tutorial/blob/master/tools/pom.xml
- ant file to produce sed commands from property file https://github.com/dveselka/java-tutorial/blob/master/tools/build.xml
Install antcontrib
Get via Maven https://github.com/dveselka/java-tutorial/blob/master/tools/pom.xml
To install ant-contrib:
- Copy
ant-contrib-0.3.jar
to thelib
directory of your Ant installation. If you want to use one of the tasks in your own project, add the lines<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
to your build file. - Keep
ant-contrib-0.3.jar
in a separate location. You now have to tell Ant explicitly where to find it (say in/usr/share/java/lib
):<taskdef resource="net/sf/antcontrib/antcontrib.properties"> <classpath> <pathelement location="/usr/share/java/lib/ant-contrib-0.3.jar"/> </classpath> </taskdef>
Get antcontrib.jar via Maven
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>dave</groupId>
<artifactId>tools</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>dave</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<!-- https://mvnrepository.com/artifact/ant-contrib/ant-contrib -->
<dependency>
<groupId>ant-contrib</groupId>
<artifactId>ant-contrib</artifactId>
<version>1.0b3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
$ mvn clean install
[INFO] --- maven-dependency-plugin:2.8:copy-dependencies (default) @ tools ---
[INFO] Copying ant-contrib-1.0b3.jar to /git/java-tutorial/tools/target/lib/ant-contrib-1.0b3.jar
[INFO] Copying ant-1.5.jar to /git/java-tutorial/tools/target/lib/ant-1.5.jar
Properties file https://github.com/dveselka/java-tutorial/blob/master/tools/dave.properties
a=1
jdbc.url=jdbc:oracle:thin:dave@dave:1521/DAVE_DB
url=https://dave.com/dave
date=Sun Sep 15 08:43:48 AM CEST 2024
jdbc.url=jdbc:oracle:thin:dave@dave:1521/DAVE_DB
url=https://dave.com/dave
date=Sun Sep 15 08:43:48 AM CEST 2024
Build.xml to produce sed file from properties file https://github.com/dveselka/java-tutorial/blob/master/tools/build.xml
Notice that @ gives value of for cycle variable , $ gives value of ant property
<project name="properties-to-sed">
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="target/lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<target name="convert">
<property file="dave.properties" prefix="dave."/>
<propertyselector property="dave" match="dave\.(.*)" select="\1"/>
<for param="key" list="${dave}">
<sequential>
<echo>@{key}=${dave.@{key}}</echo>
<echo message="s#@{key}#${dave.@{key}}#g${line.separator}" file="replace-dave-props.sed" append="true"/>
</sequential>
</for>
</target>
</project>
Run ant
$ ant convert
Buildfile: /git/java-tutorial/tools/build.xml
convert:
[echo] url=https://dave.com/dave
[echo] a=1
[echo] date=Sun Sep 15 08:43:48 AM CEST 2024
[echo] jdbc.url=jdbc:oracle:thin:dave@dave:1521/DAVE_DB
BUILD SUCCESSFUL
Total time: 0 seconds
Buildfile: /git/java-tutorial/tools/build.xml
convert:
[echo] url=https://dave.com/dave
[echo] a=1
[echo] date=Sun Sep 15 08:43:48 AM CEST 2024
[echo] jdbc.url=jdbc:oracle:thin:dave@dave:1521/DAVE_DB
BUILD SUCCESSFUL
Total time: 0 seconds
Sed file produced from properties file
$ cat replace-dave-props.sed
s#url#https://dave.com/dave#g
s#a#1#g
s#date#Sun Sep 15 08:43:48 AM CEST 2024#g
s#jdbc.url#jdbc:oracle:thin:dave@dave:1521/DAVE_DB#g
Asking Gemimi for hints - useful, provided idea with antcontrib for cycle, needs fixing - add antcontrib install, fix for cycle
Approach:
Create a build.xml file:
- Write the Ant build script in a
build.xml
file. - Define a
target
to perform the conversion.
- Write the Ant build script in a
Load properties file:
- Use the
property
task to load the properties file.
- Use the
Generate sed commands:
- Use the
for
task to iterate over the properties entries. - For each entry, create a sed command using the
echo
task.
- Use the
Write sed commands to file:
- Use the
echo
task to write the generated sed commands to a file.
- Use the
build.xml example:
XML
<project name="properties-to-sed">
<target name="convert">
<property file="replacements.properties"/>
<for param="key" elements="${replacements.keys()}">
<sequential>
<echo message="s/${replacements.${key}}/${replacements.${key}}/g" file="sed_script.sed" append="true"/>
</sequential>
</for>
</target>
</project>
Explanation:
- The
replacements.properties
file should contain key-value pairs where the key is the search pattern and the value is the replacement string. For example:
search1=replacement1
search2=replacement2
- The
build.xml
file defines aconvert
target. - The
property
task loads the properties file. - The
for
task iterates over the keys in the properties file. - For each key, an
echo
task creates a sed command using the key and value as the search pattern and replacement string. - The
echo
task writes the sed command to thesed_script.sed
file, appending it to the end of the file.
Additional considerations:
- You can customize the sed command format based on your specific requirements.
- If you need to escape special characters in the search patterns or replacement strings, use appropriate escape sequences.
- For more complex scenarios or performance optimization, consider using a dedicated scripting language like Python or Perl.
Asking ChatGPT for comparison
No comments:
Post a Comment