Contents
  1. 1. 多模块的项目 maven命令打包
  2. 2. maven打包命令

多模块的项目 maven命令打包

般情况都是在父级pom中配置打包的插件,其他module的pom不需要特别的配置,当配置完成后,点击idea中maven工具的package,就能执行一系列打包操作;

要对多个module的项目做打包,一般情况都是在父级pom中配置打包的插件,其他module的pom不需要特别的配置,当配置完成后,点击idea中maven工具的package,就能执行一系列打包操作;

img

这里先使用maven-jar-plugin插件,在父级pom中添加配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 1 <!--通过maven-jar-plugin插件打jar包-->
2 <plugin>
3 <groupId>org.apache.maven.plugins</groupId>
4 <artifactId>maven-jar-plugin</artifactId>
5 <version>2.4</version>
6 <configuration>
7 <archive>
8 <manifest>
9 <addClasspath>true</addClasspath>
10 <classpathPrefix>lib/</classpathPrefix>
11 <!--main入口-->
12 <mainClass>com.platform.WebApplication</mainClass>
13 </manifest>
14 </archive>
15 <!--包含的配置文件-->
16 <includes>
17 </includes>
18 <excludes>
19 </excludes>
20 </configuration>
21 </plugin>

上面的配置我们需要注意以下几个节点:

  • mainClass:我们需要指定main入口,当然这不是必须的,如果同一个project中有多个main入口,那打包的时候才需要,仅仅就一个main入口这个其实忽略;
  • classpathPrefix:指定加入classpath中依赖包所在的前缀文件夹名
  • addClasspath:依赖包放加入到classpath中,默认true
  • includes:需要包含在jar中的文件,一般不配置(注意:如果配置路径不合适,可能会吧class排除掉)
  • excludes:如果是要做jar包外部配置文件的话,这里需要用excludes排除这些配置文件一起打包在jar中

使用maven-jar-plugin插件针对项目工程来打包,这个时候通过maven的package命令打包,能看到jar中有一个lib文件夹(默认),其中包含了工程项目中所引入的第三方依赖包,通过java -jar xxx.jar能看到jar成功启动:

img

在规范的项目中,一般有dev,test,uat,pro等环境,针对这些个环境需要有不同的配置,springboot中可以通过application-dev|test|…yml来区分不同的配置,仅仅需要在默认的application.yml中加入spring.profiles.active=dev|test…就行了;

这种方式有个不便的地方,比如本地调试或发布上线都需要来回修改active的值(当然通过jar启动时,设置命令行active参数也可以),不是很方便;下面采用在pom中配置profiles,然后通过在idea界面上鼠标点击选择启动所用的配置;首先,在main层创建配置文件目录如下结构:

img

为了区分测试,这里对不同环境配置文件设置了server.port来指定不同端口(dev:3082,pro:3182)
然后,在父级pom中配置如下profiles信息:

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
 1     <profiles>
2 <profile>
3 <id>dev</id>
4 <!--默认运行配置-->
5 <activation>
6 <activeByDefault>true</activeByDefault>
7 </activation>
8 <properties>
9 <activeProfile>dev</activeProfile>
10 </properties>
11 </profile>
12 <profile>
13 <id>test</id>
14 <properties>
15 <activeProfile>test</activeProfile>
16 </properties>
17 </profile>
18 <profile>
19 <id>uat</id>
20 <properties>
21 <activeProfile>uat</activeProfile>
22 </properties>
23 </profile>
24 <profile>
25 <id>pro</id>
26 <properties>
27 <activeProfile>pro</activeProfile>
28 </properties>
29 </profile>
30 </profiles>

节点说明:

  • activeByDefault:设置为默认运行配置
  • activeProfile:所选择的启动配置,它的值对应上面创建profiles下面的dev|test|pro文件夹

然后,在pom中的build增加resources节点配置:

1
2
3
4
5
6
1 <resources>
2 <!--指定所使用的配置文件目录-->
3 <resource>
4 <directory>src/main/profiles/${activeProfile}</directory>
5 </resource>
6 </resources>

此刻我们的配置就完成了,正常情况下idea上maven模块能看到这样的图面:

img

这个时候仅仅只需要我们勾选这些个按钮就行了,不管是调试还是最后打包,都按照这个来获取所需的配置文件。

maven打包命令

mvn clean package -P dev|pro|test

Contents
  1. 1. 多模块的项目 maven命令打包
  2. 2. maven打包命令