maven 打jar包依赖问题

共有3个插件maven-shade-plugin,maven-jar-plugin+dependency,maven-assembly-plugin

 https://blog.csdn.net/fuck487/article/details/77503925

 

1.shade直接打包,包含所有依赖项

https://blog.csdn.net/bljbljbljbljblj/article/details/52484595

<plugin>

    <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>1.7.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <filters>
                            <filter>
                                <artifact>:</artifact> -》<artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                        <transformers>
                            <transformer
                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.xxx.xxx</mainClass>
                            </transformer>
                            <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>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>

  

1+ spring boot也是内置依赖 

2.jar不含依赖项

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.newlandframework.rpc.boot.RpcServerStarter</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<phase>prepare-package</phase>可将jar依赖包一起打入,不过会有问题,它会把依赖jar包打进去,在里面开一个lib目录塞进去,而不是像assembly和shade一样以class的形式打进jar包 2019.12.5


3.assembly
内置依赖:
https://blog.csdn.net/bljbljbljbljblj/article/details/52484595
<plugin>

<artifactId>maven-assembly-plugin</artifactId>
                <version>2.2-beta-5</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.xxx.xxx</mainClass>   包含main方法的类
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef> 将依赖包一起打包
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>                        执行器 mvn assembly:assembly
                        <id>make-assembly</id>         是个名字,可以任意
                        <phase>package</phase>         绑定到package生命周期阶段上
                        <goals>
                            <goal>single</goal>        只运行一次
                        </goals>
                    </execution>
                </executions>
    </plugin>

  

至于我为什么最终会选择maven-shade-plugin这种方式,是因为之前在使用maven-assembly-plugin的时候,在本地运行没有任何问题,而当我们放到linux上运行jar包的时候,就会出现错误Unable to locate Spring NamespaceHandler for XML schema namespace[http://www.springframework.org/schema/context]等等之类的错误,错误原因大概是因为由于项目中用到了spring框架,而当我们在项目中使用spring的多个内容的时候,包含了相同名字的文件,通过看网上的解决办法,发现maven-shade-plugin这个插件的打jar包方式可以解决该问题,上述pom.xml上的<transformer>标签中的内容就是解决该问题的!


参考:
http://blog.51cto.com/alex233/1885759
http://www.cnblogs.com/dzblog/p/6913809.html
https://blog.csdn.net/bljbljbljbljblj/article/details/52484595

posted on 2018-07-23 13:55  silyvin  阅读(719)  评论(0编辑  收藏  举报