Maven执行clean install时sources.jar未安装到.m2仓库求助
问题说明
执行mvn clean install可生成项目普通jar文件,但本地Maven仓库(.m2目录)中没有生成sources.jar;单独执行mvn source:jar能在target目录生成该文件,说明maven-source-plugin在clean install的构建流程中未被触发执行。
以下是原pom.xml配置:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>pick.box</groupId> <artifactId>pick-box</artifactId> <version>1.0.8</version> <name>pick-box</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <java.version>17</java.version> </properties> <distributionManagement> <repository> <id>github</id> <name>GitHub wbf22 Apache Maven Packages</name> <url>https://maven.pkg.github.com/wbf22/PickBox</url> </repository> </distributionManagement> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.10.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.16.1</version> </dependency> </dependencies> <build> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <!-- run mvn source:jar to generate these in /target --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.2.1</version> <executions> <execution> <id>attach-sources</id> <phase>package</phase> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> </plugins> </pluginManagement> </build> </project>
问题根源
你把maven-source-plugin配置放在了<pluginManagement>标签下,这个标签仅用于统一管理插件版本,不会让插件实际参与到Maven的构建生命周期流程中。只有当插件被直接配置在<build><plugins>标签下时,才会绑定到指定的生命周期阶段自动执行。
修复方案
将maven-source-plugin从<pluginManagement>中移出,放到<build>下的<plugins>标签内。修改后的关键配置如下:
<build> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <!-- 此处可保留其他插件的版本管理配置 --> </plugins> </pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.2.1</version> <executions> <execution> <id>attach-sources</id> <phase>package</phase> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
验证方法
修改完成后重新执行mvn clean install,此时maven-source-plugin会绑定到package阶段执行,生成的sources.jar会被自动安装到本地.m2仓库中。
内容的提问来源于stack exchange,提问作者brando f
相关产品推荐
相关产品推荐

