如何将maven-ant-plugin设置的属性加入maven-jar-plugin的Jar清单?
解决方案:将Ant生成的自定义类路径属性写入Jar清单
我来帮你搞定这个问题!你的整体思路是对的——用maven-antrun-plugin拼接自定义类路径,再传递给maven-jar-plugin写入Manifest的Class-path条目,但现有配置有几个关键细节需要调整,才能让属性正确传递并生效:
问题分析
- Jar插件执行配置缺失核心部分:你的
maven-jar-plugin的execution没有指定goal(比如jar目标),而且绑定的phase是install,即便阶段顺序上在package之后,没有明确执行目标的话,这个配置根本不会生效。 - 语法错误:
manifestEntries里的<Class-path>${abc}</Class-path>>多了一个多余的>,这会直接导致Manifest生成失败。 - 阶段顺序优化:虽然默认阶段顺序是
package先于install,但把Jar插件绑定到package阶段,能更稳妥地保证在Ant插件生成属性之后执行打包操作。
修正后的完整配置
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>build-folder1-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <prefix>folder1</prefix> <outputDirectory>${project.build.directory}/folder1/</outputDirectory> <outputAbsoluteArtifactFilename>true</outputAbsoluteArtifactFilename> <excludeGroupIds>package.folder1</excludeGroupIds> </configuration> </execution> <execution> <id>build-folder2-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <prefix>folder2</prefix> <outputDirectory>${project.build.directory}/folder2/</outputDirectory> <outputAbsoluteArtifactFilename>true</outputAbsoluteArtifactFilename> <includeGroupIds>package.folder2</includeGroupIds> </configuration> </execution> </executions> </plugin> <plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <id>concat-build-classpath</id> <phase>package</phase> <goals> <goal>run</goal> </goals> <configuration> <exportAntProperties>true</exportAntProperties> <target> <path id="master-classpath"> <fileset dir="${project.build.directory}/folder1/"> <include name="*.jar" /> </fileset> <fileset dir="${project.build.directory}/folder2/"> <include name="*.jar" /> </fileset> </path> <property name="absolute-path" value="${toString:master-classpath}" /> <loadresource property="relative-path"> <propertyresource name="absolute-path" /> <filterchain> <tokenfilter> <filetokenizer/> <replaceregex pattern="(^.*?target\\|(?<=;).*?target\\)" replace="" flags="g" /> <replaceregex pattern="(\\)" replace="/" flags="g" /> <replaceregex pattern="(folder1)" replace="../folder1" flags="g" /> </tokenfilter> </filterchain> </loadresource> <property name="abc" value="${relative-path}"/> <!-- 可选:打印属性确认值是否符合预期 --> <echo message="Generated classpath: ${abc}" /> </target> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <executions> <execution> <id>include-path-in-jar</id> <phase>package</phase> <!-- 绑定到package阶段,确保在antrun之后执行 --> <goals> <goal>jar</goal> <!-- 必须指定goal,否则配置不会生效 --> </goals> <configuration> <archive> <manifestEntries> <Class-path>${abc}</Class-path> <!-- 去掉多余的> --> </manifestEntries> </archive> </configuration> </execution> </executions> </plugin> </plugins> </build>
关键调整说明
- Jar插件绑定正确目标和阶段:给
maven-jar-plugin的execution添加了<goal>jar</goal>,并将phase改为package,确保在Ant插件完成属性生成后立即执行Jar打包。 - 修复语法错误:移除了
Class-path标签末尾多余的>,避免Manifest文件格式错误。 - 添加调试步骤:在Ant的target里加入
<echo>命令,方便你快速确认生成的abc属性值是否符合预期。
验证方法
执行mvn clean package后,打开生成的Jar文件,查看META-INF/MANIFEST.MF文件,应该能看到Class-path条目包含你拼接的相对路径Jar列表。
内容的提问来源于stack exchange,提问作者Sandeep Sahu
相关产品推荐
相关产品推荐

