You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by SteveB <x1...@passeridae.de> on 2012/11/25 14:08:03 UTC

include via script task

Hi all,

 

I'm trying to call ant's include task inside a <script> element, like 

  

<script language="beanshell">

    <![CDATA[

        import org.apache.tools.ant.taskdefs.ImportTask;

       

        ImportTask includeTask = project.createTask("include");

        includeTask.setFile("./global-properties.xml");

        includeTask.perform();

    ]]>

</script>

 

This fails with the error  "import only allowed as a top-level task".

 

Anyone knows how to create a "top level task" inside a <script> element ? 

 

Thanks,

 

Steve.

 

 

 

 


AW: include via script task

Posted by SteveB <x1...@passeridae.de>.
Hi Jan,

thanks for the code. I thought there was a "clean API way" to do this, but
I'll try your approach.

Regards.


-----Ursprüngliche Nachricht-----
Von: Matèrne, Jan (RZF, SG 481) [mailto:jan.materne@fv.nrw.de] 
Gesendet: Thursday, November 29, 2012 1:47 PM
An: Ant Developers List
Betreff: AW: include via script task

I played a little bit and the solution is to tweak the Ant API ...
This examples works for me, but using <import> or <include> via this way is
not recommended/supported ;-) Have fun - but be aware of "side effects" ...


Cheers
Jan


imported.xml
<project name="imported">
  <echo>Hello from imported file: ${ant.file}</echo> </project>  

build.xml
<project>

  <script language="beanshell">
      <![CDATA[
          import org.apache.tools.ant.taskdefs.ImportTask;
          import org.apache.tools.ant.Target;

          // Create an initialize the <include> (which is the same as
<import>,
          // see taskdef-mapping:
http://svn.apache.org/repos/asf/ant/core/trunk/src/main/org/apache/tools/ant
/taskdefs/defaults.properties  
          ImportTask includeTask = project.createTask("include");
          
          // Set the file to include
          includeTask.setFile("./imported.xml");
          
          // now tweak the Ant API by setting a non-null owning Target with
an empty name 
          //
http://svn.apache.org/repos/asf/ant/core/trunk/src/main/org/apache/tools/ant
/taskdefs/ImportTask.java
          //  public void execute() {
          //    ...
          //    if (getOwningTarget() == null
          //      || !"".equals(getOwningTarget().getName())) {
          //      throw new BuildException("import only allowed as a
top-level task");
          //    }
          Target helpTarget = new Target();
          helpTarget.setName("");
          includeTask.setOwningTarget(helpTarget);
          
          // For further execution the <import> requires to now its
location, so we
          // pass the <script> ones.
          includeTask.setLocation(self.getLocation());
          
          // and go ...
          includeTask.perform();
      ]]>
  </script>
  
</project>



-----Ursprüngliche Nachricht-----
Von: SteveB [mailto:x1000@passeridae.de]
Gesendet: Sonntag, 25. November 2012 14:08
An: dev@ant.apache.org
Betreff: include via script task

Hi all,

 

I'm trying to call ant's include task inside a <script> element, like 

  

<script language="beanshell">

    <![CDATA[

        import org.apache.tools.ant.taskdefs.ImportTask;

       

        ImportTask includeTask = project.createTask("include");

        includeTask.setFile("./global-properties.xml");

        includeTask.perform();

    ]]>

</script>

 

This fails with the error  "import only allowed as a top-level task".

 

Anyone knows how to create a "top level task" inside a <script> element ? 

 

Thanks,

 

Steve.

 

 

 

 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org For additional
commands, e-mail: dev-help@ant.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


AW: include via script task

Posted by Matèrne, "Jan (RZF, SG 481)" <ja...@fv.nrw.de>.
I played a little bit and the solution is to tweak the Ant API ...
This examples works for me, but using <import> or <include> via this way is not recommended/supported ;-)
Have fun - but be aware of "side effects" ...


Cheers
Jan


imported.xml
<project name="imported">
  <echo>Hello from imported file: ${ant.file}</echo>
</project>  

build.xml
<project>

  <script language="beanshell">
      <![CDATA[
          import org.apache.tools.ant.taskdefs.ImportTask;
          import org.apache.tools.ant.Target;

          // Create an initialize the <include> (which is the same as <import>,
          // see taskdef-mapping: http://svn.apache.org/repos/asf/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/defaults.properties  
          ImportTask includeTask = project.createTask("include");
          
          // Set the file to include
          includeTask.setFile("./imported.xml");
          
          // now tweak the Ant API by setting a non-null owning Target with an empty name 
          // http://svn.apache.org/repos/asf/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ImportTask.java
          //  public void execute() {
          //    ...
          //    if (getOwningTarget() == null
          //      || !"".equals(getOwningTarget().getName())) {
          //      throw new BuildException("import only allowed as a top-level task");
          //    }
          Target helpTarget = new Target();
          helpTarget.setName("");
          includeTask.setOwningTarget(helpTarget);
          
          // For further execution the <import> requires to now its location, so we
          // pass the <script> ones.
          includeTask.setLocation(self.getLocation());
          
          // and go ...
          includeTask.perform();
      ]]>
  </script>
  
</project>



-----Ursprüngliche Nachricht-----
Von: SteveB [mailto:x1000@passeridae.de] 
Gesendet: Sonntag, 25. November 2012 14:08
An: dev@ant.apache.org
Betreff: include via script task

Hi all,

 

I'm trying to call ant's include task inside a <script> element, like 

  

<script language="beanshell">

    <![CDATA[

        import org.apache.tools.ant.taskdefs.ImportTask;

       

        ImportTask includeTask = project.createTask("include");

        includeTask.setFile("./global-properties.xml");

        includeTask.perform();

    ]]>

</script>

 

This fails with the error  "import only allowed as a top-level task".

 

Anyone knows how to create a "top level task" inside a <script> element ? 

 

Thanks,

 

Steve.

 

 

 

 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org