maven介绍

1.引入好的maven文章:

            来源 1:  https://www.jianshu.com/p/0fb5e3fb704d【maven内部原理解析1】

            来源 2:  https://blog.csdn.net/lcgoing/article/details/91350857【maven内部原理解析2】  原作者:zlwen

 

maven内部运行原理解析1

maven至今还是Java编程语言构建的事实标准,大部分项目还在使用maven来进行构建,因此了解maven内部运行的原理对定位和分析问题还是很有裨益的。本篇文章主要介绍一些maven内部运行过程中的一些基本概念,相信看完后,对那么些刚刚接触maven的读者来说maven将不再陌生。
  在具体分析项目构建的过程前,需要了解maven的一些基本概念,这些概念十分重要,请务必理解清楚后再看下文。基本概念主要有:POM,Lifecycle。这两个概念又会包含一些小的概念,下文会逐步讲解。
  POM: 注意这里的POM不是maven中构建过程使用的配置文件pom.xml,但他们之间还是有联系的。POM的全称是Project Object Model,用通俗点的话说就是对要构建的项目进行建模,将要构建的项目看成是一个对象(Object),后文就使用PO来指代这个对象。既然是一个对象,那么PO有哪些属性呢?在maven中一个项目都是用一个唯一的坐标(coordinate)来表示,坐标由groupId, artifactId, version, classifier, type这五部分组成。这样来说PO应该也要具备坐标属性。另外一方面,一个项目肯定不是孤立存在的,可能依赖于其他的一些项目,那么也就是说PO应该具备dependencies这个属性,用来表示其所依赖的外部项目。我们可以尝试一下用Java代码来描述下PO这个对象:

class PO{
    private String groupId;
    private String artifactId;
    private String version;
    private String classifier;
    private String type;
    private Set<PO> dependencies;
}
 

我们知道XML的表达能力是很强大的,一个Java中的对象是可以用XML来描述,例如一个上面定义的PO对象则可以用下面的XML来描述(表达有不规范之处,理解其中的含义即可):

<PO>
    <groupId></groupId>
    <artifactId></artifactId>
    <version></version>
    <classifier><classifier>
    <type></type>
    <dependencies>
        <PO></PO>
        <PO></PO>
        ...
    </dependencies>
</PO>
 

是不是好像和什么有点类似?对,就是pom.xml。pom.xml就是PO对象的XML描述。上面的PO定义还不完整,我们继续看下PO还有什么其他的属性。我们知道在Java中类是可以继承的,一个对象在创建的时候会同时创建其父类对象,类似的,PO对象也有其父对象,用parent属性来表示,并且PO对象会继承其父对象的所有属性。另外一方面,一个项目可能根据不同职责分为多个模块(module),所有模块其实也就是一个单独的项目,只不过这些项目会使用其父对象的一些属性来进行构建。我们将这些新的属性加到PO的定义中去:

lass PO{
    private String groupId;
    private String artifactId;
    private String version;
    private String classifier;
    private String type;
    private Set<PO> dependencies;
    private PO parent;
    private Set<PO> modules;
}
 

再将这个定义用XML语言表示一下:

<PO>
    <parent></parent>
    <groupId></groupId>
    <artifactId></artifactId>
    <version></version>
    <classifier><classifier>
    <type></type>
    <dependencies>
        <PO></PO>
        <PO></PO>
        ...
    </dependencies>
    <modules>
        ...
    </modules>
</PO>
 

是不是越来越像pom.xml了?对,这就是pom.xml的由来。再说一遍:pom.xml就是PO对象的XML描述。到此为止,相信你再看pom.xml文件时,会有一个全新的认识。

  上面说的这些PO属性是项目的一些固有属性,到目前为止,我们还没有涉及项目的构建过程。构建过程对应的是PO对象的build属性,那么你应该马上想到,在pom.xml中就是<build>元素中的内容。这里就有引入maven中第二个核心概念:Lifecycle。Lifecycle直译过来就是生命周期。我们平常会接触到哪些周期呢?一年中春夏秋冬就是一个周期。一个周期中可能分为多个阶段,比如这里的春夏秋冬。在maven中一个构建过程就对应一个Lifecycle,这个Lifecycle也分为多个阶段,每个阶段叫做phase。你可能会问,那这个Lifecycle中包含多少个phase呢?一个标准的构建Lifecycle包含了如下的phase:

validate: 用于验证项目的有效性和其项目所需要的内容是否具备
initialize:初始化操作,比如创建一些构建所需要的目录等。
generate-sources:用于生成一些源代码,这些源代码在compile phase中需要使用到
process-sources:对源代码进行一些操作,例如过滤一些源代码
generate-resources:生成资源文件(这些文件将被包含在最后的输入文件中)
process-resources:对资源文件进行处理
compile:对源代码进行编译
process-classes:对编译生成的文件进行处理
generate-test-sources:生成测试用的源代码
process-test-sources:对生成的测试源代码进行处理
generate-test-resources:生成测试用的资源文件
process-test-resources:对测试用的资源文件进行处理
test-compile:对测试用的源代码进行编译
process-test-classes:对测试源代码编译后的文件进行处理
test:进行单元测试
prepare-package:打包前置操作
package:打包
pre-integration-test:集成测试前置操作     
integration-test:集成测试
post-integration-test:集成测试后置操作
install:将打包产物安装到本地maven仓库
deploy:将打包产物安装到远程仓库
 

在maven中,你执行任何一个phase时,maven会将其之前的phase都执行。例如 mvn install,那么maven会将deploy之外的所有phase按照他们出现的顺序一次执行。
  Lifecycle还牵涉到另外一个非常重要的概念:goal。注意上面Lifecycle的定义,也就是说maven为程序的构建定义了一套规范流程:第一步需要validate,第二步需要initialize... ... compile,test,package,... ... install,deploy,但是并没有定义每一个phase具体应该如何操作。这里的phase的作用有点类似于Java语言中的接口,只协商了一个契约,但并没有定义具体的动作。比如说compile这个phase定义了在构建流程中需要经过编译这个阶段,但没有定义应该怎么编译(编译的输入是什么?用什么编译javac/gcc?)。这里具体的动作就是由goal来定义,一个goal在maven中就是一个Mojo(Maven old java object)。Mojo抽象类中定义了一个execute()方法,一个goal的具体动作就是在execute()方法中实现。实现的Mojo类应该放在哪里呢?答案是maven plugin里,所谓的plugin其实也就是一个maven项目,只不过这个项目会引用maven的一些API,plugin项目也具备maven坐标。
  在执行具体的构建时,我们需要为lifecycle的每个phase都绑定一个goal,这样才能够在每个步骤执行一些具体的动作。比如在lifecycle中有个compile phase规定了构建的流程需要经过编译这个步骤,而maven-compile-plugin这个plugin有个compile goal就是用javac来将源文件编译为class文件的,我们需要做的就是将compile这个phase和maven-compile-plugin中的compile这个goal进行绑定,这样就可以实现Java源代码的编译了。有点绕,多看几遍。那么有人就会问,在哪里绑定呢?答案是在pom.xml<build>元素中配置即可。例如:

<build>
<plugins>
  <plugin>
    <artifactId>maven-myquery-plugin</artifactId>
    <version>1.0</version>
    <executions>
      <execution>
        <id>execution1</id>
        <phase>test</phase>
        <configuration>
          <url>http://www.foo.com/query</url>
          <timeout>10</timeout>
          <options>
            <option>one</option>
            <option>two</option>
            <option>three</option>
          </options>
        </configuration>
        <goals>
          <goal>query</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>
</build>
 

就将maven-myquery-plugin中的query这个goal绑定到了test这个phase,后续在maven执行到test phase时就会执行query goal。还有有人可能会问,我都没有指定Java源文件的位置,编译啥?这就引出了maven的design principle。在maven中,有一个非常著名的principle就是convention over configuration(约定优于配置)。这一点和ant有非常大的区别,例如使用ant来进行编译时,我们需要指定源文件的位置,输出文件的位置,javac的位置,classpath... ...在maven中这些都是不需要,若没有手动配置,maven默认从<项目根目录>/src/main/java这个目录去查找Java源文件,编译后的class文件会保存在<项目根目录>/target/classes目录。在maven中,所有的PO都有一个根对象,就是Super POM。Super POM中定义了所有的默认的配置项。Super POM对应的pom.xml文件可以在maven安装目录下lib/maven-model-builder-3.0.3.jar:org/apache/maven/model/pom-4.0.0.xml中找到。用一张图来表示maven Lifecycle,phase,goal之间的关系:

maven内部运行原理解析2

  在第一篇里主要介绍了maven的几个核心概念,这一篇里我们就以一个简单的例子来分析整个maven运行的过程。构建所使用的项目结构如下:

 
项目结构
主要是一个echo项目,其包含了两个module,分别是api和biz。echo项目的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>org.maven</groupId>
    <artifactId>echo</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>

    <modules>
        <module>api</module>
        <module>biz</module>
    </modules>
</project>

  

这里有个比较费解的地方就是<packaging>pom</packaging>。若<packaging>元素的内容是jar,那么我们很好理解,也就是说这个项目最终会被打包成一个jar包。那<packaging>元素为pom又是什么意思呢?从字面上的意思来看,这个项目将打包成一个pom。我们不妨去maven仓库里去瞧瞧(前提是已经在项目下运行了mvn install命令)。我们发现在<maven仓库路径>/org/maven/echo/1.0.0目录下有一个echo-1.0.0.pom文件,细心的读者可能已经发现这个文件其实和echo项目中的pom.xml是同一个文件。这样做的目的是什么呢?还记得第一篇中说过的PO对象吧,我们说过PO对象也是有继承关系的,比如说这里echo项目对应的PO对象就是api项目对应的PO对象的父对象(api项目是echo项目的一个module,在api项目的pom.xml中<parent>元素的内容所对应的就是echo项目),而echo项目PO对象的父对象又是哪个呢?答案是Super POM对应的PO对象。这就是maven中project inheritance的概念。当实际执行maven命令的时候,会根据project inheritance关系对项目的pom.xml进行转化,得到真正执行时所用到的pom.xml,即所谓的effective pom。而<packaging>pom</packaging>的作用就在这里。因此可以得到一个结论:所有<packaging>元素为pom的项目其实并不会输出一个可供外部使用,类似于jar包的东西。这类项目的作用有两个:

  • 管理子项目
    例如这里的api和biz就是echo项目的两个module。若没有echo这个父项目,我们需要到api和biz两个项目下分别执行mvn install命令才能完成整个构建过程,而有了echo这个父项目之后,我们只需在echo项目中执行mvn install即可,maven会解析pom.xml,发现该项目有api和biz两个module,它会分别到这两个项目下去执行mvn install命令。当module数量比较多的时候,能大大提高构建的效率。

  • 管理继承属性
    比如api和biz都需要某个依赖,那么在echo项目的pom.xml中声明即可,因为根据PO对象的继承关系,api和biz项目会继承echo项目的依赖,这样就可以减少一些重复的输入。

effective pom包含了当前项目的PO对象,直到Super POM对应的PO对象中的信息。要看一个项目的effective pom,只需在项目中执行

mvn help:effective-pom

命令即可查看。这里顺带说一句,有的同学可能不理解上面这个命令是什么意思。maven命令的语法为

mvn [options] [goal(s)] [phase(s)]
这里出现了第一篇中讲述的两个概念:goal和phase。maven允许你执行一个或者多个goals/phases。很明显这面的命令help:effective-pom并不是一个phase(maven构建过程中的phase请参考上一篇文章),那么也就是说它是一个goal。对这个goal只不过是采用了缩写的形式,其全称是这样的:
org.apache.maven.plugins:maven-help-plugin:2.2:effective-pom

以分号为分隔符,包含了groupId,artifactId,version,goal四部分。若groupId为org.apache.maven.plugins则可以使用上述的简写形式。也就是说

mvn help:effective-pom
mvn org.apache.maven.plugins:maven-help-plugin:2.2:effective-pom

是等价的,都是执行了maven-help-plugin这个plugin中的effective-pom这个goal。

    好了,继续回到effective pom。我们说过maven在真正构建的时候用的就是effective pom,那么说明effective pom中包含了构建的所有信息,我们以biz项目中的effective pom为例来看下effective pom长什么样子。在biz项目中执行mvn help:effective-pom命令,你会得到如下输出:

    <?xml version="1.0"?>
    <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>
    <parent>
      <groupId>org.maven</groupId>
      <artifactId>echo</artifactId>
      <version>1.0.0</version>
    </parent>
    <groupId>org.maven</groupId>
    <artifactId>echo-biz</artifactId>
    <version>1.0.0</version>
    <dependencies>
      <dependency>
        <groupId>org.maven</groupId>
        <artifactId>echo-api</artifactId>
        <version>1.0.0</version>
        <scope>compile</scope>
      </dependency>
    </dependencies>
    <build>
      <sourceDirectory>/Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/main/java</sourceDirectory>
      <scriptSourceDirectory>/Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/main/scripts</scriptSourceDirectory>
      <testSourceDirectory>/Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/test/java</testSourceDirectory>
      <outputDirectory>/Users/allstarw/workspace/own-proj/misc/maven/echo/biz/target/classes</outputDirectory>
      <testOutputDirectory>/Users/allstarw/workspace/own-proj/misc/maven/echo/biz/target/test-classes</testOutputDirectory>
      <resources>
        <resource>
          <directory>/Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/main/resources</directory>
        </resource>
      </resources>
      <testResources>
        <testResource>
          <directory>/Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/test/resources</directory>
        </testResource>
      </testResources>
      <directory>/Users/allstarw/workspace/own-proj/misc/maven/echo/biz/target</directory>
      <finalName>echo-biz-1.0.0</finalName>
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>2.4.1</version>
          <executions>
            <execution>
              <id>default-clean</id>
              <phase>clean</phase>
              <goals>
                <goal>clean</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.3.1</version>
          <executions>
            <execution>
              <id>default-install</id>
              <phase>install</phase>
              <goals>
                <goal>install</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>2.5</version>
          <executions>
            <execution>
              <id>default-resources</id>
              <phase>process-resources</phase>
              <goals>
                <goal>resources</goal>
              </goals>
            </execution>
            <execution>
              <id>default-testResources</id>
              <phase>process-test-resources</phase>
              <goals>
                <goal>testResources</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.10</version>
          <executions>
            <execution>
              <id>default-test</id>
              <phase>test</phase>
              <goals>
                <goal>test</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>2.3.2</version>
          <executions>
            <execution>
              <id>default-testCompile</id>
              <phase>test-compile</phase>
              <goals>
                <goal>testCompile</goal>
              </goals>
            </execution>
            <execution>
              <id>default-compile</id>
              <phase>compile</phase>
              <goals>
                <goal>compile</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>2.3.2</version>
          <executions>
            <execution>
              <id>default-jar</id>
              <phase>package</phase>
              <goals>
                <goal>jar</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.7</version>
          <executions>
            <execution>
              <id>default-deploy</id>
              <phase>deploy</phase>
              <goals>
                <goal>deploy</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
    </project>
篇幅有点长,省略了部分内容。对比biz项目的pom.xml,我们发现effective pom中增加了Super POM中继承过来的一些配置,比如说<sourceDirectory>定义了biz项目的源码路径,以及Lifecycle中各个phase绑定的goal:
[phase]     [goal]
compile     maven-compiler-plugin:2.3.2:compile
package     maven-jar-plugin:2.3.2:jar
install     maven-install-plugin:2.3.1:install
... ...

有了effective pom的概念之后,再来看maven构建的输出日志,是不是有点豁然开朗的感觉?

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order: 
[INFO] 
[INFO] echo
[INFO] echo-api
[INFO] echo-biz
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building echo 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ echo ---
[INFO] Installing /Users/allstarw/workspace/own-proj/misc/maven/echo/pom.xml to /Users/allstarw/.m2/repository/org/maven/echo/1.0.0/echo-1.0.0.pom
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building echo-api 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ echo-api ---
[debug] execute contextualize
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/allstarw/workspace/own-proj/misc/maven/echo/api/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ echo-api ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/allstarw/workspace/own-proj/misc/maven/echo/api/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ echo-api ---
[debug] execute contextualize
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/allstarw/workspace/own-proj/misc/maven/echo/api/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ echo-api ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ echo-api ---
[INFO] No tests to run.
[INFO] Surefire report directory: /Users/allstarw/workspace/own-proj/misc/maven/echo/api/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ echo-api ---
[INFO] Building jar: /Users/allstarw/workspace/own-proj/misc/maven/echo/api/target/echo-api-1.0.0.jar
[INFO] 
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ echo-api ---
[INFO] Installing /Users/allstarw/workspace/own-proj/misc/maven/echo/api/target/echo-api-1.0.0.jar to /Users/allstarw/.m2/repository/org/maven/echo-api/1.0.0/echo-api-1.0.0.jar
[INFO] Installing /Users/allstarw/workspace/own-proj/misc/maven/echo/api/pom.xml to /Users/allstarw/.m2/repository/org/maven/echo-api/1.0.0/echo-api-1.0.0.pom
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building echo-biz 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ echo-biz ---
[debug] execute contextualize
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ echo-biz ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ echo-biz ---
[debug] execute contextualize
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ echo-biz ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ echo-biz ---
[INFO] No tests to run.
[INFO] Surefire report directory: /Users/allstarw/workspace/own-proj/misc/maven/echo/biz/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ echo-biz ---
[INFO] 
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ echo-biz ---
[INFO] Installing /Users/allstarw/workspace/own-proj/misc/maven/echo/biz/target/echo-biz-1.0.0.jar to /Users/allstarw/.m2/repository/org/maven/echo-biz/1.0.0/echo-biz-1.0.0.jar
[INFO] Installing /Users/allstarw/workspace/own-proj/misc/maven/echo/biz/pom.xml to /Users/allstarw/.m2/repository/org/maven/echo-biz/1.0.0/echo-biz-1.0.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] echo .............................................. SUCCESS [0.461s]
[INFO] echo-api .......................................... SUCCESS [2.581s]
[INFO] echo-biz .......................................... SUCCESS [0.382s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.593s
[INFO] Finished at: Wed Jul 06 00:22:50 CST 2016
[INFO] Final Memory: 10M/156M
[INFO] ------------------------------------------------------------------------
日志的输出十分清楚,分别构建echo,api,biz这三个项目(因为biz项目依赖api项目,因此api项目需要首先构建)。对每个项目构建时,将lifecycle中的phase所对应的goal依次执行。


2.maven命令

         1.如果觉得使用mvn ......命令时,输出到命令行的数据太多,可以使用以下命令将命令行的数据输出到文件

        mvn -X install  > test.log   或者  mvn install -X | tee log.txt 

 

         2. 查看某个插件的作用及子命令(个人理解为goals)       

     mvn help:describe -Dplugin=assembly      <--表示查看assembly插件的信息--->

 

 

 

3.assmbly举例子

             1.首先创建父工程 maven_package_test

             2.创建子工程maven_package_child_one

             3.创建子工程maven_package_child_two

     我要实现的功能如下:

            我要将maven_package_child_one和maven_package_child_two中下的部分文件以及两个工程打的包汇总到一个具体的目录下。

            网上大多数都是直接在父工程中配置,然后打包,之前我没有成功,从新创建一个子工程start_package专门用于打包,具体配置如下

            工程总体目录:

             

 

            1.父项目不用配置任何打包插件

            2.start_package项目为打包汇总项目,pom打包插件配置如下

<build>
        <plugins>
            <plugin>
                <!--负责编译的插件-->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <!--插件版本,不配置该插件会有默认的编译插件配置-->
                <version>3.8.0</version>
            </plugin>
            <plugin>
                <!--自定义的打包插件-->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <finalName>output</finalName>
                    <descriptors>
                        <!--自定义的插件配置文件-->
                        <descriptor>src/main/pack/package.xml</descriptor>
                    </descriptors>
                    <!--配置输出目录,配置output,会在target下创建output文件夹,然后将配置文件中的输出输出到/target/output下-->
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase><!--绑定生命周期为package-->
                        <goals>
                            <goal>single</goal><!--表示该插件只运行一次 -->
                            <!--mvn help:describe -Dplugin=assembly-->
                            <!--执以上语句,则会解释插件assembly,你会看到很多goals,每个goal有自己的含义,可以看成插件的小命令-->
                        </goals>
                        <!-- 需要注意的是当配置该项之后,如果发生You must set at least one file. -> [Help 1]异常,
                        检查配置文件中的<directory>,很有可能这里配置的路径不对-->
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

           对应src/main/pack/目录下的 package.xml内容如下:

<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id></id>
    <formats>
        <format>tar.gz</format>
        <format>dir</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <!--将maven_package_child_one module下已经打成的包copy到指定目录-->
            <directory>../maven_package_child_one/target/output/</directory>
            <includes>
                <include>**/*.*</include>
            </includes>
            <outputDirectory>all</outputDirectory>
        </fileSet>
        <fileSet>
            <!--将maven_package_child_two module下已经打成的包copy到指定目录-->
            <directory>../maven_package_child_two/target/output/</directory>
            <includes>
                <include>**/*.*</include>
            </includes>
            <outputDirectory>all</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

      3.maven_package_child_one和maven_package_child_two的pom文件插件配置和start_package一模一样

      4.maven_package_child_one和maven_package_child_two的配置文件package.xml分别如下:

       maven_package_child_one

<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id></id>
    <formats>
        <format>tar.gz</format>
        <format>dir</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>src/main/assembly</directory>
            <includes>
                <!--*表示当前文件夹往下一层,**表示所有子文件夹和文件,**/*.jar,子文件中以jar结尾的文件-->
                <include>**/*.*</include>
            </includes>
            <outputDirectory>maven_package/maven_package_child_one</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>target</directory>
            <includes>  <!--打包时会先生成target下的...jar,然后将该jar copy到目标目录下-->
                <include>**/*.jar</include>
            </includes>
            <outputDirectory>maven_package/maven_package_child_one</outputDirectory>
        </fileSet>
    </fileSets>

</assembly>

        maven_package_child_two

<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id></id>
    <formats>
        <format>tar.gz</format>
        <format>dir</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>src/main/pack</directory>
            <includes>
                <!--*表示当前文件夹往下一层,**表示所有子文件夹和文件,**/*.jar,子文件中以jar结尾的文件-->
                <include>**/*.*</include>
            </includes>
            <outputDirectory>maven_package/maven_package_child_two</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>target</directory>
            <includes>  <!--打包时会先生成target下的...jar,然后将该jar copy到目标目录下-->
                <include>**/*.jar</include>
            </includes>
            <outputDirectory>maven_package/maven_package_child_two</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

注意配置的路径都需要有,否则有可能会有异常

            这样配置之后,单独打包某个前两个项目也是可以的,打包总项目就可以将结果汇总到start_package的target下

 

 
 
posted @ 2020-10-11 18:13  小虎。。。。  阅读(265)  评论(0)    收藏  举报