Run Ant script from Gradle

后端 未结 2 1984
清酒与你
清酒与你 2021-01-15 13:16

I\'m trying to run ant script from Gradle, but I have this exception:

  Cause: the class org.apache.tools.ant.taskdefs.optional.junit.JUnitTask was not found         


        
2条回答
  •  隐瞒了意图╮
    2021-01-15 13:42

    I have fixed the following Gradle error:

    Execution failed for task ':test'.
    > Problem: failed to create task or type junit
      Cause: the class org.apache.tools.ant.taskdefs.optional.junit.JUnitTask was not found.
              This looks like one of Ant's optional components.
      Action: Check that the appropriate optional JAR exists in
              -ANT_HOME/lib
              -the IDE Ant configuration dialogs
    

    by adding the following lines in my Ant script build.xml just before my Ant task

    
    

    This is the full Ant target test:

    
      
    
      
      
    
      
          
          
              
                  
              
          
      
    
    

    The answer about "JUnitTask was not found" from oers has been a useful help to find my above fix.

    Identify the issue

    To understand the issue, I have tweaked the scripts build.xml and build.gradle in order to compare the Ant and Gradle outputs. See also the answer of How to get “gradle” have similar output details as “ant”? for some details.

    The interesting difference is in the -classpath parameter:

    Using Ant

    /some/path/myApplication.jar
    /usr/share/java/ant-launcher-1.9.7.jar
    /usr/share/ant/lib/ant.jar
    /usr/share/ant/lib/junit.jar
    /usr/share/ant/lib/ant-junit.jar
    /usr/share/ant/lib/ant-junit4.jar
    

    Using Gradle

    /some/path/myApplication.jar
    /usr/lib/gradle/3.2.1/lib/ant-launcher-1.9.6.jar
    /usr/lib/gradle/3.2.1/lib/ant-1.9.6.jar
    

    Difference

    We can observe that Gradle uses its own ant-launcher-1.9.6.jar and ant-1.9.6.jar. But Gradle does not use the JARs from Ant installation (/usr/share/ant/lib/*jar are symbolic links to /usr/share/java/*jar).

    Long term

    In this discussion about integration of Ant JUnit taskdef, Luke Daley confirms in Novembre 2013;

    Gradle uses its own 'instance' of ant and does not bother using what's installed at $ANT_HOME

    In this discussion about JUnit 5 Eric Wendelin expressed the priority to integrate more JUnit within Gradle core.

    Threfore, solution for long term is to stop using JUnit from Ant scripts and convert them into Gradle syntax.

提交回复
热议问题