Maven多模块——打包指定模块

mvn -h 查看命令及其用途:

E:\nicole\workspace\test_parent>mvn -h

usage: mvn [options] [<goal(s)>] [<phase(s)>]

Options:
 -am,--also-make                        If project list is specified, also
                                        build projects required by the
                                        list
 -amd,--also-make-dependents            If project list is specified, also
                                        build projects that depend on
                                        projects on the list
 -B,--batch-mode                        Run in non-interactive (batch)
                                        mode (disables output color)
 -b,--builder <arg>                     The id of the build strategy to
                                        use
 -C,--strict-checksums                  Fail the build if checksums don't
                                        match
 -c,--lax-checksums                     Warn if checksums don't match
 -cpu,--check-plugin-updates            Ineffective, only kept for
                                        backward compatibility
 -D,--define <arg>                      Define a system property
 -e,--errors                            Produce execution error messages
 -emp,--encrypt-master-password <arg>   Encrypt master security password
 -ep,--encrypt-password <arg>           Encrypt server password
 -f,--file <arg>                        Force the use of an alternate POM
                                        file (or directory with pom.xml)
 -fae,--fail-at-end                     Only fail the build afterwards;
                                        allow all non-impacted builds to
                                        continue
 -ff,--fail-fast                        Stop at first failure in
                                        reactorized builds
 -fn,--fail-never                       NEVER fail the build, regardless
                                        of project result
 -gs,--global-settings <arg>            Alternate path for the global
                                        settings file
 -gt,--global-toolchains <arg>          Alternate path for the global
                                        toolchains file
 -h,--help                              Display help information
 -l,--log-file <arg>                    Log file where all build output
                                        will go (disables output color)
 -llr,--legacy-local-repository         Use Maven 2 Legacy Local
                                        Repository behaviour, ie no use of
                                        _remote.repositories. Can also be
                                        activated by using
                                        -Dmaven.legacyLocalRepo=true
 -N,--non-recursive                     Do not recurse into sub-projects
 -npr,--no-plugin-registry              Ineffective, only kept for
                                        backward compatibility
 -npu,--no-plugin-updates               Ineffective, only kept for
                                        backward compatibility
 -nsu,--no-snapshot-updates             Suppress SNAPSHOT updates
 -ntp,--no-transfer-progress            Do not display transfer progress
                                        when downloading or uploading
 -o,--offline                           Work offline
 -P,--activate-profiles <arg>           Comma-delimited list of profiles
                                        to activate
 -pl,--projects <arg>                   Comma-delimited list of specified
                                        reactor projects to build instead
                                        of all projects. A project can be
                                        specified by [groupId]:artifactId
                                        or by its relative path
 -q,--quiet                             Quiet output - only show errors
 -rf,--resume-from <arg>                Resume reactor from specified
                                        project
 -s,--settings <arg>                    Alternate path for the user
                                        settings file
 -t,--toolchains <arg>                  Alternate path for the user
                                        toolchains file
 -T,--threads <arg>                     Thread count, for instance 2.0C
                                        where C is core multiplied
 -U,--update-snapshots                  Forces a check for missing
                                        releases and updated snapshots on
                                        remote repositories
 -up,--update-plugins                   Ineffective, only kept for
                                        backward compatibility
 -v,--version                           Display version information
 -V,--show-version                      Display version information
                                        WITHOUT stopping build
 -X,--debug                             Produce execution debug output

假设Maven多模块项目如下:
在这里插入图片描述
test-parent pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.nicole</groupId>
	<artifactId>test-parent</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	<name>test-parent</name>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<modules>
		<module>test-common</module>
		<module>test-module1</module>
		<module>test-module2</module>
		<module>test-module3</module>
	</modules>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>com.nicole</groupId>
				<artifactId>test-common</artifactId>
				<version>0.0.1-SNAPSHOT</version>
			</dependency>
		</dependencies>
	</dependencyManagement>
</project>

test-common pom.xml:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.nicole</groupId>
    <artifactId>test-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>test-common</artifactId>
  <name>test-common</name>
  <url>http://maven.apache.org</url>
  <packaging>jar</packaging>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
</project>

test-module1 pom.xml:

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.nicole</groupId>
		<artifactId>test-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>test-module1</artifactId>
	<packaging>war</packaging>
	<name>test-module1 Maven Webapp</name>
	
	<dependencies>
		<dependency>
			<groupId>com.nicole</groupId>
			<artifactId>test-common</artifactId>
		</dependency>
	</dependencies>
	<build>
		<finalName>test-module1</finalName>
	</build>
</project>

test-common被test-module1,test-module2,test-module3给继承。
示例一、打包所有模块

E:\nicole\workspace\test_parent>mvn clean install
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for test-parent 0.0.1-SNAPSHOT:
[INFO]
[INFO] test-parent ........................................ SUCCESS [  0.623 s]
[INFO] test-common ........................................ SUCCESS [  3.274 s]
[INFO] test-module1 Maven Webapp .......................... SUCCESS [  0.966 s]
[INFO] test-module2 Maven Webapp .......................... SUCCESS [  0.434 s]
[INFO] test-module3 Maven Webapp .......................... SUCCESS [  0.678 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.424 s
[INFO] Finished at: 2019-12-30T18:01:30+08:00
[INFO] ------------------------------------------------------------------------

示例二、-pl 打包指定模块

E:\nicole\workspace\test_parent>mvn clean install -pl test-common,test-module1
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for test-common 0.0.1-SNAPSHOT:
[INFO]
[INFO] test-common ........................................ SUCCESS [  3.494 s]
[INFO] test-module1 Maven Webapp .......................... SUCCESS [  1.056 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.909 s
[INFO] Finished at: 2019-12-30T18:02:39+08:00
[INFO] ------------------------------------------------------------------------

示例三、-am 同时打包所指定模块的依赖模块

E:\nicole\workspace\test_parent>mvn clean install -pl test-module1 -am
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for test-parent 0.0.1-SNAPSHOT:
[INFO]
[INFO] test-parent ........................................ SUCCESS [  0.559 s]
[INFO] test-common ........................................ SUCCESS [  3.198 s]
[INFO] test-module1 Maven Webapp .......................... SUCCESS [  1.020 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.127 s
[INFO] Finished at: 2019-12-30T18:04:49+08:00
[INFO] ------------------------------------------------------------------------

示例四、-amd 同时打包依赖于所指定模块的模块

E:\nicole\workspace\test_parent>mvn clean install -pl test-common -amd
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for test-common 0.0.1-SNAPSHOT:
[INFO]
[INFO] test-common ........................................ SUCCESS [  3.497 s]
[INFO] test-module1 Maven Webapp .......................... SUCCESS [  1.178 s]
[INFO] test-module2 Maven Webapp .......................... SUCCESS [  0.536 s]
[INFO] test-module3 Maven Webapp .......................... SUCCESS [  0.746 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.330 s
[INFO] Finished at: 2019-12-30T18:06:11+08:00
[INFO] ------------------------------------------------------------------------

示例五、-rf 从所指定模块顺序开始打包

E:\nicole\workspace\test_parent>mvn clean install -rf test-module2
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for test-module2 Maven Webapp 0.0.1-SNAPSHOT:
[INFO]
[INFO] test-module2 Maven Webapp .......................... SUCCESS [  2.146 s]
[INFO] test-module3 Maven Webapp .......................... SUCCESS [  0.489 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.128 s
[INFO] Finished at: 2019-12-30T18:06:43+08:00
[INFO] ------------------------------------------------------------------------

示例六、-pl -amd -rf 对裁剪后的模块堆再次裁剪

E:\nicole\workspace\test_parent>mvn clean install -pl test-common -amd -rf test-module1
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for test-module1 Maven Webapp 0.0.1-SNAPSHOT:
[INFO]
[INFO] test-module1 Maven Webapp .......................... SUCCESS [  2.195 s]
[INFO] test-module2 Maven Webapp .......................... SUCCESS [  0.523 s]
[INFO] test-module3 Maven Webapp .......................... SUCCESS [  0.633 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.889 s
[INFO] Finished at: 2019-12-30T18:07:46+08:00
[INFO] ------------------------------------------------------------------------

-pl -amd 得到test-common,test-module1,test-module2,test-module3
rf 从test-module1开始打包

  • 9
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Maven是一个非常常用的构建工具,在使用它构建多模块工程时,可以减少一些重复的工作,提高工程构建的效率。多模块工程即将一个大型工程分成多个模块,每个模块有自己的代码、配置文件和构建规则,模块可以独立构建,也可以重新组合成一个整体再进行构建。有了Maven这个工具,我们可以更加轻松地进行工程构建及部署。下面是Maven模块工程打包部署的一些要点: 1. 父工程:每个模块将会有一个父工程。父工程管理和统一构建所有子模块,同时父工程中也包含了一些工具依赖,如插件和配置等。 2. 子模块:每个子模块可以单独构建成一个独立的jarwar或者其他类型的文件,但是它们都是在父工程中定义的。每个子模块应该有独立的代码库,独立的测试用例和独立的依赖库。 3. 依赖管理:每个模块都有自己的依赖库,同时父工程也可以统一管理所有子模块的依赖。 4. 打包部署:对于一个多模块工程,最终会有一个多个模块组成的大型部署包。我们需要定义如何将每个子模块打包文件合并到最终的大包中。 总的来说,Maven提供了丰富的构建工具和支持,使得多模块工程打包部署变得更加简单和高效。在实际应用中,我们可以利用Maven的依赖管理,提高组件的复用率,也可以利用多模块工程的优势,让我们的代码更加清晰易懂,更容易维护和管理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值