• Maven创建多模块项目(包括依赖版本号的统一更新)


    0、多项目工程的文件夹及依赖关系

    bus-core-api为公共项目,app-web-ui依赖bus-core-api,app-desktop-ui依赖bus-core-api

    1、创建一个父Maven工程

    mvn archetype:generate -DgroupId=com.jsoft.test -DartifactId=testproject -Dversion=1.0-SNAPSHOT -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=pom-root -DinteractiveMode=false -DarchetypeVersion=RELEASE

    注意:此项目为pom类型的工程,创建好之后只有一个pom.xml文件,<packaging>类型为pom。

    创建好后的pom.xml文件内容如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <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.jsoft.test</groupId>
      <artifactId>testproject</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>pom</packaging>
      <name>testproject</name>
    </project>

    2、创建三个子Maven工程

    注意:此时创建的工程的目录是进去父Maven工程的目录进行创建

    ①创建bus-core-api项目

    mvn archetype:generate -DgroupId=com.jsoft.test -DartifactId=bus-core-api -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false -DarchetypeVersion=RELEASE

    ②创建app-desktop-ui项目

    mvn archetype:generate -DgroupId=com.jsoft.test -DartifactId=app-desktop-ui -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false -DarchetypeVersion=RELEASE

    ③创建app-web-ui项目

    mvn archetype:generate -DgroupId=com.jsoft.test -DartifactId=app-web-ui -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false -DarchetypeVersion=RELEASE

    此时创建好项目之后,父Maven工程pom.xml也会跟着改变,自动加入<Module>节点,如下所示:

    <?xml version="1.0" encoding="UTF-8"?>
    <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.jsoft.test</groupId>
      <artifactId>testproject</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>pom</packaging>
      <name>testproject</name>
      <modules>
        <module>bus-core-api</module>
        <module>app-desktop-ui</module>
        <module>app-web-ui</module>
      </modules>
    </project>

    而通过进入到父Maven工程的目录创建的子Maven工程,pom.xml也会做相应的变化,增加了<parent>节点,内容如下:

    bus-core-api:

    <?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.jsoft.test</groupId>
        <artifactId>testproject</artifactId>
        <version>1.0-SNAPSHOT</version>
      </parent>
      <groupId>com.jsoft.test</groupId>
      <artifactId>bus-core-api</artifactId>
      <version>1.0-SNAPSHOT</version>
      <name>bus-core-api</name>
      <url>http://maven.apache.org</url>
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </project>

    app-desktop-ui:

    <?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.jsoft.test</groupId>
        <artifactId>testproject</artifactId>
        <version>1.0-SNAPSHOT</version>
      </parent>
      <groupId>com.jsoft.test</groupId>
      <artifactId>app-desktop-ui</artifactId>
      <version>1.0-SNAPSHOT</version>
      <name>app-desktop-ui</name>
      <url>http://maven.apache.org</url>
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </project>

    app-web-ui:

    <?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.jsoft.test</groupId>
        <artifactId>testproject</artifactId>
        <version>1.0-SNAPSHOT</version>
      </parent>
      <groupId>com.jsoft.test</groupId>
      <artifactId>app-web-ui</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>war</packaging>
      <name>app-web-ui Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
      <build>
        <finalName>app-web-ui</finalName>
      </build>
    </project>

    而此时项目还没有完工,因为bus-core-api为公共项目,app-web-ui依赖bus-core-api,app-desktop-ui依赖bus-core-api,所以必须在app-web-ui和app-desktop-ui增加对bus-core-api的依赖引用。修改后的pom.xml如下:

    app-desktop-ui:

    <?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.jsoft.test</groupId>
        <artifactId>testproject</artifactId>
        <version>1.0-SNAPSHOT</version>
      </parent>
      <groupId>com.jsoft.test</groupId>
      <artifactId>app-desktop-ui</artifactId>
      <version>1.0-SNAPSHOT</version>
      <name>app-desktop-ui</name>
      <url>http://maven.apache.org</url>
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>com.jsoft.test</groupId>
          <artifactId>bus-core-api</artifactId>
          <version>1.0-SNAPSHOT</version>
        </dependency>
      </dependencies>
    </project>

    app-web-ui:

    <?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.jsoft.test</groupId>
        <artifactId>testproject</artifactId>
        <version>1.0-SNAPSHOT</version>
      </parent>
      <groupId>com.jsoft.test</groupId>
      <artifactId>app-web-ui</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>war</packaging>
      <name>app-web-ui Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>com.jsoft.test</groupId>
          <artifactId>bus-core-api</artifactId>
          <version>1.0-SNAPSHOT</version>
        </dependency>
      </dependencies>
      <build>
        <finalName>app-web-ui</finalName>
      </build>
    </project>

    可以看出,每个项目上都需要增加对bus-core-api的依赖,而且这个依赖需要写上<version>节点,上面就已经写上了1.0-SNAPSHOT。

    接下来测试依赖是否成功,先进入父Maven项目的目录,然后进行打包mvn package

    打包完成后,打开app-web-ui.war包,查看WEB-INFlib目录下是否有bus-core-api-1.0-SNAPSHOT.jar的引用。

    如上所示已经成功引用。

    至此,使用Maven新建多模块项目的过程就已经完成了。

    参考:

    http://stackoverflow.com/questions/6328778/how-to-create-an-empty-multi-module-maven-project

    https://maven.apache.org/pom.html#Aggregation

    测试工程:https://github.com/easonjim/5_java_example/tree/master/maventest/test9/testproject

    但是,可能会有一个疑问,比如说我bus-core-api这个项目在下面两个项目上的依赖,当bus-core-api更新版本号时,难道app-web-ui和app-desktop-ui项目上的pom.xml文件的<version>也要做相应的更改吗?完全要自己手动去修改吗?

    经过研究可以通过下面方法实现:

    1、在父Maven项目定义一个属性<properties>叫做<projectVersion>,然后每个项目的<version>节点都引用这个变量${projectVersion},包括依赖上的<version>都使用这个。那么当更新版本时手动修改一处地方即可。虽然解决了大量工作,但是小量的修改还是可以接受的。

    2、也许能行,通过maven-release-plugin插件实现,参考:http://maven.apache.org/maven-release/maven-release-plugin/examples/update-versions.html

    3、终极解决方法,使用versions-maven-plugin插件,官网:http://www.mojohaus.org/versions-maven-plugin/index.html

    当使用此插件在父Maven项目testproject时,运行如下命令将更新全部项目的版本号,包括子项目之间的依赖也都同步更新:

    mvn versions:set -DnewVersion=2.0-SNAPSHOT

    当进入到子Maven项目bus-core-api时,运行如下命令将更新全部项目对bus-core-api项目引用的版本号:

    mvn versions:set -DnewVersion=2.1-SNAPSHOT

    当更改版本号时有问题,可以通过以下命令进行版本号回滚:

    mvn versions:revert

    如果一切都没有问题,那就直接提交版本号:

    mvn versions:commit

    通过这个插件就可以对特定项目更新版本号时,其它与它有关联的项目都可以一并更新。

    其它使用方法,参考:http://www.mojohaus.org/versions-maven-plugin/plugin-info.html

    还有使用此插件需要注意的,比如自己手动修改了某个文件的版本号,那么这样通过这个插件去更新时是更新不到的,因为匹配不上手动修改的版本号。如果要使其生效,就必须更改成统一的版本号。

  • 相关阅读:
    shell遍历文件夹并执行命令
    安装PIL的坑
    iptables不小心把127.0.0.1封了,导致redis连不上
    python读取中文
    不要在基类析构函数中调用纯虚函数,否则运行时会报错“pure virtual method called”
    阿里云64位centos6.3系统上编译安装redis
    Git
    Easy Mock
    Sortable
    几个框架
  • 原文地址:https://www.cnblogs.com/EasonJim/p/6863987.html
Copyright © 2020-2023  润新知