Maven shade插件没有将依赖类文件放入jar

时间:2015-10-31 03:43:27

标签: java maven m2eclipse maven-shade-plugin

My Maven项目使用外部库作为依赖项com.sk89q.intake:intake,我正试图通过maven-shade-plugin打包到我的jar中。构建项目时,生成的jar不包含com.sk89q.intake:intake的任何类文件。在构建过程中,我收到此消息,但构建继续并成功:

[INFO] --- maven-shade-plugin:2.4.2:shade (default) @ EventManagerPlugin
[INFO] No artifact matching filter com.sk89q.intake:intake

为什么会这样?我能够在项目中下载,访问和使用依赖项,因此对工件的命名应该没有任何问题。

的pom.xml

<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>deletethis.eventmanager</groupId>
    <artifactId>EventManagerPlugin</artifactId>
    <version>1.0.0-beta1</version>
    <repositories>
        <repository>
            <id>spigot-repo</id>
            <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
        </repository>
        <repository>
            <id>maven.sk89q.com</id>
            <url>http://maven.sk89q.com/repo/</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
           <groupId>org.spigotmc</groupId>
           <artifactId>spigot-api</artifactId>
           <version>1.8.8-R0.1-SNAPSHOT</version>
           <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.bukkit</groupId>
            <artifactId>bukkit</artifactId>
            <version>1.8.8-R0.1-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.sk89q.intake</groupId>
            <artifactId>intake</artifactId>
            <version>4.2-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Built-By>deletethis</Built-By>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.2</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>com.sk89q.intake:intake</artifact>
                                    <includes>
                                        <include>com/sk89q/intake/**</include>
                                    </includes>
                                </filter>
                            </filters>
                            <relocations>
                                <relocation>
                                    <pattern>com.sk89q.intake</pattern>
                                    <shadedPattern>deletethis.eventmanager.lib.com.sk89q.intake</shadedPattern>
                                </relocation>
                            </relocations>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

如您所见,我包含com.sk89q.intake:intake神器。我查看了maven-shade-plugin文档,看不出我做错了什么。命名与我在网上找到的所有内容一致;也就是groupId:artifactId

我还尝试在没有<relocation>类重定位标记的情况下构建,以查看它们是否在干扰。

知道我正在使用M2Eclipse并建立clean install目标可能很有用。

1 个答案:

答案 0 :(得分:1)

问题在于您宣布与com.sk89q.intake:intake范围的provided依赖关系。

预计容器会在运行时提供依赖关系,因此maven-shade-plugin不会将其添加到您的着色jar中。因此,您需要从依赖项声明中删除provided范围:

<dependency>
    <groupId>com.sk89q.intake</groupId>
    <artifactId>intake</artifactId>
    <version>4.2-SNAPSHOT</version>
</dependency>

此更改后的相关构建日志:

[INFO] --- maven-shade-plugin:2.4.2:shade (default) @ test ---
[INFO] Including com.sk89q.intake:intake:jar:4.2-SNAPSHOT in the shaded jar.
[INFO] Including com.google.guava:guava:jar:18.0 in the shaded jar.
[INFO] Including com.google.code.findbugs:jsr305:jar:3.0.0 in the shaded jar.