如何使用 maven-shade 和 felix 生成带有新清单的着色 jar

How to use maven-shade and felix to generate a shaded jar with a new manifest

我目前正在从事一个项目,该项目试图整合使用 shade 和 felix。目标是创建一个仅包含我们需要的代码的阴影 jar,然后使用 felix 创建我们需要的清单。我的pom构建部分如下:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>1.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <shadedArtifactAttached>false</shadedArtifactAttached>
                        <artifactSet>
                            <includes>
                                <include>${project.groupId}:*</include>
                            </includes>
                        </artifactSet>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <executions>
                <execution>
                    <id>bundle-manifest</id>
                    <phase>package</phase>
                    <goals>
                        <goal>manifest</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <manifestLocation>${project.build.directory}</manifestLocation>
                <niceManifest>true</niceManifest>
            </configuration>
        </plugin>

    </plugins>
</build>

现在我 运行 正在讨论的问题是我在其他地方看到的,但所有这些线程似乎都在此时死亡。所以阴影 jar 是正确创建的,然后 Felix 运行s 然后将 MANIFEST.MF 文件(据我所知这是正确的)放在 target/classes/META-INF/ 但它没有放在阴影罐子中也有同样的表现。罐子里的清单与 Felix 运行 之前存在的清单相同。

似乎我需要 Shade 到 运行,然后是 Felix,然后重新 运行 jar 创建。我错过了什么吗?

由于找不到更好的术语,我正在尝试找出如何使用新清单重新打包 JAR。

你的主要问题是manifest必须在jar文件中生成,你可以从shade插件生成你的Manifest文件(根据你的需要调整):

 <configuration>
        <transformers>
            <!-- This bit sets the main class for the executable jar as you otherwise -->
            <!-- would with the assembly plugin -->
            <transformer
                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <manifestEntries>
                    <Import-Package>org.apache.common</Import-Package>
                    <Export-Package>org.test</Export-Package>
                    <Main-Class>com.br.iacit.tutorialdoJar.ImageLab</Main-Class>
                    <Specification-Title>Java Advanced Imaging Image I/O Tools</Specification-Title>
                    <Specification-Version>1.1</Specification-Version>
                    <Specification-Vendor>Sun Microsystems, Inc.</Specification-Vendor>
                    <Implementation-Title>com.sun.media.imageio</Implementation-Title>
                    <Implementation-Version>1.1</Implementation-Version>
                    <Implementation-Vendor>Sun Microsystems, Inc.</Implementation-Vendor>
                </manifestEntries>
            </transformer>
            <!-- This bit merges the various GeoTools META-INF/services files -->
            <transformer
                implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
        </transformers>
    </configuration>

输出清单

Manifest-Version: 1.0
Toto: test
Export-Package: org.test
Archiver-Version: Plexus Archiver
Built-By: NG673AB
X-Compile-Target-JDK: 1.7
Import-Package: org.apache.common
X-Compile-Source-JDK: 1.7
Created-By: Apache Maven 3.3.3
Build-Jdk: 1.8.0_66
Main-Class: tt.tt.main

编辑:我设法让它编译正确,见下文:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>1.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                                <resource>META-INF/MANIFEST.MF</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
                                <resource>META-INF/MANIFEST.MF</resource>
                                <file>src/main/resources/MANIFEST.MF</file>
                            </transformer>
                        </transformers>
                        <shadedArtifactAttached>false</shadedArtifactAttached>
                        <artifactSet>
                            <includes>
                                <include>${project.groupId}:*</include>
                            </includes>
                        </artifactSet>

                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <executions>
                <execution>
                    <id>bundle-manifest</id>
                    <phase>package</phase>
                    <goals>
                        <goal>manifest</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <manifestLocation>src/main/resources/</manifestLocation>
                <niceManifest>true</niceManifest>
                <instructions>
                    <Export-Package>test</Export-Package>
                </instructions>
            </configuration>
        </plugin>

    </plugins>
</build>