You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在不使用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:

方案1:使用Ant(最推荐的无Maven/Gradle方案)

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

  1. Install the Ant Plugin in Jenkins (if not already installed).
  2. Create a new Freestyle project, and configure your code repository (Git/SVN, etc.).
  3. Add a build step: Invoke Ant.
  4. In the "Targets" field, enter war (or clean war if you want to clean before building).
  5. Configure your Ant installation path (set this up first in Jenkins' Global Tool Configuration).
  6. Save and run the build—Jenkins will execute the Ant script and generate your WAR with all dependencies included.
方案2:Use Jenkins + Custom Shell/PowerShell Script

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

  1. Create a Freestyle project and link your code repo.
  2. Add a build step: Execute shell (Linux/macOS) or Execute Windows batch command (convert the script to .bat format for Windows).
  3. Paste the script into the step, adjusting paths to match your project.
  4. Save and run the build.
方案3:Jenkins WAR Plugin (Simpler but Less Flexible)

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:

  1. Install the WAR Plugin.
  2. In your project config, add a build step: Build a WAR file.
  3. Specify your web root directory, compiled classes directory, and lib directory.
  4. Note: You’ll need to manually move any dependencies from .classpath (not in lib) into the specified lib folder, as the plugin doesn’t parse .classpath automatically.

Key Notes

  • Ensure your .classpath file 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 javac and jar commands configured in its global tools.

内容的提问来源于stack exchange,提问作者Phani Sai Ram

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:12:29