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

Jenkins+Maven打包空JAR:如何指定待打包目录?

Jenkins构建AngularJS项目生成空JAR的解决方法

问题背景

项目以AngularJS脚本为主,需先执行npm install安装依赖、npm build构建生成./dist/apps/arena-web-ruf目录,再通过Jenkins的Maven任务打包该目录内容。但当前生成的JAR为空,日志提示JAR将为空 - 没有标记任何内容要包含!。

核心原因

默认Maven打包只会包含src/main/java、src/main/resources等标准目录下的内容,而项目的构建产物在dist/apps/arena-web-ruf,未被配置为Maven的打包目标目录,导致无内容纳入JAR。

解决步骤

1. 配置pom.xml指定打包目录

修改项目的pom.xml,添加maven-jar-plugin插件,明确指定要打包的目录和内容:

<modelVersion>4.0.0</modelVersion>
<groupId>iprods</groupId>
<artifactId>arena-web-ruf</artifactId>
<version>2022.3-SNAPSHOT</version>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.3.0</version>
            <configuration>
                <!-- 指定打包的源目录为dist下的目标子目录 -->
                <directory>${project.basedir}/dist/apps/arena-web-ruf</directory>
                <!-- 包含该目录下所有文件 -->
                <includes>
                    <include>**/*</include>
                </includes>
            </configuration>
        </plugin>
    </plugins>
</build>

2. 调整Jenkins构建流程,确保先执行npm构建

当前Jenkins任务仅执行了Maven命令,需在Maven构建前添加npm的依赖安装和构建步骤,保证dist目录生成后再打包:

steps {
    // 执行npm依赖安装与项目构建
    sh 'npm install'
    sh 'npm run build' // 若项目的build命令为其他名称,替换为实际命令
    
    rtServer (
        id: 'Artifactory',
        url: 'https://artifactory.domain.dev/artifactory',
        credentialsId: 'ac0c0e829676'
    )

    rtMavenDeployer (
        id: "MAVEN_DEPLOYER",
        serverId: "Artifactory",
        releaseRepo: "my-generic-release-local",
        snapshotRepo: "my-generic-snapshot-local"
    )

    rtMavenResolver (
        id: "MAVEN_RESOLVER",
        serverId: "Artifactory",
        releaseRepo: "apache-maven-remote",
        snapshotRepo: "apache-maven-remote"
    )
    
    rtMavenRun (
        tool: "maven-3.2.5",
        pom: 'pom.xml',
        goals: 'clean install',
        deployerId: "MAVEN_DEPLOYER",
        resolverId: "MAVEN_RESOLVER"
    )
}

3. 验证打包结果

重新触发Jenkins构建,若日志中不再出现JAR将为空 - 没有标记任何内容要包含!的警告,说明配置生效。可通过以下命令检查JAR内的内容:

jar tf /data/jenkins/maven/target/arena-web-ruf-2022.3-SNAPSHOT.jar

执行后会列出JAR中的所有文件,确认是否包含dist/apps/arena-web-ruf下的静态资源与脚本文件。

注意事项

  • 若npm build生成的目录路径发生变化,需同步更新pom.xml中<directory>的路径。
  • 确保Jenkins运行环境已安装对应版本的Node.js和npm,否则npm命令无法正常执行。

内容的提问来源于stack exchange,提问作者vel

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 22:45:39