好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

maven 打包项目的几种方式

Maven可以使用mvn package指令对项目进行打包,如果使用Java -jar xxx.jar执行运行jar文件,会出现"no main manifest attribute, in xxx.jar"(没有设置Main-Class)、ClassNotFoundException(找不到依赖包)等错误。

要想jar包能直接通过java -jar xxx.jar运行,需要满足:

1、在jar包中的META-INF/MANIFEST.MF中指定Main-Class,这样才能确定程序的入口在哪里;

2、要能加载到依赖包。

使用Maven有以下几种方法可以生成能直接运行的jar包,可以根据需要选择一种合适的方法。

方法一:使用maven-jar-plugin和maven-dependency-plugin插件打包

这种方式生成jar包有个缺点,就是生成的jar包太多不便于管理,其它两种方式只生成一个jar文件,包含项目本身的代码、资源以及所有的依赖包。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

< build >

         < plugins >

             <!--      配置打包,并配置MainClass, 但不将依赖包打包,会出现找不到依赖的异常    -->

             < plugin >

                 < groupId >org.apache.maven.plugins</ groupId >

                 < artifactId >maven-jar-plugin</ artifactId >

                 < configuration >

                     < archive >

                         < manifest >

                             < addClasspath >true</ addClasspath >

                             <!--   MANIFEST.MF 中 Class-Path -->

                             < classpathPrefix >lib/</ classpathPrefix >

                             < mainClass >cn.zoom.wqz.filter.Application</ mainClass >

                         </ manifest >

                     </ archive >

                 </ configuration >

             </ plugin >

             <!--      配置依赖包      -->

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

                             <!--              将依赖包打包至target下的lib目录              -->

                             < outputDirectory >${project.build.directory}/lib</ outputDirectory >

                         </ configuration >

                     </ execution >

                 </ executions >

             </ plugin >

         </ plugins >

     </ build >

方法二:使用maven-assembly-plugin插件打包

打包方式:

?

1

mvn package assembly:single 

配置方式:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

< plugin >

         < groupId >org.apache.maven.plugins</ groupId >

         < artifactId >maven-assembly-plugin</ artifactId >

         < configuration >

             < archive >

                 < manifest >

                     < mainClass >cn.zoom.wqz.filter.Application</ mainClass >

                 </ manifest >

             </ archive >

             < descriptorRefs >

                 < descriptorRef >jar-with-dependencies</ descriptorRef >

             </ descriptorRefs >

         </ configuration >

     </ plugin >

打包后会在target目录下生成一个xxx-jar-with-dependencies.jar文件,这个文件不但包含了自己项目中的代码和资源,还包含了所有依赖包的内容。所以可以直接通过java -jar来运行。

此外还可以直接通过mvn package来打包,无需assembly:single,不过需要加上一些配置:

  不过,如果项目中用到spring Framework,用这种方式打出来的包运行时会出错,使用下面的方法三可以处理。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

< plugin >

      < groupId >org.apache.maven.plugins</ groupId >

      < artifactId >maven-assembly-plugin</ artifactId >

      < configuration >

          < archive >

              < manifest >

                  < mainClass >cn.zoom.wqz.filter.Application</ mainClass >

              </ manifest >

          </ archive >

          < descriptorRefs >

              < descriptorRef >jar-with-dependencies</ descriptorRef >

          </ descriptorRefs >

      </ configuration >

      <!-- 其中<phase>package</phase>、<goal>single</goal>即表示在执行package打包时,执行assembly:single -->

      < executions >

          < execution >

              < id >make-assembly</ id >

              < phase >package</ phase >

              < goals >

                  < goal >single</ goal >

              </ goals >

          </ execution >

      </ executions >

  </ plugin >

方法三:使用maven-shade-plugin插件打包

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

< plugin >

     < groupId >org.apache.maven.plugins</ groupId >

     < artifactId >maven-shade-plugin</ artifactId >

     < executions >

         < execution >

             < phase >package</ phase >

             < goals >

                 < goal >shade</ goal >

             </ goals >

             < configuration >

                 < transformers >

                     < transformer implementation = "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" >

                         < mainClass >cn.zoom.wqz.filter.Application</ mainClass >

                     </ transformer >

                 </ transformers >

             </ configuration >

         </ execution >

     </ executions >

</ plugin >

配置完成后,执行mvn package即可打包。在target目录下会生成两个jar包,注意不是original-xxx.jar文件,而是另外一个。和maven-assembly-plugin一样,生成的jar文件包含了所有依赖,所以可以直接运行。

如果项目中用到了Spring Framework,将依赖打到一个jar包中,运行时会出现读取XML schema文件出错。原因是Spring Framework的多个jar包中包含相同的文件spring.handlers和spring.schemas,如果生成一个jar包会互相覆盖。为了避免互相影响,可以使用AppendingTransformer来对文件内容追加合并:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

< plugin >

                 < groupId >org.apache.maven.plugins</ groupId >

                 < artifactId >maven-shade-plugin</ artifactId >

                 < executions >

                     < execution >

                         < phase >package</ phase >

                         < goals >

                             < goal >shade</ goal >

                         </ goals >

                         < configuration >

                             < transformers >

                                 < transformer implementation = "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" >

                                     < mainClass >cn.zoom.wqz.filter.Application</ 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 >

到此这篇关于maven 打包的几种方式的文章就介绍到这了,更多相关maven 打包内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://www.cnblogs.com/yelao/p/11675923.html

查看更多关于maven 打包项目的几种方式的详细内容...

  阅读:28次