Maven Shade插件创建没有依赖关系的Jar并从另一个位置加载依赖关系jar

时间:2015-11-20 04:51:00

标签: maven maven-shade-plugin

我想使用没有第三方依赖关系的maven-shade-plugin创建一个jar文件。但是,应将第三方依赖项jar复制到某个文件夹(比如libs)。 所以,当我分发我的应用程序时,我将main.jar和libs文件夹一起分发,所以当我启动创建的主jar时,它应该从libs文件夹加载依赖项。

是否可以在maven-shade-plugin中执行此操作?然后如何配置它?

2 个答案:

答案 0 :(得分:0)

这不能实现,因为maven shade插件将所有依赖jar文件内容(类文件)提取到主jar文件中。但是,这可以使用maven-jar-pluginmaven-dependency-plugin来实现。这是我的构建设置:

<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <classpathPrefix>lib/</classpathPrefix>
                    <mainClass>fully.qualified.MainClass</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
            <execution>
                <phase>install</phase>
                <goals>
                    <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                    <outputDirectory>${project.build.directory}/lib</outputDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

答案 1 :(得分:0)

当然,我还通过maven shade插件直接构建了一个大罐子。

以下是我在一个jar中构建spring应用程序的用法,包括所有spring framework依赖:

<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>META-INF/spring.handlers</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.schemas</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.tooling</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <manifestEntries>
                            <Main-Class>${project.build.mainClass}</Main-Class>
                            <Build-Number>1</Build-Number>
                            <Specification-Title>${project.artifactId}</Specification-Title>
                            <Specification-Version>${project.version}</Specification-Version>
                            <Implementation-Title>${project.artifactId}</Implementation-Title>
                            <Implementation-Version>${project.version}</Implementation-Version>
                            <Implementation-Vendor-Id>${project.groupId}</Implementation-Vendor-Id>
                        </manifestEntries>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

除了与Maven Assembly Plugin的比较之外,如HP中所述,Maven Assembly Plugin仅提供一些基本支持,但是Maven Shade插件可以提供更多控制。