如何在不使用Gradle或Maven的情况下用Jenkins构建动态Web项目的WAR包
Got it, let's work through how to build your dynamic web project's WAR in Jenkins without relying on Maven or Gradle—while making sure all dependencies from your .classpath file and lib folder are included. Here are a few practical, tested approaches:
Ant is a tried-and-true build tool that works perfectly for this use case, and Jenkins has first-class support for it. You'll just need to write a build.xml config file to define your project structure, dependency locations, and build steps.
Step 1: Create an Ant build.xml
Here's a sample config that handles both lib folder JARs and dependencies from your .classpath file:
<project name="YourWebProject" default="war" basedir="."> <!-- Define project paths (adjust these to match your project structure) --> <property name="webroot" value="WebContent"/> <!-- Your web root (e.g., WebContent or src/main/webapp) --> <property name="build.dir" value="build"/> <property name="war.file" value="dist/your-project.war"/> <property name="lib.dir" value="lib"/> <property name="classpath.file" value=".classpath"/> <!-- Load Ant Contrib for advanced logic (needs ant-contrib.jar in Ant's lib) --> <taskdef resource="net/sf/antcontrib/antcontrib.properties"/> <xmlproperty file="${classpath.file}" collapseAttributes="true"/> <!-- Initialize build directories --> <target name="init"> <mkdir dir="${build.dir}"/> <mkdir dir="${build.dir}/classes"/> <mkdir dir="dist"/> </target> <!-- Compile Java source code --> <target name="compile" depends="init"> <javac srcdir="src" destdir="${build.dir}/classes"> <classpath> <!-- Include all JARs from lib folder --> <fileset dir="${lib.dir}" includes="**/*.jar"/> <!-- Include dependencies from .classpath file --> <path> <foreach list="${classpath.classpathentry.path}" delimiter=";" target="add-classpath-entry"/> </path> </classpath> </javac> </target> <!-- Helper task to process individual classpath entries --> <target name="add-classpath-entry"> <pathconvert property="entry.path"> <path location="${toString}"/> </pathconvert> <pathelement location="${entry.path}"/> </target> <!-- Package the WAR file --> <target name="war" depends="compile"> <war destfile="${war.file}" webxml="${webroot}/WEB-INF/web.xml"> <!-- Copy all web content --> <fileset dir="${webroot}"/> <!-- Add compiled class files --> <classes dir="${build.dir}/classes"/> <!-- Add lib folder JARs to WEB-INF/lib --> <lib dir="${lib.dir}"/> <!-- Add external dependencies from .classpath to WEB-INF/lib --> <foreach list="${classpath.classpathentry.path}" delimiter=";" target="add-external-lib"/> </war> </target> <!-- Helper task to copy external JARs to WAR's lib --> <target name="add-external-lib"> <pathconvert property="lib.path"> <path location="${toString}"/> </pathconvert> <if> <not><available file="${lib.path}" type="dir"/></not> <then> <copy file="${lib.path}" todir="${war.file}/WEB-INF/lib" flatten="true"/> </then> </if> </target> <!-- Clean build artifacts --> <target name="clean"> <delete dir="${build.dir}"/> <delete dir="dist"/> </target> </project>
Note: This uses Ant Contrib for looping logic—you'll need to place ant-contrib-x.x.jar in your Ant installation's lib folder, or add it via Jenkins' Ant tool configuration.
Step 2: Configure Jenkins to use Ant
- Install the Ant Plugin in Jenkins (if not already installed).
- Create a new Freestyle project, and configure your code repository (Git/SVN, etc.).
- Add a build step: Invoke Ant.
- In the "Targets" field, enter
war(orclean warif you want to clean before building). - Configure your Ant installation path (set this up first in Jenkins' Global Tool Configuration).
- Save and run the build—Jenkins will execute the Ant script and generate your WAR with all dependencies included.
If you prefer to avoid Ant, you can write a custom script to handle compilation and WAR packaging, then run it directly in Jenkins.
Sample Shell Script (Linux/macOS)
#!/bin/bash # Adjust these paths to match your project WEB_ROOT="WebContent" SRC_DIR="src" LIB_DIR="lib" CLASSPATH_FILE=".classpath" BUILD_DIR="build/classes" DIST_DIR="dist" WAR_FILE="${DIST_DIR}/your-project.war" # Create build directories mkdir -p "${BUILD_DIR}" mkdir -p "${DIST_DIR}" # Build classpath: include lib JARs + .classpath dependencies CLASSPATH="" # Add all JARs from lib folder for jar in "${LIB_DIR}"/*.jar; do CLASSPATH="${CLASSPATH}:${jar}" done # Parse .classpath for additional dependencies while read -r line; do if [[ $line == *path=* ]]; then PATH_ENTRY=$(echo "$line" | sed -n 's/.*path="\([^"]*\)".*/\1/p') if [[ -f "$PATH_ENTRY" ]]; then CLASSPATH="${CLASSPATH}:${PATH_ENTRY}" elif [[ -d "$PATH_ENTRY" ]]; then CLASSPATH="${CLASSPATH}:${PATH_ENTRY}" fi fi done < "${CLASSPATH_FILE}" # Compile Java code javac -d "${BUILD_DIR}" -cp "${CLASSPATH}" "${SRC_DIR}"/**/*.java # Package WAR: start with web content, add classes and dependencies cd "${WEB_ROOT}" jar -cvf "../${WAR_FILE}" . cd .. # Add compiled classes jar -uvf "${WAR_FILE}" -C "${BUILD_DIR}" . # Add lib folder JARs jar -uvf "${WAR_FILE}" -C "${LIB_DIR}" . # Add external dependencies from .classpath while read -r line; do if [[ $line == *path=* ]]; then PATH_ENTRY=$(echo "$line" | sed -n 's/.*path="\([^"]*\)".*/\1/p') if [[ -f "$PATH_ENTRY" && "$PATH_ENTRY" != "${LIB_DIR}"/* ]]; then jar -uvf "${WAR_FILE}" "${PATH_ENTRY}" fi fi done < "${CLASSPATH_FILE}" echo "WAR file generated at: ${WAR_FILE}"
Jenkins Configuration
- Create a Freestyle project and link your code repo.
- Add a build step: Execute shell (Linux/macOS) or Execute Windows batch command (convert the script to
.batformat for Windows). - Paste the script into the step, adjusting paths to match your project.
- Save and run the build.
Jenkins has a WAR Plugin that can package web projects directly, but it’s less flexible with .classpath dependencies. It works best for simple projects:
- Install the WAR Plugin.
- In your project config, add a build step: Build a WAR file.
- Specify your web root directory, compiled classes directory, and lib directory.
- Note: You’ll need to manually move any dependencies from
.classpath(not inlib) into the specified lib folder, as the plugin doesn’t parse.classpathautomatically.
Key Notes
- Ensure your
.classpathfile uses paths that are valid on the Jenkins server (adjust relative paths if needed). - For Windows, replace path separators (
/→\) and classpath separators (:→;) in scripts. - Make sure Jenkins has access to a JDK with
javacandjarcommands configured in its global tools.
内容的提问来源于stack exchange,提问作者Phani Sai Ram

