3

我对如何创建 ant 文件非常陌生。其他类似的问题对我没有多大帮助,所以这就是我所拥有的。

我有这个蚂蚁文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <project name="UMS-PKS 33 Deploy Scripts for Snapshot" default="main"  basedir="..">

        <property name="src.dir"     value="setup/CopyScriptsToDatabase"/>
        <property name="ant.dir"     value="ant/src"/>
        <property name="build.dir"   value="tmp/buildMySql"/>
        <property name="classes.dir" value="${build.dir}/classes"/>
        <property name="jar.dir"     value="${build.dir}/jar"/>
        <property name="main-class"  value="CopyScriptsToDatabase.test"/>
        <property file="ant/properties/compile.properties" />
        <property file="ant/properties/profile.properties" />
        <property file="ant/properties/${deploy.properties}" />

        <path id="lib.classpath">
            <fileset dir="${ant.dir}">
                <include name="mysql-connector-java-commercial-5.1.21-bin.jar" />
            </fileset>
        </path>

        <target name="clean">

        </target>

        <target name="compile">
            <mkdir dir="${classes.dir}"/>
            <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
        </target>

        <target name="jar" depends="compile">
            <mkdir dir="${jar.dir}"/>
            <jar destfile="${jar.dir}/CopyScriptsToDatabase.jar" basedir="${classes.dir}">
                <manifest>
                    <attribute name="Main-Class" value="${main-class}" />
                </manifest>
            </jar>
        </target>

        <target name="run" depends="jar">
            <java jar="${jar.dir}/CopyScriptsToDatabase.jar" fork="true" classpath="lib.classpath">
                <arg value="${dwh.serverName}"/>
            </java>
        </target>

        <target name="clean-build" depends="clean,jar"/>
        <target name="main" depends="clean,run"/>
    </project>

尝试从 ant 运行 jar 文件时出现以下错误:

        Buildfile: D:\Projects\UMS-PKS\ant\33_DeployScritps.xml
    clean:
    compile:
        [javac] D:\Projects\UMS-PKS\ant\33_DeployScritps.xml:26: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
        [javac] Compiling 1 source file to D:\Projects\UMS-PKS\tmp\buildMySql\classes
    jar:
          [jar] Building jar: D:\Projects\UMS-PKS\tmp\buildMySql\jar\CopyScriptsToDatabase.jar
    run:
         [java] java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
         [java]     at java.net.URLClassLoader$1.run(Unknown Source)
         [java]     at java.security.AccessController.doPrivileged(Native Method)
         [java]     at java.net.URLClassLoader.findClass(Unknown Source)
         [java]     at java.lang.ClassLoader.loadClass(Unknown Source)
         [java]     at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
         [java]     at java.lang.ClassLoader.loadClass(Unknown Source)
         [java]     at java.lang.Class.forName0(Native Method)
         [java]     at java.lang.Class.forName(Unknown Source)
         [java]     at CopyScriptsToDatabase.test.main(Unknown Source)
         [java] localhost
         [java] MySQL Connect Example.
    main:
    BUILD SUCCESSFUL
    Total time: 1 second

我需要添加什么才能使其正常工作?

谢谢,萨斯加布里埃尔

4

2 回答 2

0
  1. 根据您的 ant 文件所在的目录,您在目录 ../ant/src 中有 mysql-connector-java-commercial-5.1.21-bin.jar 文件吗?
  2. 如果您在 ../ant/src/ 中有文件,请使用 zip 工具打开它并确保 Driver.class 在 com/mysql/jdbc/ 中。而且我认为最好将其命名为 lib 放置第 3 方 lib 文件的位置。
于 2013-01-15T06:44:03.060 回答
0

我最近遇到了同样的问题。经过一番调查,我找到了解决方案。我会为下一个正在寻找此解决方案的人写下来。

替换此部分:

<target name="run" depends="jar">
    <java jar="${jar.dir}/CopyScriptsToDatabase.jar" fork="true classpath="lib.classpath">
        <arg value="${dwh.serverName}"/>
    </java>
</target>

对此:

<path id="classref1" location="${jar.dir}/CopyScriptsToDatabase.jar"/>
<path id="classref2" location="path/to/jars/mysql-connector-java-commercial-5.1.21-bin.jar"/>
<target name="run" depends="jar">
    <java fork="true classname="lib.classpath">
        <classpath>
            <path refid="classref1"/>
            <path refid="classref2"/>
        </classpath>
        <arg value="${dwh.serverName}"/>
    </java>
</target>

这就像运行:

java -cp "mysql-connector-java-commercial-5.1.21-bin.jar:CopyScriptsToDatabase.jar" {lib.classpath} {arg_serverName}

于 2018-12-23T14:40:23.477 回答