Say I have the following in my build.xml file:
<target name="FOO">
<echo>a
b
c
d</echo>
</target>
The result is:
[echo] a
[echo] b
[echo] c
[echo] d
I want the output to be:
[echo] a
[echo] b
[echo] c
[echo] d
To get that, I could change build.xml to:
<target name="FOO">
<echo>a
b
c
d</echo>
</target>
But is there anything else I can do to get the same output? Anything that keeps the build.xml indentation the same as the original, that is.
One of the solution is to use line.separator property
<property name="nl" value="${line.separator}"/>
<target name="FOO">
<echo>a${nl}b${nl}c${nl}d</echo>
</target>
Output:
FOO:
[echo] a
[echo] b
[echo] c
[echo] d
Second solution is to create custom ANT task, that will provide output as you need (your custom java code will modify ant output massage). See http://www.developer.com/java/article.php/3630721/Introduction-to-Custom-Ant-Tasks.htm
Related
I have a csv/txt file with below entries
abc,123
xyz,678
ijk,921
I'm trying to read the file through for loop and assign first element to var1, and second to var2
var1=abc
var2=123
I need to use these variables to perform some task and return back to the loop to read the next line of the file and assign new values.
Below is what I have, and I'm not able to assign the variable
<loadfile property="message" srcFile="test.txt" />
<target name="compile">
<for param="line" list="${message}" delimiter="${line.separator}">
<sequential>
<echo>#{line}</echo>
<propertyregex property="var1"
input="#line"
regexp="/^(.+?),(.+)/"
select="\1" />
<echo message="${var1}" />
</sequential>
</for>
</target>
Below is the output I get, showing no assignment of value to var1.
[echo] abc,123
[echo] ${var1}
[echo] xyz,678
[echo] ${var1}
[echo] ijk,921
[echo] ${var1}
You have a couple of errors, the below loop body works for me, see output at the end.
The first problem is that the #{line} parameter substitution syntax in the for loop uses an antcontrib macrodef, and the {} brakets are not optional - you had missed them out of the input="" attribute, whereas in the echo task you had included them.
The second problem is that you need not specify enclosing slashes /../ in the rexexp= attribute.
<echo>#{line}</echo>
<propertyregex property="var1"
override="yes"
input="#{line}"
regexp="^(.+?),(.+)"
select="\1"/>
<propertyregex property="var2"
override="yes"
input="#{line}"
regexp="^(.+?),(.+)"
select="\2"/>
<echo message="${var1},${var2}" />
Output:
[echo] abc,123
[echo] abc,123
[echo] xyz,678
[echo] xyz,678
[echo] ijk,921
[echo] ijk,921
I have a Java application that I am packaging into a JAR. I created an Ant script to do that as I need to also add resources to the JAR (icons etc).
Now, I have libraries that I use in my project (Apache HttpClient and a JSON library). I also copy their contents into the JAR, as it is the simplest way.
My build file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="create_run_jar"
name="Create Runnable Jar">
<target name="create_run_jar">
<jar destfile="C:/Users/Pietu1998/Documents/Java/Whatever.jar"
filesetmanifest="mergewithoutmain">
<manifest>
<attribute name="Main-Class"
value="net.pietu1998.whatever" />
<attribute name="Class-Path" value="." />
</manifest>
<fileset dir="C:/Users/Pietu1998/Documents/Java/Whatever/bin"
includes="*.class" />
<fileset dir="C:/Users/Pietu1998/Documents/Java/Whatever/res" />
<zipfileset excludes="META-INF/**"
src="C:/Users/Pietu1998/Documents/Java/Whatever/lib/commons-codec-1.6.jar" />
<zipfileset excludes="META-INF/**"
src="C:/Users/Pietu1998/Documents/Java/Whatever/lib/commons-logging-1.1.3.jar" />
<!-- More (like 5) JARs -->
</jar>
</target>
</project>
However, the libraries (JARs) have their own META-INF folders, and they have a lot of stuff in them; files like LICENSE, CONDITIONS and also a folder for Maven called maven.
The project is for personal use, so I just want to get rid of the unnecessary stuff. I have tried some ways to exclude all of the META-INF stuff, but something is always left behind.
excludes="META-INF" leaves everything.
excludes="META-INF/**" leaves the maven folder.
**/excludes="META-INF/**", see above.
I believe I could use a lot of include-excludes or patternsets, but it would lead to a lot of repeating.
Is there a way (not for this specific case) to exclude a folder (META-INF here) and all its contents including subdirectories, and preferably with not too much repeating (for a lot of libraries)?
Using excludes="META-INF/**"" works for me (Ant 1.9.3, Windows 7, JDK 1.7.0_51)
Some test after downloading original commons-logging-1.1.3.jar from here :
<project>
<zipfileset excludes="META-INF/**" src="c:/area51/commons-logging-1.1.3.jar" id="foobar"/>
<!-- for output line by line -->
<pathconvert property="foo" refid="foobar">
<map from="c:/area51/commons-logging-1.1.3.jar:" to="${line.separator}"/>
</pathconvert>
<echo>${foo}</echo>
</project>
output contains only classfiles under org/apache/commons/logging/.. :
[echo] commons-logging-1.1.3.jar
[echo] org/apache/commons/logging/Log.class;
[echo] org/apache/commons/logging/LogConfigurationException.class;
[echo] org/apache/commons/logging/LogFactory$1.class;
[echo] org/apache/commons/logging/LogFactory$2.class;
[echo] org/apache/commons/logging/LogFactory$3.class;
[echo] org/apache/commons/logging/LogFactory$4.class;
[echo] org/apache/commons/logging/LogFactory$5.class;
[echo] org/apache/commons/logging/LogFactory$6.class;
[echo] org/apache/commons/logging/LogFactory.class;
[echo] org/apache/commons/logging/LogSource.class;
[echo] org/apache/commons/logging/impl/AvalonLogger.class;
[echo] org/apache/commons/logging/impl/Jdk13LumberjackLogger.class;
[echo] org/apache/commons/logging/impl/Jdk14Logger.class;
[echo] org/apache/commons/logging/impl/Log4JLogger.class;
[echo] org/apache/commons/logging/impl/LogFactoryImpl$1.class;
[echo] org/apache/commons/logging/impl/LogFactoryImpl$2.class;
[echo] org/apache/commons/logging/impl/LogFactoryImpl$3.class;
[echo] org/apache/commons/logging/impl/LogFactoryImpl.class;
[echo] org/apache/commons/logging/impl/LogKitLogger.class;
[echo] org/apache/commons/logging/impl/NoOpLog.class;
[echo] org/apache/commons/logging/impl/ServletContextCleaner.class;
[echo] org/apache/commons/logging/impl/SimpleLog$1.class;
[echo] org/apache/commons/logging/impl/SimpleLog.class;
[echo] org/apache/commons/logging/impl/WeakHashtable$1.class;
[echo] org/apache/commons/logging/impl/WeakHashtable$Entry.class;
[echo] org/apache/commons/logging/impl/WeakHashtable$Referenced.class;
[echo] org/apache/commons/logging/impl/WeakHashtable$WeakKey.class;
[echo] org/apache/commons/logging/impl/WeakHashtable.class
Maybe you forgot to set the update attribute from your jar task to true :
<jar destfile=".." update="true" ..>
to overwrite some already existing jarfile with the same name !?
I found a way to do it with the stars.
<zipfileset excludes="META-INF/**/*" src="C:\library.jar" />
This excludes the META-INF folder and all its contents.
* means it excludes all files inside a folder, and
foo/**/bar means it excludes bar in any level inside foo.
So, foo/**/* excludes every file on every level inside foo.
I have a property and target in my build.xml:
<property name="somedir" value="path/to/dir/${prop}"/>
<echo message="${prop}"/>
<target name="foo">
<echo message="Property: ${somedir}"/>
</target>
In the directory where build.xml is I run:
ant -Dprop="someVal" foo
This gets echoed:
[echo] someVal
[echo] Property: path/to/dir/
What happened to ${prop} when foo is called? How do I get the value to persist when foo is invoked?
Thanks in advance!
I can't reproduce your issue. What version of ANT are you using?
build.xml
<project name="demo" default="foo">
<property name="somedir" value="path/to/dir/${prop}"/>
<echo message="${prop}"/>
<target name="foo">
<echo message="Property: ${somedir}"/>
</target>
</project>
Run as follows:
$ ant -Dprop=hello
Buildfile: /home/me/tmp/build.xml
[echo] hello
foo:
[echo] Property: path/to/dir/hello
ANT version:
$ ant -version
Apache Ant(TM) version 1.9.0 compiled on March 5 2013
I'm using Ant version 1.8.4
I have this as my build.xml
<project>
<property name="somedir" value="path/to/dir/${prop}"/>
<echo message="${prop}"/>
<target name="foo">
<echo message="Property: ${somedir}"/>
</target>
</project>
I get this:
$ ant -Dprop=foo foo
Buildfile: /Users/david/build.xml
[echo] foo
foo:
[echo] Property: path/to/dir/foo
BUILD SUCCESSFUL
Total time: 0 seconds
I had to put <project> and </project> in the build.xml or else it wouldn't execute.
Is there something I'm missing? It seems to work fine.
I was trying to overwrite a pre-defined property that is immutable from the build.xml ...this is more along the lines of what I'm trying to do:
<project>
<property name="basedir" value="/usr/me/${prop}"/>
<echo message="${basedir}"/>
</project>
echos:
Buildfile: build.xml
[echo] /usr/me
BUILD SUCCESSFUL
Total time: 0 seconds
facepalm
I have a file has many lines such as:
aaa bbb ccc ddd eeee
a111 b111 c111 d111
A222 B222 X222 Y222 Z222
FFF GGG HHH
etc
I hope to use ant to extract all substring in this file
aaa bbb ccc ddd eeee
a111 b111 c111 d111
A222 B222 X222 Y222 Z222
FFF GGG HHH
in each line, ie
all aaa bbb ccc ddd eee in 1st line,
all FFF GGG HHH in last line etc
Pls help how to do it but not using regex???
My code is:
<for list="#{line}" delimiter=" " param = "val">
<sequential>
<echo>in sequential : #{line}</echo>
<var name="first.part" value="1"/>
<var name="second.part" value="2"/>
<var name="third.part" value="3"/>
<propertyregex
property="first.part"
input="#{line}"
select="\0"/>
<propertyregex
property="second.part"
input="#{line}"
select="\1"/>
<propertyregex
property="third.part"
input="#{line}"
select="\2"/>
<echo> after assign: 1st.part = ${first.part}</echo>
<echo> after assign: 2nd.part = ${second.part}</echo>
<echo> after assign: 3rd.part = ${third.part}</echo>
</sequential>
</for>
But ant 1.6 did not support regex,
I got error:
The type doesn't support the "regex" attribute.
how to fix it???
Thanks
ANT is not a programming language. You are best advised to embed one.
The following example uses groovy:
<project name="demo" default="scan">
<target name="bootstrap">
<mkdir dir="${user.home}/.ant/lib"/>
<get dest="${user.home}/.ant/lib/groovy.jar" src="http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.1.3/groovy-all-2.1.3.jar"/>
</target>
<target name="scan">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
<groovy>
new File("File1.txt").eachLine {
def values = it.split()
ant.echo "Extracted values: ${values}"
values.each {
ant.echo "Need to so something with ${it}"
}
}
</groovy>
</target>
</project>
Produces the following output
scan:
[echo] Extracted values: [aaa, bbb, ccc, ddd, eeee]
[echo] Need to so something with aaa
[echo] Need to so something with bbb
[echo] Need to so something with ccc
[echo] Need to so something with ddd
[echo] Need to so something with eeee
[echo] Extracted values: [a111, b111, c111, d111]
[echo] Need to so something with a111
[echo] Need to so something with b111
[echo] Need to so something with c111
[echo] Need to so something with d111
[echo] Extracted values: [A222, B222, X222, Y222, Z222]
[echo] Need to so something with A222
[echo] Need to so something with B222
[echo] Need to so something with X222
[echo] Need to so something with Y222
[echo] Need to so something with Z222
[echo] Extracted values: [FFF, GGG, HHH]
[echo] Need to so something with FFF
[echo] Need to so something with GGG
[echo] Need to so something with HHH
Update
An alternative way to enable the groovy task is to use a classpath reference as follows:
<path id="build.path">
<pathelement location="/path/to/groovy-all/jar/groovy-all-2.1.3.jar"/>
</path>
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
I have two deployment configurations.
Each configuration store in its property file.
Dev configuration in dev.properties:
deploy.wowza.domain=DEV_IP_ADDRESS
Prod configuration in prod.properties:
deploy.wowza.domain=PROD_IP_ADDRESS
I have build.xml
<?xml version="1.0"?>
<project name="MAIN" default="dev" basedir=".">
<target name="dev">
<property file="${java.root.dir}/ant/dev.properties"/>
<echo>
DEV
${deploy.wowza.domain}
</echo>
<sleep seconds="1"/>
</target>
<target name="prod">
<property file="${java.root.dir}/ant/prod.properties"/>
<echo>
PROD
${deploy.wowza.domain}
</echo>
<antcall target="deploy"/>
</target>
</project>
If I run prod or dev task it show correct property value only from second run
D:\Dropbox\camwithme>ant prod
Buildfile: D:\Dropbox\camwithme\build.xml
prod:
[copy] Copying 1 file to D:\Dropbox\camwithme\wowza_cam\ant
[echo]
[echo] PROD
[echo] PROD_IP_ADDRESS
[echo]
BUILD SUCCESSFUL
Total time: 0 seconds
D:\Dropbox\camwithme>ant dev
Buildfile: D:\Dropbox\camwithme\build.xml
dev:
[copy] Copying 1 file to D:\Dropbox\camwithme\wowza_cam\ant
[echo]
[echo] DEV
[echo] PROD_IP_ADDRESS !!! Should be dev ip here !!!
[echo]
BUILD SUCCESSFUL
Total time: 0 seconds
D:\Dropbox\camwithme>ant prod
Buildfile: D:\Dropbox\camwithme\build.xml
prod:
[copy] Copying 1 file to D:\Dropbox\camwithme\wowza_cam\ant
[echo]
[echo] PROD
[echo] DEV_IP_ADDRESS
[echo]
Problem was because of another build.xml which load properties files before copy