maven-shade-plugin 和自定义包装类型

Posted

技术标签:

【中文标题】maven-shade-plugin 和自定义包装类型【英文标题】:maven-shade-plugin and custom packaging type 【发布时间】:2015-09-24 13:19:31 【问题描述】:

我正在尝试使用内置的依赖项打包一个 OSGi 包。我正在使用 maven-shade-plugin 2.3 包含一些依赖项,但它在打包阶段失败并出现以下错误:

[ERROR] The project main artifact does not exist. This could have the following
[ERROR] reasons:
[ERROR] - You have invoked the goal directly from the command line. This is not
[ERROR]   supported. Please add the goal to the default lifecycle via an
[ERROR]   <execution> element in your POM and use "mvn package" to have it run.
[ERROR] - You have bound the goal to a lifecycle phase before "package". Please
[ERROR]   remove this binding from your POM such that the goal will be run in
[ERROR]   the proper phase.
[ERROR] - You removed the configuration of the maven-jar-plugin that produces the main artifact.

这是我项目的 pom 的样子:

<?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>ru.multicabinet.plugin</groupId>
<artifactId>testArtifact</artifactId>
<version>1.0-SNAPSHOT</version>

<packaging>bundle</packaging>

<dependencies>
    <dependency>
        <groupId>org.osgi</groupId>
        <artifactId>org.osgi.core</artifactId>
        <version>4.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.osgi</groupId>
        <artifactId>org.osgi.compendium</artifactId>
        <version>4.3.1</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.apache.felix.scr.annotations</artifactId>
        <version>1.9.6</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.7</version>
    </dependency>
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1.1</version>
    </dependency>

</dependencies>

<build>
    <plugins>

        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-scr-plugin</artifactId>
            <version>1.15.0</version>
            <executions>
                <execution>
                    <id>generate-scr-scrdescriptor</id>
                    <goals>
                        <goal>scr</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>

            <configuration>
                <remoteOBR>obr-repo</remoteOBR>
                <instructions>
                    <Bundle-Description>Test Plugin</Bundle-Description>
                    <Import-Package>
                        org.osgi.framework,
                        javax.net.ssl,
                        javax.mail,
                        javax.mail.internet,
                        javax.activation
                    </Import-Package>
                    <Bundle-SymbolicName>ru.multicabinet.plugin.license.testArtifact</Bundle-SymbolicName>
                </instructions>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <artifactSet>
                            <includes>
                                <include>commons-codec:commons-codec</include>
                            </includes>
                        </artifactSet>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>


</project>

我怀疑我收到此错误是因为我使用的是“捆绑”打包类型,因此 shade 插件无法识别实际的 jar 文件,因此抱怨主工件。如何解决?

谢谢

【问题讨论】:

如果你想给一个包加阴影,你需要创建一个单独的模块,你可以在其中创建它的阴影变体,但不像你在打包是捆绑的模块中已经意识到的那样。 @khmarbaise 但这个单独的包将丢失其中的所有 osgi 元数据,因为它不是一个捆绑包,所以它对我没有用处。我正在寻找一种方法,使其既是有效的 OSGi 包,又包含我需要的所有依赖项。 【参考方案1】:

我设法通过将项目的打包类型更改回“jar”并配置 maven bundle 插件以将 osgi 元数据注入最终 jar 工件而不必使用 maven bundle plugin documentation 中描述的“bundle”打包来解决这个问题.

用于将 OSGi 元数据注入 jar 包的示例 maven bundle 插件配置:

<plugin>
  <artifactId>maven-jar-plugin</artifactId>
  <configuration>
    <archive>  
     <manifestFile>$project.build.outputDirectory/META-INF/MANIFEST.MF</manifestFile>
    </archive> 
  </configuration>
</plugin>  
<plugin>   
   <groupId>org.apache.felix</groupId>
   <artifactId>maven-bundle-plugin</artifactId>
   <executions>
     <execution>
     <id>bundle-manifest</id>
     <phase>process-classes</phase>
     <goals>    
       <goal>manifest</goal>
     </goals>   
     </execution>
   </executions>
</plugin>

【讨论】:

以上是关于maven-shade-plugin 和自定义包装类型的主要内容,如果未能解决你的问题,请参考以下文章

如何使用静态变量和自定义 getter 和 setter 在 SWIG 中扩展结构?

用它包装的函数保存一个 sklearn `FunctionTransformer`

maven-shade-plugin插件

Netbeans:项目主神器通过maven-shade-plugin处理

如何判断哪个签名的 jar 导致 maven-shade-plugin 失败?

maven-shade-plugin 的用途是啥,为啥要重新定位 Java 包?