<plugins>
            <!-- 复制依赖的插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>true</overWriteSnapshots>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.weibosdk.listener.boot.ListenerBootstrap</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <createDependencyReducedPom>false</createDependencyReducedPom>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <!-- Main函数所在类 -->
                                    <mainClass>com.weibosdk.listener.boot.ListenerBootstrap</mainClass>
                                </transformer>

                                <!-- Spring相关的文件 -->
                                <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>

        </plugins>

这样子,运行以下命令即可生成两种方式的jar包了。

mvn clean package -Dmaven.test.skip=true
╭─sky@sky-linux ~/git/weibosdk/wbsdk-listener/target  ‹pre-release20160525*› 
╰─➤  ls -alh
total 11M
drwxr-xr-x 7 sky sky 4.0K  6月  1 12:27 .
drwxr-xr-x 4 sky sky 4.0K  6月  1 12:27 ..
drwxr-xr-x 7 sky sky 4.0K  6月  1 12:27 classes
drwxr-xr-x 3 sky sky 4.0K  6月  1 12:27 generated-sources
drwxr-xr-x 2 sky sky 4.0K  6月  1 12:27 lib
drwxr-xr-x 2 sky sky 4.0K  6月  1 12:27 maven-archiver
drwxr-xr-x 3 sky sky 4.0K  6月  1 12:27 maven-status
-rw-r--r-- 1 sky sky  58K  6月  1 12:27 original-wbsdk-listener-1.0.0.jar
-rw-r--r-- 1 sky sky  11M  6月  1 12:27 wbsdk-listener-1.0.0.jar

其中wbsdk-listener-1.0.0.jar是一个可独立运行的jar包,即直接执行以下命令:

java -jar wbsdk-listener-1.0.0.jar

即可运行。可以copy到任何一台装有Java环境的服务器上运行。

original-wbsdk-listener-1.0.0.jar则只是项目的文件,它还会依赖lib目录下的jar包文件。这样子将项目和依赖文件分开打包,好处是部署的时候,如果不更新依赖,就只需要更新项目文件即可(比如快点,不然每次上传几十MB的文件到服务器~~~)

Spring Boot 插件

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <classifier>spring-boot</classifier>
                            <mainClass>
                                hello.App
                            </mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

参考资料

chenzhou123520 iteye