JavaWeb technology into the world 12: packaging from manually compile the project to build tools Maven

Micro-channel public number [yellow] small ramp programmer manufacturers, the Internet industry new knowledge, lifelong learning practitioner. After concerns reply "Java", "Python", "C ++", "big data", "machine learning", "algorithm", "AI", "Android", "front-end", "iOS", "PubMed", " BAT "," school trick "," written "," interview "," surface can be obtained via the corresponding free learning materials "," computer Basics "," LeetCode "and other keyword. 

 

                     

Mike's Build the road (on)

Editor: Liu Xin  yard farmers stand  2016-07-10

  • Abstract: Hand Build trouble either to stay in the city and his girlfriend, Mike certainly go to North Canton struggle. Now he can only stay in this city of 2.5 lines, enter this software development company, 7-8 individuals, 10 guns, is a typical software small workshops. First day of work, CTO and chief architect and project manager and development team leader Zhang Li called to go, he inculcate said: "Li ah, I read your resume, I tell you quite optimistic about the company's development but as a newcomer, you are not familiar with the new business, can not develop the core system, this time, you want while learning, while helping the project to be a very important task: Build "Alan heart that you pull back to me English ah, although she thought, Mike or not
  • Hand Build troubles

    If it were not for his girlfriend and stay in a city, to the north of Guangzhou Li certainly struggle to go.

     

    Now he can only stay in this city of 2.5 lines, enter this software development company, 7-8 individuals, 10 guns, is a typical software small workshops.

     

    First day of work, CTO and chief architect and project manager and development team leader Zhang Li called to go, he inculcate said:

     

    "Li ah, I read your resume, I have for you in the company's development is very promising, but as a rookie, you are not familiar with the new business, can not develop the core system, this time, you want while learning while helping the project to be a very important task: Build "

     

    Alan heart that you pull back to me in English ah, although she thought, Mike still quietly, smiling asked:

     

    "This Build is what?"

     

    Zhang said: "I am very busy, no time to explain to you, here's a document, you see to know."

     

    Then, Zhang training and preparation him several sheets of paper, added: "There are questions to ask Wang, he had come a month than you do already very familiar Build"

     

    Mike carefully read it again, read:

     

    XXX Company Build Process (test environment)

    (1) Set the Eclipse workspace, encoded as UTF-8, java compiler level JDK 1.7

     

    (2) Download the latest source code from SVN to the Eclipse workspace

     

    (3) ensure that the work area is no compilation errors Eclispe

     

    (4) manually modify the following profiles 20

    database.properties

    cache.properties

    user.properties

    。。。。。。

    (5) in the Eclipse Web project export to War package

    Wang also added a particularly marked here in red pen: at Web project depends java other items will be automatically included in the package of War WEB-INF / lib directory

     

    (6) uploaded to the test server, install

     

    (7) to do a smoke test

     

    Li smiled: Is not that a compile, package, deploy, test it also processes Build?!

     

    Just then, the backbone of the development of the trabecular called Li: "Alan, I changed a few Bug, soon to be tested, and quickly gave me Build a test environment."

     

    Li not neglect, according to the document immediately do it again, it took nearly half an hour before the toss completed.

     

    But to the last step, do the smoke test of time, the system did not start!

     

    Mike checked for a long time only to find that the test environment is JDK 1.6, but the writing on the Build document is of course not run up a 1.7.

     

    Li Wang secretly curse predecessors: Here you kid must know there is a pit, how not marked out on the document?

     

    New Build quickly be placed in a test environment, test the smoke passed.

     

    Just relieved, test Xiao Zhao called out: "trabecular, that you have not Bug fix ah"

     

    The backbone of the development of the trabecular instinctive reaction to: "It's impossible I have tested the local code, the code also submitted!"

     

    Trabecular then the finger of blame points to Mike: "hey Yes Alan, your Build is not wrong again."

     

    Li heart started, and quickly went to check, and sure enough, in step 4, you modify configuration files when the database make mistakes, pointing to the development of the library, rather than testing library.

     

    Quickly change it, it had to do Build Wang also ran along for the ride, under strict attention Build Commissioner Wang, development and testing trabecular Xiao Zhao three pairs of eyes front, head Li must sweating.

     

    Fortunately, the third time finally succeeded. All tests were passed.

     

    (In fact, Mike in a tense busy forgot to update the document, change the JDK 1.6)

     

    So, after a week, Mike had to do four or five Build trembling every day, although to do more and more mature, less and less mistakes, but still take up a lot of time every day.

     

    Wasted the best years would yet in Build, the firm can not.

    Automated Build

    Mike decided to put this manual, laborious, error-prone Build up to automation, the future anyone who is doing Build a test environment, just run a command.

     

    What language to achieve it? Of course, Java Dafa is good! Li at the University of practicing for so long, think of OO, design patterns has reached a pinnacle, and now finally come into play.

     

    Li worked during the day, in the evening returned it to develop the automated Build, dry day to 12 points before they give up.

     

    But Mike does not feel tired, they are reluctant to go to bed every day to sleep, for the creation of a new tool for the benefit of everyone's idea has inspired their own, and sometimes even feel very happy.

     

    A month later, freshly baked automation tools, this is actually a set of Java API, Li called it BuildTool V1.0 dedicated to download the source code, compile, package, deploy, test.

    For example, if you want to compile java code, you can write:

    Mike's Build the road (on) _Java

    Li is very proud to FileSet this abstract, it represents a collection of files, either java source file path, the path may be the classpath.

     

    Other API like to download the source code, packaging, deployment, testing is similar.

     

    Now Mike really only need to run a command, you can test environment that generates a build:

    java BuildTool test

     

    All of a sudden a lot less effort, and the machine is running, basically can not go wrong.

     

    Li because the automated BuildTool, the company obtained the awards, but also pay up a bit.

     

    For Mike, this is not the most important, most importantly through the design and implementation of this BuildTool, their ability has been greatly improved.

     

    It has not only a HTML fill in the blank with only a SSH framework of the staff!

    Comment code farmers stand: Most people will complain that the project is very boring, without challenge, the status quo will only encounter problems,

    Few people will find the "pain points" problems at work, and really begin to solve it, to bring the value of which is to improve yourself, make an important way to distinguish themselves and others in.

    JAVA vs XML

    This year's situation is very good, good business development, recruit a number of new, suddenly it took three or four new projects, Mike volunteered to build an automated Build for these projects.

     

    But Li soon discovered the problem, the direct use of Java language to write, although the function can be achieved, but it seems too cumbersome.

    Write your own code to see they have a few days to think about in order to understand.

    It is not you own BuildTool API design? That was well designed ah.

     

    Think carefully about two days, Li has finally realized the problem: not of their own design is not good, the Java language is too "low" again!

     

    Build automation tasks to be described in fact, is what the operational level.

     

    And with java is a universal language can do anything, be sure to use it to write the introduction of too much detail, resulting in difficulty to read and write!

     

    Li thought: Can you develop a new, specialized language used for the automation Build it?

    (Code farmers turn over NOTE: This is actually the so-called domain-specific language (Domain Specific Language, referred to as DSL))

     

    But developing a new language that may cost a little higher, a little more harm than good.

     

    Small Li Baisi understand why, until you hear the trabecular configuration files and project manager in the discussion hibernate one day, suddenly thought like spring, hibernate are using XML to describe the system.

     

    "That's my BuildTool also can use XML to describe ah" Mike quickly put the java compiler program described in XML for a moment:

    Mike's Build the road (on) _Java

    It really is much cleaner! And compared to the original Java program, this XML is almost self-explanatory!

     

    XML may be highly scalable, it can be any custom labels such as <javac> <srcDir> <classpath> used to describe a logical Build.

     

    But the only place that is uncomfortable: XML can not run Java programs like that, just plain text only.

     

    不过这也无妨,只要用Java写一个解析器,用来解析这些XML文件然后在Java中执行就可以了。有了BuildTool V1.0作为基础, 写一个解析器不是什么难事, 很快 BuildTool V2.0 就新鲜出炉了。

     

    小李不再帮其他项目组去写Build 程序,因为用XML描述以后,大家很快就能学会, 并且乐在其中。

    CTO老张看到这个工具,大为赞赏, 它给小李说: “别叫什么Build Tool, 太俗, 别人听了一点感觉都没有, 我给你起个名,叫 ANT ”

     

    "ANT? " 小李似乎看到很多小蚂蚁在不辞劳苦帮着做Build, 心里暗暗佩服老张: 这个名字起的太好了, 姜还是老的辣啊。

小李的Build之路(下)

转自: 刘欣 码农翻身 2016-07-12

前言: 接上一篇《小李的Build之路(上)

 

小李发明的ANT确实是好用, 现在不仅仅是小李的公司, 连其他公司的朋友听说了,也拿去使用, 交口称赞。

 

只是小李发现了一点奇怪的现象,每个人在开始写新项目的Ant build文件之前, 都会找到自己说:

“小李, 把你那个build.xml 文件发我一份吧, 让我参考下。”

 

小李的那一份build.xml其实是自己项目的第一个ant 脚本, 为啥大家都要把它

Copy走呢?   刚开始的时候小李以为大家不会写,要按照自己的模板照葫芦画瓢。 

 

偶然有一次,小李看到了别人项目的Ant build脚本, 不由得大吃一惊, 这简直和自己原始的build.xml如出一辙。 

 

小李赶紧把公司内所有项目的Ant脚本都要过来,仔细观察了一下, 很快就发现了这些脚本中蕴藏着一些共同的“模式”,这些模式主要体现在Build的步骤上:

 

1. 从版本控制系统下载代码

2. 编译java 文件,形成jar包

3. 运行单元测试

4. 生成代码覆盖度报告和测试报告

4. 打包形成war 文件

5. 上传到测试服务器上,进行安装

 

其实这也难怪,实际的Build不就是这样的嘛。 但是中间也有不同之处:

 

(1) 路径不同,例如

java 源文件 下载下来以后放的位置不同,五花八门

编译成的java class 放置的位置不同

测试报告放置不同

war包生成后放置的路径不同

。。。

 

(2)  项目依赖不同,例如

各个项目依赖的第三方jar包可能是不一样的

各个项目都有一个Web子项目,它依赖于其他java 项目,所以在build的时候,要先build这些java 项目才行

例如下图中的OnlineShop,这是个Web项目, 它依赖于ApplicationConfg, LoggingFramework, OnlineShopApi这三个子项目。

 

项目依赖这个没办法, 毕竟是各个项目的业务所要求的,小李没有办法改变。

 

但是那些不同的路径真的是必要的吗?   能不能让大家的路径都保持一致呢 ? 

 

一个新的主意像闪电一样划过黑暗的夜空: 

确实可以保持一致, 但是大家都要遵循一定的约定

如果大家都这么做,小李就可以增强一下Ant,只要运行ant  complie , 就会自动的去src/main/java 找到源文件进行编译, 只要运行ant test,        就会自动去src/test/java 找到测试用例, 编译并运行。

 

换句话说,只要遵循目录的约定, 大家就不用费心费力的指定各种路径了, 一切在背后由工具自动搞定, 这样的话Build脚本就可以极大的简化了,只需要寥寥几行即可。

 

这只是一个java项目,要是多个java项目,有依赖关系,像上面提到的 OnlineShop 依赖OnlineShopAPI, AppplicationConfig, LoggingFramework , 该怎么处理?

 

这也不难, 小李想,首先每个java项目都得遵守上述约定,其次需要定义项目之间的依赖关系, 也可以用XML描述出来。

 

每个java项目都需要有个叫pom.xml的文件, 例如OnlineShop这个项目的pom如下:

 

这样以来工具就能自动找到被依赖的项目, 然后去编译打包它了。

 

此外,各个java项目之间也需要按约定来组织目录,例如:

+- pom.xml

+- online-shop-web

| +- pom.xml

| +- src

|   +- main

|     +- webapp

+- online-shop-api

| +- pom.xml

| +- src

|   +- main

|     +- java

+- logging-framework

| +- pom.xml

| +- src

|   +- main

|     +- java

+- app-config

| +- pom.xml

| +- src

|   +- main

|     +- java

 

如果扩展一下, 把第三方的jar 文件例如JUnit 也可以给用这种方式来描述:

想到这一层,小李不禁激动起来,因为第三方的jar 管理一直是一个令人头疼的问题,最早的时候大家都是手工的Copy来Copy去, 由于版本不同导致的错误特别难于发现。

 

每个人在建立自己Eclipse workspace的时候, 得拿到所有依赖的jar包, 并且在项目上设置好, 可是非常的费劲啊。 

 

如果利用这种声明的办法, 每个人岂不卸下了一个巨大的包袱 ? 

 

当然公司需要建立一个公用的第三方jar 文件版本库, 把公司最常用的第三方jar包都放进去, 工具在分析项目的配置文件pom.xml的时候,就可以去公司的版本库里去读取并且下载到本地。

 

将来有新人进入公司, 只要给他一个pom.xml , 用Eclipse导入,就能轻松的把一个可以直接运行的workspace建立起来, 再也不需要设置那些烦心的jar了。

 

如果将来在网络上建立公开的软件版本库, 任何人都可以从那里去下载各种软件包,那受惠的可不仅仅是自己公司了, 而是所有人,真是一个激动人心的场景啊。

 

不过还是从自己公司开始吧, 小李冷静下来分析了一下: 让所有的项目组都使用约定的目录,并且建立一个公司级别的软件库,自己可是没有这样的权限啊, 小李去找CTO老张求助。

 

老张不愧是老江湖, 听了几分钟小李的介绍,马上就明白了, 并且把这个想法提升了一个高度:

 

“你这叫约定重于配置, 知道不?  从Ruby on Rails 开始,这个词开始流行了, 大家现在都很忙, Ant build脚本用的也没问题,先不改了”

 

小李还不死心: “可是这么做的话对以后的新项目大有好处啊,不用Copy 繁琐的build脚本了, 也不用费心的折腾workspace了”

 

“那也不能现在改,项目进度最重要,大家都没时间, 这样吧,等大家项目闲下来再改动如何? ” 老张妥协了一下。

 

可是在公司基本上就不会有空闲的时间, 一个个新需求压的大家透不过气来,偶尔有空闲时间,大家也都犯懒了, 总是想休息。 

 

此外惯性的力量是惊人的,大家都愿意待在舒适区里, 不愿意变化,  虽然也看到了新工具的好处, 大家都懒得换新的。 

 

时间过的很快,一年过去了, 小李看着自己辛辛苦苦加班写出来的Ant 2.0 ,  还是无人采用, 很是伤心。 

 

经过公司的允许, 小李决定把这个工具开源, 为了和Ant区分开来, 特地起了个新的名称: Maven。

 

Maven 迅速被大家用了起来,除了小李的公司。

 

又过了半年, 小李跳槽了。 

 

(完)

 

(完)

Maven 三十分钟入门

阅读 1164

收藏 95

2017-05-08

原文链接:sadwxqezc.github.io

Maven

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model(POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.

参考文档:Maven Documentation

Maven概述

一个基本的Pom文件

<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.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Maven Quick Start Archetype</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Pom文件是面向工程的一个管理模型,全称是Project Object Model。这个Pom文件中包含的基本内容有:

  1. project pom文件的顶层元素
  2. modelVersion Pom对象模型的版本
  3. groupId 是创建这个项目的组织或部门的唯一标志,比如说org.apache.maven.plugins就是所有Maven plugins的groupId
  4. artifactId 是这个项目生成结果的唯一标志,Maven生成结果的命名模式为<artifactId>-<version>.<extension>,比如说my app-1.0.jar
  5. packaging 这个参数表明了项目生成结果的打包模式,比如JAR,WAR,EAR等,同时这个参数还表明了build过程采用的特定生命周期
  6. version 这个参数表明了项目生成结果的版本,在version里经常会看到SNAPSHOT标志,它表明了项目处于开发阶段。
  7. name 这个参数代表项目的展示名字,通常作用在Maven生成的文档中
  8. url 这个参数代表哪里可以找到该项目,通常作用在Maven生成的文档中
  9. description 这个参数描述了项目的基本信息,通常作用在Maven生成的文档中

Maven项目的基本结构

my-app
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- mycompany
    |               `-- app
    |                   `-- App.java
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java

上面是一个Maven项目的基本结构,项目的相关资源位于${basedir}/src/main/java中,而测试的资源位于${basedir}/src/test/java中,而pom文件位于pom.xml中。

Maven的执行

1. mvn compile

执行mvn compile,编译的结果会默认放在${basedir}/target/classes目录下。

2. mvn test

执行mvn test,会执行${basedir}/src/test/java中的单元测试。如果只想编译测试代码而不执行,则执行mvn test-compile

3. mvn package

执行mvn package会对项目进行打包,假如当前在pom中的packaging设定为jar,那么执行该命令后会在${basedir}/target目录下生成对应的jar包。

4. mvn install

如果想把mvn package生成的Jar文件安装在本地库中以让其它项目引用,则可以执行mvn install命令,将生成的jar包放在${user.home}/.m2/repository中。

Plugins

Plugins用来定制Maven项目的编译过程,假如要配置Java Compiler允许JDK 5.0的资源,那么只需要在Pom中增加如下内容:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.3</version>
      <configuration>
        <source>1.5</source>
        <target>1.5</target>
      </configuration>
    </plugin>
  </plugins>
</build>

<configuration>中的配置会应用在对应Plugin的所有Goal。

资源文件打包

${basedir}/src/main/resources目录下的所有文件都会被打包到Jar文件中,比如如下一个文件结构:

my-app
|-- pom.xml
`-- src
    |-- main
    |   |-- java
    |   |   `-- com
    |   |       `-- mycompany
    |   |           `-- app
    |   |               `-- App.java
    |   `-- resources
    |       `-- META-INF
    |           `-- application.properties
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java

该项目打包成Jar文件后的内部组织结构为:

|-- META-INF
|   |-- MANIFEST.MF
|   |-- application.properties
|   `-- maven
|       `-- com.mycompany.app
|           `-- my-app
|               |-- pom.properties
|               `-- pom.xml
`-- com
    `-- mycompany
        `-- app
            `-- App.class

文件过滤

Maven支持文件过滤的功能,可以在build时候为文件提供变量赋值,比如说上面的application.properties文件中有如下定义:

# application.properties
application.name=${project.name}
application.version=${project.version}

那么需要在pom文件中做如下的定义:

<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.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>Maven Quick Start Archetype</name>
  <url>http://maven.apache.org</url>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
  </build>
</project>

执行mvn process-resources命令后,在target/classes下找到application.properties可以看到如下结果:

# application.properties
application.name=Maven Quick Start Archetype
application.version=1.0-SNAPSHOT

可见其中形如${<property name>}的变量已经被替换成了对应的值,如果要引入其它文件中定义的属性,只需要在pom文件中定义<filters>,比如:

  <build>
    <filters>
      <filter>src/main/filters/filter.properties</filter>
    </filters>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
  </build>

那么Maven会先读出filter.properties中的属性,然后把这些属性注入对应的resources中。

Dependencies

<dependencies>标签下列出了所有的外部依赖,比如下面的Pom文件添加了Junit的依赖:

<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.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>Maven Quick Start Archetype</name>
  <url>http://maven.apache.org</url>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

<dependency><scope>标签的值可以为compile,test和runtime,当Maven编译项目时,它首先会在${user.home}/.m2/repository这个本地库目录下寻找所需的依赖,如果没有会去远程的库上寻找,并将其下载到本地库中,默认的远程库地址为 http://repo.maven.apache.org/maven2/

包的发布

当需要发布一个包的远程仓库时,需要配置库的地址和相应的权限,一个范例如下:

<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.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>Maven Quick Start Archetype</name>
  <url>http://maven.apache.org</url>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.codehaus.plexus</groupId>
      <artifactId>plexus-utils</artifactId>
      <version>1.0.4</version>
    </dependency>
  </dependencies>

  <build>
    <filters>
      <filter>src/main/filters/filters.properties</filter>
    </filters>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
  </build>
  <!--
   |
   |
   |
   -->
  <distributionManagement>
    <repository>
      <id>mycompany-repository</id>
      <name>MyCompany Repository</name>
      <url>scp://repository.mycompany.com/repository/maven2</url>
    </repository>
  </distributionManagement>
</project>

它所需要的权限配置在settings.xml中:

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                      http://maven.apache.org/xsd/settings-1.0.0.xsd">
  ...
  <servers>
    <server>
      <id>mycompany-repository</id>
      <username>jvanzyl</username>
      <!-- Default value is ~/.ssh/id_dsa -->
      <privateKey>/path/to/identity</privateKey> (default is ~/.ssh/id_dsa)
      <passphrase>my_key_passphrase</passphrase>
    </server>
  </servers>
  ...
</settings>

多Modules管理

Maven很好的支持了多个Modules的管理,假如一个Maven项目结构如下:

+- pom.xml
+- my-app
| +- pom.xml
| +- src
|   +- main
|     +- java
+- my-webapp
| +- pom.xml
| +- src
|   +- main
|     +- webapp

对于根目录下的pom文件,内容如下:

<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.mycompany.app</groupId>
  <artifactId>app</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <modules>
    <module>my-app</module>
    <module>my-webapp</module>
  </modules>
</project>

其中的<modules>定义了其管理的两个子modules。

假如my-webapp需要依赖my-app包,那么在my-webapp/pom.xml中增加:

  ...
  <dependencies>
    <dependency>
      <groupId>com.mycompany.app</groupId>
      <artifactId>my-app</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    ...
  </dependencies>

然后在my-app和my-webapp的pom文件中都增加parent配置:

<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">
  <parent>
    <groupId>com.mycompany.app</groupId>
    <artifactId>app</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  ...

然后在最顶层目录下执行mvn clean install,会创建my-webapp/target/my-webapp.war,其中包含:

$ jar tvf my-webapp/target/my-webapp-1.0-SNAPSHOT.war
   0 Fri Jun 24 10:59:56 EST 2005 META-INF/
 222 Fri Jun 24 10:59:54 EST 2005 META-INF/MANIFEST.MF
   0 Fri Jun 24 10:59:56 EST 2005 META-INF/maven/
   0 Fri Jun 24 10:59:56 EST 2005 META-INF/maven/com.mycompany.app/
   0 Fri Jun 24 10:59:56 EST 2005 META-INF/maven/com.mycompany.app/my-webapp/
3239 Fri Jun 24 10:59:56 EST 2005 META-INF/maven/com.mycompany.app/my-webapp/pom.xml
   0 Fri Jun 24 10:59:56 EST 2005 WEB-INF/
 215 Fri Jun 24 10:59:56 EST 2005 WEB-INF/web.xml
 123 Fri Jun 24 10:59:56 EST 2005 META-INF/maven/com.mycompany.app/my-webapp/pom.properties
  52 Fri Jun 24 10:59:56 EST 2005 index.jsp
   0 Fri Jun 24 10:59:56 EST 2005 WEB-INF/lib/
2713 Fri Jun 24 10:59:56 EST 2005 WEB-INF/lib/my-app-1.0-SNAPSHOT.jar

可见my-app-1.0-SNAPSHOT.jar已经被放到了WEB-INF/lib目录下。

 

 

A technical station Ali Java engineers. Author Huang oblique, focused Java related technologies: SSM, SpringBoot, MySQL, distributed, middleware, cluster, Linux, network, multi-threaded, occasionally speaking point Docker, ELK, as well as dry goods and technology to share the learning experience, committed to Java full stack development! (No reply after public concern "Java" Java can receive basic, advanced, and the project architect and other free learning materials, more databases, distributed, service and other popular micro learning video technology, rich content, both theory and practice, also presented will be the original author of the study guide Java, Java programmer interview guide and other dry goods resources)

Guess you like

Origin www.cnblogs.com/xll1025/p/11366483.html