![](/img/trans.png)
[英]Include Maven profile name in assembly-plugin built (with dependencies) jar
[英]files from different maven-modules with the same name can not co-exist in a jar-file created with the maven assembly-plugin
如果有兩個文件具有不同的內容但在兩個不同的maven-modules中具有相同的名稱,那么它們都與maven assembly-plugin放在一個jar文件中,只有一個文件最終成為.jar文件的一部分。
問題 :有沒有辦法確保在構建jar文件時將文件的內容組合到一個文件中?
我顯然不想手動將信息放在一起,因為這是我試圖通過在不同模塊中拆分項目來避免的。
編輯:我有一個自定義的Assembly-Descriptor,我想保留,即使我開始使用另一個插件。 這個描述符基本上排除了所有語言,但英語排除了資源和錯誤文本。
<id>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
<unpackOptions>
<excludes>
<exclude>**/*Resources_*</exclude>
<exclude>**/*ErrorsText_*</exclude>
</excludes>
</unpackOptions>
</dependencySet>
</dependencySets>
由maven-assembly-plugin
文檔指定:
如果您的項目想要將工件打包在超級jar中,則程序集插件僅提供基本支持。 要獲得更多控制,請使用Maven Shade插件 。
使用maven-shade-plugin
你可以有一個胖jar(比如使用程序集插件)並解決使用Resources變換器合並文件的類似問題。 在您的情況下, AppendingTransformer
將合並具有相同名稱但具有不同內容的文件。
某些jar包含具有相同文件名的其他資源(例如屬性文件)。 為避免覆蓋,您可以選擇通過將其內容附加到一個文件來合並它們。
一個簡單的配置看起來像:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>path/to/file/file-name-here</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
更新
您不需要為shade插件提供外部程序集描述符,您可以直接將您的需求配置為插件配置。
在您的情況下,要從已組裝的jar中排除資源,您可以使用陰影過濾器 。
一個簡單的配置(與上面的一個合並)看起來像:
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>**/*Resources_*</exclude>
<exclude>**/*ErrorsText_*</exclude>
</excludes>
</filter>
</filters>
</configuration>
還遇到了這個問題,我需要從依賴模塊中篩選出同名的資源文件,解決方法如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>artifact1</artifact>
<excludes>
<exclude>application.yml</exclude>
<exclude>logging.yml</exclude>
</excludes>
</filter>
<filter>
<artifact>artifact2</artifact>
<excludes>
<exclude>application.yml</exclude>
<exclude>logging.yml</exclude>
</excludes>
</filter>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
請注意以下幾行是為了避免可能的異常:
線程“main”中的異常java.lang.SecurityException:Manifest主要屬性的簽名文件摘要無效
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
如果您需要更多詳細信息,請參閱為Uber JAR選擇內容
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.