Ant Jar构建Bat脚本执行失败,请求脚本修正协助
Let's break down why your script isn't working and fix it step by step, like we'd troubleshoot together on a dev team:
1. First: Diagnose the Ant Build Failure
The && operator only runs the COPY command if the preceding Ant command succeeds (returns exit code 0). Since your JAR isn't being generated, the Ant step is almost certainly failing. Here's how to get to the root of it:
- Open a Command Prompt, navigate to the folder where your batch script lives, and run the Ant command directly:
C:\apache-ant-1.9.15\bin\ant -f build.xml - Look for error messages in the output—common issues include:
build.xmlnot found (it needs to be in the same folder as your batch script, or you need to specify its full path)- Missing dependencies (like JDK tools not being in your system PATH)
- Your
build.xmldoesn't have a definedjartarget, or the default target skips building the JAR
2. Fix the Ant Command
If build.xml isn't in the same directory as your batch script, use its full absolute path (wrap it in quotes if there are spaces in the path):
C:\apache-ant-1.9.15\bin\ant -f "C:\Full\Path\To\Your\Project\build.xml" jar
Note the jar at the end—this explicitly tells Ant to run the JAR-building target. If your build.xml uses a different target name (like package), replace it with that.
3. Fix the COPY Command
Your current COPY line uses placeholder paths, so you need to:
- Point directly to the JAR file Ant generates (not just a source folder—Ant typically outputs JARs to a
build/libsordistsubfolder in your project) - Wrap all paths in quotes to handle spaces in folder names
- Add a check to create the destination folder if it doesn't exist
4. Revised, Robust Batch Script
Here's a polished version of your script with error checking and clear configuration:
@echo off setlocal enabledelayedexpansion :: Configure your paths here (update these to match your project!) set "ANT_EXE=C:\apache-ant-1.9.15\bin\ant.exe" set "BUILD_XML=C:\Path\To\Your\Project\build.xml" set "BUILT_JAR=C:\Path\To\Your\Project\build\libs\your-app-name.jar" set "DEST_FOLDER=C:\Your\Target\Destination\Folder" :: Run the Ant build echo Starting Ant build... "%ANT_EXE%" -f "%BUILD_XML%" jar :: Check if Ant succeeded if !errorlevel! equ 0 ( echo Ant build completed successfully. :: Create destination folder if it doesn't exist if not exist "%DEST_FOLDER%" mkdir "%DEST_FOLDER%" :: Copy the JAR file echo Copying JAR to destination... copy "%BUILT_JAR%" "%DEST_FOLDER%" /y echo Done! ) else ( echo ERROR: Ant build failed. Check the output above for details. pause ) endlocal
5. Double-Check Your build.xml
Make sure your build.xml includes a valid JAR target. Here's a minimal working example:
<project name="YourApp" default="jar" basedir="."> <property name="src.dir" value="src"/> <property name="build.dir" value="build"/> <property name="libs.dir" value="${build.dir}/libs"/> <property name="main.class" value="com.yourcompany.YourMainClass"/> <target name="compile"> <mkdir dir="${build.dir}/classes"/> <javac srcdir="${src.dir}" destdir="${build.dir}/classes"/> </target> <target name="jar" depends="compile"> <mkdir dir="${libs.dir}"/> <jar destfile="${libs.dir}/your-app-name.jar" basedir="${build.dir}/classes"> <manifest> <attribute name="Main-Class" value="${main.class}"/> </manifest> </jar> </target> </project>
内容的提问来源于stack exchange,提问作者KARSHIL SHETH

