How to group targets in Phing?
Asked Answered
A

2

15

Is there a way to group targets in Phing? I have a number of targets that can be reused by running them in different orders and combinations. For example to create a new dev build:

$ phing clean
$ phing prepare
$ phing build
$ phing runtests

Or to update a dev build from a repo:

$ phing update
$ phing runtests

Is there a way to group these targets to run them as single command? I'm aware that you can chain targets eg:

$ phing clean prepare build runtests
$ phing update runtests

But ideally I'd like to run a command such as:

$ phing cleanbuild

This would run all four targets. This way the build file can separated out into reusable targets. Creating a new target that does this will result in duplication of code from the existing targets. I know that you can use the depends parameter to execute other targets but I want each target to be independent of each other.

The Phing documentation doesn't seem to be very clear on how to do this but I'm sure it's possible to call targets within other targets as it must be a pretty common way of doing things. Many thanks.

Atrioventricular answered 16/7, 2011 at 12:13 Comment(1)
Im very interested in seeing the outcome of this question as I am just looking a using phing myself to automate database migrations etcCrossopterygian
A
28

OK, after a hunting around a bit I found the answer. You can call targets from inside other targets by using the PhingCallTask task. An example of a composite task:

<target name="cleanbuild" description="Runs a clean dev build">
    <phingcall target="clean" />
    <phingcall target="prepare" />
    <phingcall target="build" />
    <phingcall target="runtests" />
</target>

Documentation is here:

http://www.phing.info/docs/guide/stable/apbs25.html

Atrioventricular answered 16/7, 2011 at 14:59 Comment(1)
One thing to note from the documentation: Important note about scope: every <phingcall> tag creates a new local scope. Thus, any properties or other variables set inside that scope will cease to exist (or revert to their previous value) once the <phingcall> tag completes.Teriteria
C
16

Your own answer is fine. Or you can use the depends attribute and make it even shorter:

<target name="cleanbuild" description="Runs a clean dev build" depends="clean, prepare, build, runtests"></target>
Coruscate answered 17/7, 2011 at 21:36 Comment(3)
That's an interesting way of doing it but it's not very readable and could look very messy if you start adding more targets. Good choice for small composite targets though. Cheers.Atrioventricular
I agree regarding the readability. One advantage of depends is that if any of the targets depend on another target if being called separately, you don't get the dependent targets being called twice. Let's say you want to call runtests separately. If it always depends on 'prepare', using <phingcall target="prepare"/> in runtests would result in the 'prepare' target being called 2x when running cleanbuild. If you use 'depends' it gets called only once per phing invocation.Coruscate
I just came across a situation where this approach made complete sense over my answer. Very useful for avoiding running tasks multiple times.Atrioventricular

© 2022 - 2024 — McMap. All rights reserved.