How can i import ant build files that are generating in progress

ant.importBuild (‘prototype/all-custom-make.xml’)

the all-custom-make.xml will be created after starting gradle build , so at the first time gradle parse the code and appear the error that this file does not exist to import it , even the directory prototype . Who can help me???

Please post as much of your build script as you can? All of it, preferably. At the very least, the task block or configuration block that contains:

  1. Your ant.importBuild (‘prototype/all-custom-make.xml’)
  2. The statement that creates the prototype directory and the statement that creates the XML file

The prototype directory, and the file xml are created by the engine with Ant , it is created when i start the generation with gradle , and i want to use it from gradle ! At the first time , gradle parse the code when i start generation and can not continue because at that moment , the directory and the file are not yet created !! , there is any solution to stop parsing code ? or any idea to resolve this problem !

Without seeing your code, your description sounds like you might want to look into doLast():

Without seeing your actual build script though, that’s just a guess. So you might want to get a second opinion on that. But it’s worth a shot.

EDIT: I should probably point out that the parse tag in my example is made-up. It’s meant to represent whatever actual task you’re using to do the „parse the code when i start generation“ stuff you described.

1 Like

My code is so specific, so ,i wanted to describe the situation.
this task will use targets from ant build file , which is created after starting generation with gradle !
i already used “doLast” but it didn’t resolve the problem.

task enhance_jpa(type:Jar){
description =‘enhance model sources with JPA configuration’
enhance_jpa.dependsOn(‘create-task-jpa’)

//this file does not exist , it will be created when gradle execute this task
ant.importBuild (‘prototype/all-custom-make.xml’){antTargetName ->
‘ant-’ + antTargetName
}
ant.importBuild(’./user/build/custom-make.xml’) //this file exists before start generation with gradle
enhance_jpa.dependsOn(‘beforeEnhance’)

enhance_jpa.dependsOn(‘sub-enhance-jpa-builtin’)
enhance_jpa.dependsOn(‘sub-enhance-jpa-third-party’)

from(sourceSets.SourceSetModel.output){
archivesBaseName = ‘model’
destinationDir = file(’./prototype/lib’)
}

from(’./prototype/jpa’){
include ‘*.properties’
include ‘ehcache.xml’
include ‘hibernate.cfg.xml’
archivesBaseName = ‘model’
}

into (’/META-INF’) {
from (’./prototype/jpa’) {
include ‘persistence.xml’
include ‘orm-*.xml’
archivesBaseName = ‘model’
}}

enhance_jpa.dependsOn(‘add.external.jar.content’)
enhance_jpa.dependsOn(‘afterEnhance’)
}

1 Like

By that, do you mean this line creates that prototype/all-custom-make.xml file?

enhance_jpa.dependsOn(‘create-task-jpa’)

Probably yes , when gradle start execution , it will be created , just after parsing code

Hi. What is the exact error message that you get, please?

Could not import Ant build file ‘E:\git\PetiteEnfanceGradle\prototype\all-custom-make.xml’

Thanks. So it doesn’t say the file or the prototype directory does not exist?

I can reproduce that exact same error message if I have an empty file in my E:\git\PetiteEnfanceGradle\prototype\all-custom-make.xml. That is to say, the directory exists. And the XML file exists. But the XML file is empty.

Is there anything at all in your prototype directory?

I created the prototype directory manually , that’s why it exists , but the file is not exists so it couldn’t import it .

What is create-task-jpa? Where is it defined?

I have attempted to simulate what I understand from the script you pasted above.

I followed these steps:

  1. Create a new empty directory

  2. Copy build.xml and build.gradle from below into that new directory

  3. From the command line, run

    gradle enhance_jpa
    
  4. I got a Basedir /{project_directory}/prototype does not exist error at the line of the ant.importBuild() call

  5. I then wrapped ant.importBuild ('prototype/all-custom-make.xml') inside an enclosing doLast{} block

  6. The build succeeded after that

The build script I pasted below assumes the create-task-jpa is where the XML file is being created. That is why I asked: „What is it? Where is it defined?“ Do you know?

If my assumption is correct then something may be going wrong in your create-task-jpa. Whatever it is.


build.xml:


<project>
    <target name="hello">
        <echo>Hello, from Ant</echo>
    </target>
</project>

build.gradle:


task enhance_jpa{

    description ='enhance model sources with JPA configuration'

    enhance_jpa.dependsOn('create_task_jpa')

    doLast{    
        ant.importBuild ('prototype/all-custom-make.xml')
    }

    println "ant.importBuild ('prototype/all-custom-make.xml') succeeded"

}

task create_task_jpa(type: Copy){

    def destDir = file('prototype')
    
    delete destDir        
    
    assert !destDir.exists()
    
    mkdir destDir
    
    assert destDir.exists()
    
    from file('build.xml')
    
    into destDir
    
    rename{ buildXml -> 'all-custom-make.xml' }
    
    println "create_task_jpa completed"
}

I referred to the Authoring Tasks and the Working With Files sections of the user guide for this.

In this project, I fleshed out more fully the Gradle and Ant build scripts. Now they more closely resemble the script you shared.

This is the result of running that project:

gradle enhance_jpa clean

> Task :add.external.jar.content
[ant:echo] task 'add.external.jar.content' called

> Task :afterEnhance
[ant:echo] task 'afterEnhance' called

> Task :beforeEnhance
[ant:echo] task 'beforeEnhance' called

> Task :create-task-jpa
[ant:echo] target 'create-task-jpa' created 'prototype/all-custom-make.xml'

> Task :sub-enhance-jpa-builtin
[ant:echo] task 'sub-enhance-jpa-builtin' called

> Task :sub-enhance-jpa-third-party
[ant:echo] task 'sub-enhance-jpa-third-party' called

> Task :enhance_jpa
ant.importBuild ('prototype/all-custom-make.xml') succeeded

> Task :clean
[ant:echo] target 'clean' deleted 'prototype' directory

BUILD SUCCESSFUL in 3s
8 actionable tasks: 8 executed

While writing the Ant and Gradle scripts, I noticed that if anything is sketchy about the Ant scripts (empty file, malformed XML, schema-invalid project) then that reproduces your Could not import Ant build file error. So before you import them into build.gradle, you want to make sure that they successfully execute as standalone Ant scripts. That is, run them in Ant first to make sure they are valid Ant scripts.

And also, the doLast{} block seems to be necessary. Without it, I got that error I told you about earlier. With it, the build succeeds.

I used Using Ant from Gradle and Apache Ant™ User Manual as reference guides.