java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Maven多模块打包指定模块

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

作者:CZ__

这篇文章主要介绍了使用Maven多模块——打包指定模块问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

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多模块项目

如下:

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

总结

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

您可能感兴趣的文章:
阅读全文