简体   繁体   English

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

[英]Maven Shade Plugin Create Jar Without Dependencies and load dependency jars from another location

I want to create a jar file using maven-shade-plugin without 3rd party dependencies. 我想使用没有第三方依赖性的maven-shade-plugin创建一个jar文件。 However, 3rd party dependency jars should be copied to some folder (say libs). 但是,应将第三方依赖项jar复制到某个文件夹(例如libs)。 So, when I distribute my application, I distribute main.jar and libs folder together, so when I launch the created main jar, it should load dependencies from libs folder. 因此,当我分发应用程序时,会一起分发main.jar和libs文件夹,因此,当我启动创建的主jar时,它应该从libs文件夹加载依赖项。

Is it possible to do this in maven-shade-plugin? 是否可以在maven-shade-plugin中执行此操作? then how to configure it? 那该如何配置呢?

This cannot be achieved as maven shade plugin extracts all dependency jar file content(class files) in to main jar file. 这是无法实现的,因为Maven Shade插件将所有依赖项jar文件的内容(类文件)提取到主jar文件中。 However, this can be achieved using maven-jar-plugin and maven-dependency-plugin . 但是,可以使用maven-jar-pluginmaven-dependency-plugin This is my build settings: 这是我的构建设置:

<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>

Of course, I have also built a big jar directly through maven shade plugin. 当然,我也直接通过maven shade plugin构建了一个大jar。

Here is my usage to build a spring application in one jar including all spring framework dependency: 这是我在一个jar中构建spring应用程序的用法,其中包括所有spring框架依赖项:

<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>

In addition about the comparison with Maven Assembly Plugin , as mentioned in the HP, Maven Assembly Plugin only provide some basic support, however Maven Shade Plugin could provide more control. 除了与HP中提到的与Maven Assembly Plugin的比较之外,Maven Assembly Plugin仅提供一些基本支持,但是Maven Shade Plugin可以提供更多控制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Maven Shade 插件使用 Dependent jars 创建依赖文件夹 - Create dependency folder with Dependent jars with Maven Shade plugin Maven Shade插件包括所有依赖罐 - Maven shade plugin to include all dependency jars 将另一个依赖项(休眠)的所有依赖项隐藏到 jar - Shade all dependencies of another dependency (hibernate) into jar 从 Maven 项目构建可执行 Jar 项目,并使用 Shade 或 Maven 程序集插件 - Build an executable Jar from a Maven Project with dependencies with Shade or Maven Assembly Plugin Maven:从阴影插件中排除依赖项 - Maven: exclude dependency from shade plugin 如何避免 maven 阴影插件包含来自“test-jar”类型的传递依赖? - How to avoid maven shade plugin from including transitive dependencies from 'test-jar' types? maven-shade-plugin:排除依赖项及其所有传递依赖项 - maven-shade-plugin: exclude a dependency and all its transitive dependencies Maven Shade插件未将依赖项类文件放入jar - Maven shade plugin isn't placing dependency class files into jar 使用maven-shade-plugin,但依赖类不在最后的jar中 - use maven-shade-plugin, but dependency classes are not in the final jar maven-shade-plugin 不包括声明为依赖项的 jar - maven-shade-plugin is excluding jar that is declared as a dependency
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM