好得很程序员自学网

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

使用maven如何将项目中的test代码打包进jar中

maven将项目的test代码打包进jar中

项目结构如下:

…/src/main/java/package/** <– application code

…/src/test/java/package/** <– test code

需求:

test中有一个包含main方法的主类TestMain.java,将main、test、全部依赖代码打包到一个jar包中,最终能通过java -jar的方式运行TestMain

这里Maven Jar Plugin 、 Maven Shade Plugin 都不是很适用。

我们选择Maven Assembly Plugin来打包

三步实现:

在pom中添加如下

?

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

< project >

     ...

     < build >

         < plugins >

             < plugin >

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

                 < version >2.3</ version >

                 < configuration >

                     < descriptor >src/main/assembly/assembly.xml</ descriptor >

                 </ configuration >

                 < executions >

                     < execution >

                         < id >make-assembly</ id >

                         < phase >package</ phase >

                         < goals >

                             < goal >single</ goal >

                         </ goals >

                         < configuration >

                             < archive >

                                 < manifest >

                                     < mainClass >com.sample.TestMain</ mainClass >

                                 </ manifest >

                             </ archive >

                         </ configuration >

                     </ execution >

                 </ executions >

             </ plugin >

             ...

         </ plugins >

     </ build >

     < dependencies >

       ...

     </ dependencies >

</ project >

在resources中添加一个assembly.xml文件

?

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

< assembly

     xmlns = "http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"

     xmlns:xsi = "http://HdhCmsTestw3.org/2001/XMLSchema-instance"

     xsi:schemaLocation = "http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd" >

     < id >fat-tests</ id >

     < formats >

         < format >jar</ format >

     </ formats >

     < includeBaseDirectory >false</ includeBaseDirectory >

     < dependencySets >

         < dependencySet >

             < outputDirectory >/</ outputDirectory >

             < useProjectArtifact >true</ useProjectArtifact >

             < unpack >true</ unpack >

             < scope >test</ scope >

         </ dependencySet >

     </ dependencySets >

     < fileSets >

         < fileSet >

             < directory >${project.build.directory}/test-classes</ directory >

             < outputDirectory >/</ outputDirectory >

             < includes >

                 < include >**/*.class</ include >

             </ includes >

             < useDefaultExcludes >true</ useDefaultExcludes >

         </ fileSet >

     </ fileSets >

</ assembly >

运行mvn package

在target目录下会生成一个xxxxx-1.0-SNAPSHOT-assembly.jar的文件,这就是我们需要的jar包了。我们可以用压缩软件看到生成的这个jar包中包含了test中所有的测试类。

可以直接使用java -jar xxxxx-1.0-SNAPSHOT-assembly.jar来运行jar包。主类就是第一步中设置的那个类

参考链接

maven打包跳过test

在pom.xml文件中添加插件

?

1

2

3

4

5

6

7

8

< plugin >

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

    < artifactId >maven-surefire-plugin</ artifactId >

    < version >2.18.1</ version >

    < configuration >

    < skipTests >true</ skipTests >

    </ configuration >

</ plugin >

使用mvn命令 带附加参数

?

1

mvn install -Dmaven.test.skip=true

或者

?

1

mvn install -DskipTests

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/zhaominpro/article/details/78522282

查看更多关于使用maven如何将项目中的test代码打包进jar中的详细内容...

  阅读:18次