Ant cannot find a class needed by an externally defined taskdef

前端 未结 6 358
闹比i
闹比i 2020-12-31 06:00

I am trying to use the axis-java2wsdl ant task to create a wsdl from one of my java classes, but I cannot get the classpath correct.

I am using Ubuntu\'s libaxis-jav

相关标签:
6条回答
  • 2020-12-31 06:24

    Using the answers from here and all the partial information, I came up with a solution. I added the jar the ant file needed to a lib folder in the project (specifically mysql jdbc drivers). Then I run a setup task in ant that copies to the user's home .ant/lib folder, and then fails ant with a message to restart. It only fails once for a user, and then works every time. It might be tricky if you update the jar to a new version...

    Here's the ant build.xml:

    <project name="data_source" default="build">
    
      <!-- BUILD PROPERTIES -->
      <property file="build.properties"/>
    
      <!-- SQL Server Properties -->
      <property name="sql.driver" value="org.gjt.mm.mysql.Driver"/>
      <property name="sql.url" value="jdbc:mysql://127.0.0.1/datastore"/>
      <property name="sql.user" value="user"/>
      <property name="sql.pass" value="password"/>
    
      <!-- FILE LOCATIONS  -->
      <property name="sql.dir" location="sql"/>
    
      <!-- INITIALIZE PROJECT -->
      <target name="init" description="Initialize the project">
          <available property="no.ant.lib.dir" file="${user.home}/.ant/lib/" type="dir" />
      </target>
    
      <!-- SETUP MYSQL -->
      <target name="setup_mysql" description="Copy the lib so ant can see it" unless="no.ant.lib.dir">
          <mkdir dir="${user.home}/.ant"/>
          <mkdir dir="${user.home}/.ant/lib"/>
          <copy file="lib/mysql-connector-java-5.1.13-bin.jar" todir="${user.home}/.ant/lib"/>
    
          <!-- ant already missed picking up the jar - we have to restart -->
          <fail message="JDBC mysql class copied to ${user.dir}/.ant/lib/ - please restart ant" />
      </target>
    
      <!-- BUILD DATA SOURCES -->
      <target name="build" depends="init,setup_mysql,clean_data" description="Create and populate tables">
    
        <sql driver="${sql.driver}" url="${sql.url}" userid="${sql.user}" password="${sql.pass}" >
          <transaction src="${sql.dir}/create_tables.sql"/>
          <transaction src="${sql.dir}/insert_data.sql"/>
        </sql>
      </target>
    
       <!-- CLEAN PROJECT -->
      <target name="clean" description="Cleans up project build">
        <!-- Don't clean data sources here - may get called by accident -->
      </target>
    
      <!-- Delete all tables and data -->
      <target name="clean_data" description="Deletes all data and tables">
        <echo>Dropping all database tables in ${sql.schema}...</echo>
        <property name="exec.command" value="mysqldump -u${sql.user} -p${sql.pass} --add-drop-table --no-data ${sql.schema} | grep ^DROP | mysql -u${sql.user} -p${sql.pass} ${sql.schema}" />
        <exec executable="sh">
          <arg value = "-c"/>
          <arg value="${exec.command}"/>
        </exec>
    
      </target>
    
    </project>
    

    Hope this helps

    0 讨论(0)
  • 2020-12-31 06:30

    It works for me to specify the classpath directly in the taskdef task, as menstioned by matt b. For my project I find it useful to include the taskdef library in the project folder and specify the classpath in the ant build file, to simply setup on other development pcs. I use the following taskdef:

    <taskdef resource="antenna.properties" classpath="${myprojectroot}/lib/antenna-bin-1.2.1-beta.jar"/>
    

    Note that this might not work for versions of ant earlier than 1.7.0.

    0 讨论(0)
  • 2020-12-31 06:33

    In general, this works. But you need to check very carefully which classes are where.

    If your task class can be loaded in a classloader higher up in the classloader hierarchy (like CLASSPATH or ANT_HOME/lib) then your classpathref will simply get ignored.

    Read the FAQ entry for more details.

    Ant's class loader implementation uses Java's delegation model

    The ClassLoader class uses a delegation model to search for classes and resources. Each instance of ClassLoader has an associated parent class loader. When called upon to find a class or resource, a ClassLoader instance will delegate the search for the class or resource to its parent class loader before attempting to find the class or resource itself. The virtual machine's built-in class loader, called the bootstrap class loader, does not itself have a parent but may serve as the parent of a ClassLoader instance.

    Note: running ant -diagnostics can help too.

    0 讨论(0)
  • 2020-12-31 06:36

    Ant mechanism for adding libraries is:

    • via command line argument -lib
    • adding to ${user.home}/.ant/lib
    • adding to ${ant.home}/lib

    Only. The manual doesn't mention anything about using the system.library.dir property. Probably it pretty much ignored for this purpose.

    Also, run ant in verbose mode ( and -verbose ) to see what is doing under the hood.

    0 讨论(0)
  • 2020-12-31 06:39

    I met the same problem when I copy a *.jar to the {ant.home}/libs, then i user the -lib to locate the *.jar, it's runs ok! I consider it's the new jars can't be loaded, then i'll test it!

    0 讨论(0)
  • 2020-12-31 06:45

    Why not just take the simplest option and specify the classpath in your <taskdef>?

    <taskdef resource="axis-tasks.properties">
        <classpath>
            <fileset file="/path/to/axis/jars"/>
        </classpath>
    </taskdef>
    

    Or create a second <classpath> entry that subsets library.dir?

    <path id="axis-tools-classpath">
        <fileset dir="/path/to/axis/home">
            <include name="*.jar"/>
        </fileset>
        <path refid="library.dir"/>
    </path>
    

    Messing around with ${ant.home}/lib is not such a good idea and can almost always be avoided.

    0 讨论(0)
提交回复
热议问题