maven打包成一个jar,加入所有依赖包

  没有用spring框架,而是用最简单的maven方法新建工程,打包成jar。会要经历2个环节:

1、打包的jar要运行起来需要设置主类,否则java -jar跑不起来,报“no main manifest attribute”。需要使用maven-jar-plugin插件,配置主程序入口类:

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.hisense.main.Main</mainClass>   //主程序入口类
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

 

2、默认打包的jar没有把所依赖的jar一起打入,需要运行时指定lib目录。如果想打出可独立运行的jar,需要引入maven-shade-plugin或者maven-assembly-plugin插件,配置pom.xml文件:

  • shade插件:
    <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>2.3</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <transformers>
                                    <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <mainClass>util.Microseer</mainClass>
                                    </transformer>
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

    此插件生成的origin-前缀的jar是原始jar,不带依赖jar的。

  • assembly插件:
    <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>2.4.1</version>
                    <configuration>
                        <!-- get all project dependencies -->
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <!-- MainClass in mainfest make a executable jar -->
                        <archive>
                            <manifest>
                                <mainClass>util.Microseer</mainClass>
                            </manifest>
                        </archive>
     
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <!-- bind to the packaging phase -->
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

    注:此插件打出来的jar-with-dependencies才是是带依赖jar的包,另外那个是原始jar。可以通过执行mvn package assembly:single来直接打包,只执行mvn assembly:single不会把主类打入包

  • 这两种方式除了配置pom.xml之外,还需要在maven工程中配置Run Configurations。shade插件命令行为:shade:shade -f pom.xml,assembly插件命令行为:assembly:assembly -f pom.xml。双击运行该配置后得到对应的包。

  

posted @ 2020-08-31 21:33  badwood  阅读(3635)  评论(0编辑  收藏  举报
Badwood's Blog