Quantcast
Channel: DevOps tips & tricks
Viewing all articles
Browse latest Browse all 181

Convert Java properties file to sed commands using ant

$
0
0

 HOWTO

GitHub

Install antcontrib 

Get via Maven  https://github.com/dveselka/java-tutorial/blob/master/tools/pom.xml

To install ant-contrib:

  1. Copy ant-contrib-0.3.jar to the lib 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.
  2. 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

 

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

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:

  1. Create a build.xml file:

    • Write the Ant build script in a build.xml file.
    • Define a target to perform the conversion.
  2. Load properties file:

    • Use the property task to load the properties file.
  3. Generate sed commands:

    • Use the for task to iterate over the properties entries.
    • For each entry, create a sed command using the echo task.
  4. Write sed commands to file:

    • Use the echo task to write the generated sed commands to a file.

build.xml example:

XML
<projectname="properties-to-sed"><targetname="convert"><propertyfile="replacements.properties"/><forparam="key"elements="${replacements.keys()}"><sequential><echomessage="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 a convert 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 the sed_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


Daniel Veselka
convert properties file to sed commands using antcontrib for cycle
ChatGPT

To convert a properties file to sed commands using ant-contrib's for loop functionality, you'll need to follow a similar approach as previously mentioned, with some adjustments to implement the loop correctly. Below, I’ll provide a complete and detailed example, including how to set up your environment to use ant-contrib for iterating through properties.

Example Properties File

Assuming you have a properties file named config.properties:

properties
# config.propertiesapp.name=MyAppapp.version=1.0.0app.author=John Doe

Ant Build File
<project name="PropertiesToSed" default="convert" basedir=".">
    
    <property file="config.properties"/>
    
    <target name="convert">
        <echo message="Converting properties to sed commands..." />

        <!-- Loop through the properties defined in config.properties -->
        <property name="properties.list" value="app.name,app.version,app.author"/>

        <foreach list="${properties.list}" param="property">
            <sequential>
                <echo message="sed -i 's/${property}=.*/${property}=${property}:${property}/g' target_file.txt" />
            </sequential>
        </foreach>
        
    </target>
    
</project>

Explanation of the Ant Build File

  1. Task Definition for Ant-Contrib: The line <taskdef resource="antcontrib.properties" /> defines the ant-contrib functions so you can use them in your Ant script. Make sure you have the ant-contrib.jar in your Ant classpath.

  2. Loading Properties: The properties file is loaded into the project with <property file="config.properties"/>.

  3. Defining Properties List: A property properties.list is defined, listing all the keys in the properties file that you want to generate sed commands for.

  4. Using <for> Loop: The <for> loop is used to iterate over the properties list. Each property will be accessible via the variable ${property} in the loop.

  5. Generating sed Commands:

    • The echo command inside the loop outputs the sed command that will perform the desired replacement in target_file.txt.
    • If you want to replace the value of each property in the file, you can use ${property} to get the key and ${property.value} to retrieve the corresponding value.

Step to Execute the Ant Script

  1. Ensure you have ant-contrib: Download the ant-contrib.jar and place it in the lib directory of your Ant installation or add it to your classpath.

  2. Run the Ant command:

bash
ant convert

Final Notes

  • The generated sed command will replace the line in target_file.txt that matches the pattern property=, updating it with the value from config.properties.
  • Adjust the target_file.txt with your actual target file; this is the file where the properties should be replaced.
  • Remember that you can customize the sed command based on how you want to implement the replacements based on your specific requirements.

 


Viewing all articles
Browse latest Browse all 181

Trending Articles