如何在Maven中引用依赖版本并将指定依赖版本写入Manifest文件?
如何在Maven Shade插件中写入指定依赖的版本到Manifest文件
嘿,我懂你要做的事——不想把当前项目的版本塞进Manifest里,而是要把registry:desktop.common这个依赖的版本放进去对吧?这里有两种实用的方案,你可以根据自己的情况选:
方案1:直接硬编码版本(简单直接)
如果这个依赖的版本不怎么变动,你可以直接把Manifest里的版本值写成固定的1.0.0,修改后的shade插件配置如下:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <executions> <execution> <goals> <goal>shade</goal> </goals> <configuration> <finalName>ispf-linux</finalName> <shadedArtifactAttached>true</shadedArtifactAttached> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>desktop.linux.main.Main</mainClass> <manifestEntries> <Implementation-Version>1.0.0</Implementation-Version> </manifestEntries> </transformer> </transformers> </configuration> </execution> </executions> </plugin>
这种方式的缺点是如果依赖版本更新了,你得手动改这个值,适合版本稳定的场景。
方案2:用Maven属性动态管理(更灵活)
如果希望版本能统一管理,避免重复修改,你可以先在pom的<properties>里定义这个依赖的版本,然后在依赖配置和Manifest里都引用这个属性:
首先,在pom的<properties>块中添加:
<properties> <desktop.common.version>1.0.0</desktop.common.version> </properties>
然后修改你的依赖配置,用这个属性:
<dependency> <groupId>registry</groupId> <artifactId>desktop.common</artifactId> <version>${desktop.common.version}</version> </dependency>
最后修改shade插件的Manifest配置,引用同一个属性:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <executions> <execution> <goals> <goal>shade</goal> </goals> <configuration> <finalName>ispf-linux</finalName> <shadedArtifactAttached>true</shadedArtifactAttached> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>desktop.linux.main.Main</mainClass> <manifestEntries> <Implementation-Version>${desktop.common.version}</Implementation-Version> </manifestEntries> </transformer> </transformers> </configuration> </execution> </executions> </plugin>
这样以后只要修改<properties>里的版本号,依赖和Manifest里的版本就会同步更新,非常适合需要频繁更新依赖版本的场景。
内容的提问来源于stack exchange,提问作者MarekChr
相关产品推荐
相关产品推荐

