如何让Maven install时将校验和文件同步到本地仓库?
问题:Maven校验和文件未被安装到本地仓库
我使用checksum-maven-plugin生成构件的校验和文件,插件配置如下:
<plugin> <groupId>net.nicoulaj.maven.plugins</groupId> <artifactId>checksum-maven-plugin</artifactId> <version>1.9</version> <executions> <execution> <goals> <goal>artifacts</goal> </goals> </execution> </executions> </plugin>
执行mvn install后,target目录中已生成以下文件,但所有*.md5/sha1校验和文件都未被同步到本地Maven仓库:
foo-1.0.0.jar foo-1.0.0.jar.md5 foo-1.0.0.jar.sha1 foo-1.0.0-javadoc.jar foo-1.0.0-javadoc.jar.md5 foo-1.0.0-javadoc.jar.sha1 foo-1.0.0-sources.jar foo-1.0.0-sources.jar.md5 foo-1.0.0-sources.jar.sha1
完整的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>com.foo</groupId> <artifactId>foo</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>22</maven.compiler.source> <maven.compiler.target>22</maven.compiler.target> <exec.mainClass>com.foo.foo.Foo</exec.mainClass> <javadoc.plugin.version>3.3.1</javadoc.plugin.version> <source.plugin.version>3.2.1</source.plugin.version> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>${javadoc.plugin.version}</version> <executions> <execution> <id>attach-javadocs</id> <goals> <goal>jar</goal> </goals> <configuration> <doclint>none</doclint> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>${source.plugin.version}</version> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>net.nicoulaj.maven.plugins</groupId> <artifactId>checksum-maven-plugin</artifactId> <version>1.9</version> <executions> <execution> <goals> <goal>artifacts</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
解决方案
修改checksum-maven-plugin的配置,添加<attach>true</attach>参数,让生成的校验和文件被标记为Maven构件的附件,这样install阶段会自动将它们同步到本地仓库:
<plugin> <groupId>net.nicoulaj.maven.plugins</groupId> <artifactId>checksum-maven-plugin</artifactId> <version>1.9</version> <executions> <execution> <goals> <goal>artifacts</goal> </goals> <configuration> <!-- 开启附件模式,让校验和文件随主构件一起安装 --> <attach>true</attach> <!-- 可选:指定要生成的校验和算法,默认已包含MD5、SHA-1 --> <!-- <algorithms> <algorithm>MD5</algorithm> <algorithm>SHA-1</algorithm> </algorithms> --> </configuration> </execution> </executions> </plugin>
修改后重新执行mvn clean install,校验和文件就会和主jar、javadoc jar、sources jar一同被安装到本地Maven仓库中。
内容的提问来源于stack exchange,提问作者SilverCube
相关产品推荐
相关产品推荐

