Ant build target to list own build targets - ant

I am brand new to Ant. I would like to have the following:
<target name="targets" description "Lists Targets">
[some Ant code/tasks]
</target
When I call ant targets I want Ant to search the current file that Ant is running for all it's targets
<target name="all" depends="doc,sca,compile,test" description="Complete build">
</target>
And output
[echo] all - Complete Build - Depends on: doc, sca, compile, test
[echo] doc - Builds documentation
[echo] compile - Compiles files
[echo] sca - Performs static code analysis
[echo] test - Runs unit tests - Depends on: compile
[echo] clean - Cleans a build
[echo] install - Performs an installation
[echo] targets - Lists Targets
In Bash or Make I'd just do a grep regular expression. The following, in Make, lists the make targets:
noop: ;
targets:
#$(MAKE) --print-data-base --question noop | \
grep -v "[A-Z]:/" | \
awk '/^[Mm]akefile:/ { next } \
/^targets:/ { next } \
/^noop:/ { next } \
/^[^.%!][-A-Za-z0-9_]*:/ { print substr($$1, 1, length($$1)-1) }' | \
sort | \
pr --omit-pagination --width=80 --columns=4
Output:
[matt#office burning-boots-website]$ make -C ~/workspace/packages/ targets
all diagnose install vca_core
clean doc platforms vca_counting_line
compile help utils vca_rules
It would be nice to have something simliar in my Ant build script! would be especially nice if Ant could do the nice alignment that pr does - or even colour the output! Can Ant do cross platform terminal colour changes?

You could just use ant -p (or ant -p -v to get more information). This won't list the target dependencies, but I don't see it as a problem: dependencies are not important for the end user (they just tell how the target works). What's important is what the target does, which is what should be in its description.

Related

Why do I get BUILD SUCCESSFUL while I had an error?

I am experimenting with Jenkins and Ant. I would like to simply run my Makefile that does everything, the build and the tests.
I figured out that the best way is to use Ant because I become flexible with my build process similarly to travis.yml.
Unfortunately the compiler I am using only exists on Windows so I installed Jenkins on Windows. I wrote this build.xml
<?xml version="1.0"?>
<project name="Hello World Project" default="info">
<target name="info">
<echo>Hello World - Welcome to Apache Ant!</echo>
<exec executable="make"/>
</target>
</project>
And the output I get so far is this one:
C:\Program Files (x86)\Jenkins\workspace\test>exit 0
[test] $ cmd.exe /C "ant.bat info && exit %%ERRORLEVEL%%"
Unable to locate tools.jar. Expected to find it in C:\Program Files\Java\jre1.8.0_131\lib\tools.jar
Buildfile: C:\Program Files (x86)\Jenkins\workspace\test\build.xml
info:
[echo] Hello World - Welcome to Apache Ant!
[exec] rm -f test_*.s
[exec] arm-none-eabi-gcc.exe -O2 -Wall -S -c test.c -o test_gcc.s
[exec] make: arm-none-eabi-gcc.exe: Command not found
[exec] make: *** [Makefile:9: test_gcc.s] Error 127
[exec] Result: 2
BUILD SUCCESSFUL
Total time: 0 seconds
Finished: SUCCESS
Why do I get a BUILD SUCCESSFUL status while I got an Error?
N.B. I know I have to configure my PATH to include the toolchain. I would like to first understand this inconsistency.
By default, Ant's exec task does not fail the build when an error code is returned. However, this can simply be switched on with the failonerror attribute:
<exec executable="make" failonerror="true" />

Running an executable from a different working directory using ant

Is it possible to run an executable from ant using a working directory other than the one which contains the executable? This small ant project demonstrates what I'm trying to achieve.
Folder structure
ant test
| build.xml
| bin
| | debug
| | | program.exe
| | release
| | | program.exe
| | inputs
| | | ...
| | outputs
| | | ...
build.xml
<project name="test" basedir="./">
<target name="run">
<exec executable="${configuration}\program.exe" dir="bin"/>
</target>
</project>
${configuration} is set to release.
bin needs to be the working directory so the executable can reference files in inputs and outputs correctly, so I want to be able to run the executable contained in bin/release from the bin directory. However, ant fails to find the executable:
BUILD FAILED
D:\ant test\build.xml:6: Execute failed: java.io.IOException: Cannot run program "release\program.exe" (in directory "D:\ant test\bin"): CreateProcess error=2, The system cannot find the file specified
I am able to work around this on windows by launching cmd.exe in the bin directory and then passing it the parameter release\program.exe, but I need to be able to use the ant file on multiple platforms, so I was hoping to find a consistent syntax.
One option I considered was using conditional tasks and having separate syntaxes for windows and unix. However, in the actual project, the exec statement occurs inside a macro, and the target calls the macro a large number of times. Since conditions can only affect things at the target level, I would have to duplicate the long list of macro calls for each syntax I wanted to target, e.g.
<target name="runAll" depends="runAll-win,runAll-unix"/>
<target name"runAll-win" if="win">
<myMacro-win .../>
<!-- Huge list of macro calls -->
...
<myMacro-win .../>
</target>
<target name"runAll-unix" if="unix">
<myMacro-unix .../>
<!-- Huge list of macro calls -->
...
<myMacro-unix .../>
</target>
The dir attribute specifies the working directory but the executable path is resolved against the project basedir. Hence the task will search for the executable D:\ant test\release\program.exe.
A simple solution is to add resolveexecutable="true" to the task call:
<target name="run">
<exec executable="${configuration}\program.exe" dir="bin" resolveexecutable="true" />
</target>
From the exec task documentation:
When this attribute is true, the name of the executable is resolved firstly against the project basedir and if that does not exist, against the execution directory if specified. On Unix systems, if you only want to allow execution of commands in the user's path, set this to false. since Ant 1.6

How i can add phing task for automated parsing my project and cresting a PO for translation?

How i can add phing task for automated parsing my project and creating a PO for translation?
You can use xgettext to collect all gettext-translatable strings:
http://sourceforge.net/p/semanticscuttle/code/ci/cb4b0469ca48d9865c8b162c1446d9011adf249b/tree/scripts/update-translation-base.php#l11
$ xgettext -kT_ngettext:1,2 -kT_ -L PHP -o data/locales/messages.po src/
Execute that from your phing task.

How to execute Ant without buildfile

I'm working on a project to develop a custom Ant task.
As part of automated acceptance testing, I'd like to execute Ant from JUnit (the opposite of the usual desire) and pass it a string containing certain build XML to be tested via a command line param or stdin or pipe or something like that, rather than referring it to a buildfile on disk.
Is there any feasible way to do this?
Ant expects a file as input. You can however use the -f parameter to specify a tempfile as input:
$ cat <<EOF > tmp1.xml
<project name="demo" default="hello">
<target name="hello">
<echo>hello world</echo>
</target>
</project>
EOF
$ ant -f tmp1.xml
Obviously from Junit you're more likely the write the XML from Java :-)

Compiling java programs from CLI using javac

I have the following directory structure form where I am invoking javac
src/ lib/ build/
Under src:
src/com/xyz/App.java -- contains the main class
src/com/xyz/base/j1.java
src/com/xyz/base/j2.java
src/com/xyz/exceptions/e1.java
src/com/xyz/hibernate/factory/hbf1.java
src/com/xyz/hibernate/helper/hbh1.java
Under lib:
lib/hibernate.jar
lib/commons.jar
At the top level, I am using the following javac command:
javac -verbose -classpath lib/hibernate.jar:lib/commons.jar -d ./build -sourcepath ./src com/xyz/*.java
and I receive the following output
javac: No match
How should the args be passed to javac?
And here is the ANSWER:
javac -verbose -d build -classpath lib/commons.jar:lib/hibernate.jar [complete path for ALL the directories]/*.java
javac won't expand wildcards, that's what your shell does. so when you specify com/xyz/*.java , that will not expand to anything, as those files are under src/ but the shell doesn't know that. If you list out every java file as com/xyz/Foo.java com/xyz/Bar.java etc, it should work.
(note that if you're on windows you'll need ; and not : to seperate classpaths)
Something like this might work:
javac -verbose -classpath build:lib/hibernate.jar:lib/commons.jar -d ./build ./src/com/xyz/base/*java ./src/com/xyz/exceptions/*.java ./src/com/xyz/hibernate/factory/*.java ./src/com/xyz/*.java
I'd not do this other than as an exercise on how to compile from a command line, otherwise use a build tool like ant

Resources