Search This Blog

Showing posts with label ant. Show all posts
Showing posts with label ant. Show all posts

Ant task to join multiple lines text into single line string

  • Option 1: Use temp file:
        <target name="jdk-modules1">
            <exec executable="java" output="jdk.modules.tmp">
                <arg value="--list-modules"/>
            </exec>
            <loadfile property="jdk.modules" srcFile="jdk.modules.tmp">
                <filterchain>
                    <replaceregex pattern="@.*" replace=","/>
                    <striplinebreaks/>
                    <replaceregex pattern=",$" replace=""/>
                </filterchain>
            </loadfile>
            <delete file="jdk.modules.tmp" />
            <echo message="${jdk.modules}" />
        </target>
    
  • Option 2: Use Javascript:
        <target name="jdk-modules2">
            <exec executable="java" outputproperty="jdk.modules">
                <arg value="--list-modules"/>
            </exec>
            <script language="javascript">
                <![CDATA[
                var value = project.getProperty('jdk.modules');
                project.setProperty('jdk.modules', value.replace(/@.*\n/g, ',').replace(/@.*/g, ''));
                ]]>
            </script>
            <echo message="${jdk.modules}"/>
        </target>
    






see also

Find file or directory name in an ANT script.

  1. script for finding file name(fieleNamePrefix) inside the dir
    <fileset dir=”${dirPath}” id= “jarId” >
    <include name= “fieleNamePrefix*.jar” />
    </fileset >
    <property name= “jarName” refid= “jarId” />
    
  2. script for finding folder name(folderNamePrefix) inside the dir
    <dirset dir=”${dirPath}” id=”dirId”>
    <include name=”folderNamePrefix*”/>
    </dirset>
    <property name= “dirName” refid= “dirId” />
    

Ant in Eclipse: javac1.8 class not found

If you are running ANT in Eclipse using Java 8. The error
Class not found: javac1.8
could happen. Because Apache Ant versions less than 1.9.0 - are not compatible with Java 8. The solution could be
  • Option 1: Install Java 7 and set JAVA_HOME for eclipse to use Java 7.
  • Option 2: Add compiler or build.compiler attribute to the javac task in your ant build.xml:
    <target name="compile">
        <javac srcdir="src" destdir="build/classes" source="1.6" target="1.6" compiler="javac1.7" >
            <classpath>
                <fileset dir="lib">
                    <include name="*.jar" />
                </fileset>
            </classpath>
        </javac>
    </target>

see also

ANT: set target jvm in the build.xml file by adding source and target options to the javac task

According to https://ant.apache.org/manual/Tasks/javac.html
 <!-- -->
 <!--Target: compile-->
 <!-- -->
 <target name="compile" depends="init,clean" description="Compile the java source">
  <!-- Compile the java code from ${src} into ${build} -->
  <javac debug="on" debuglevel="lines,vars,source" srcdir="${src}" destdir="${build}" source="1.6" target="1.6">
   <classpath>
    <pathelement location="${commons-jar}" />
   </classpath>
  </javac>
 </target> 

see also

ANT: built-in properties

propertydescription
basedir the absolute path of the project's basedir (as set with the basedir attribute of <project/>).
ant.filethe absolute path of the buildfile.
ant.versionthe version of Ant
ant.project.namethe name of the project that is currently executing; it is set in the name attribute of the .
ant.project.default-targetthe name of the currently executing project's default target; it is set via the default attribute of
ant.project.invoked-targetsa comma separated list of the targets that have been specified on the command line (the IDE, an <ant> task ...) when invoking the current project.
ant.java.versionthe JVM version Ant detected; currently it can hold the values "1.2", "1.3", "1.4", "1.5" and "1.6".
ant.core.libthe absolute path of the ant.jar file.
ant.homehome directory of Ant. It is set by the launcher script and therefore maybe not set inside IDEs:
ant.library.dirthe directory that has been used to load Ant's jars from. In most cases this is ANT_HOME/lib.It is only set if Ant is started via the Launcher class (which means it may not be set inside IDEs either)

ANT: how to append some text to a file

  • To append plain text:
    <echo file="file.txt" append="true">Hello World</echo>
  • To append HTML or XML, you need escape it using CDATA:
    <echo file="file.txt" append="true">
    <![CDATA[
      <h1>Hello World</h1>
    ]]>
    </echo>

ANT: how to create a jar file including service provider configuration files in META-INF/services

According to ANT jar task, you can define your ant jar task like below (to write service provider configuration files in META-INF/services/):
<jar filesonly="true" destfile="${dist}/${ant.project.name}.jar">
    <manifest></manifest>
    <service type="javax.imageio.spi.ImageReaderSpi">
        <provider classname="my.image.My1ImageReaderSpi" />
        <provider classname="my.image.My2ImageReaderSpi" />
    </service>
    <service type="javax.imageio.spi.ImageWriterSpi">
        <provider classname="my.image.My1ImageWriterSpi" />
        <provider classname="my.image.My2ImageWriterSpi" />
    </service>
    <fileset dir="${build}" includes="**/*" />
</jar>