IntelliJ Idea14 创建Maven多模块项目

Maven多模块项目的参考资料

Sonatype上的教程 http://books.sonatype.com/mvnex-book/reference/multimodule.html

在这个教程里, 提供了一个多模块的maven项目例子供下载, 可以直接套用于实际项目.

IntelliJ Idea14里的创建步骤

1. File -> New -> Project -> Maven, 选择SDK后, 不要选Create from archetype, -> Next, 

2. 填写GroupId, 例如 com.mydomain.myproject, 填写ArtifactId, 例如 hold-on-service 我建的是dubbo项目, 这里填个大service名称, Version 默认, -> Next

3. 填写Project name, 就用ArtifactId, 如 hold-on-service, 选好项目地址, -> Finish

4. 新建好的项目里, 删掉系统创建的src目录

5. 开始创建子模块. File -> New -> Module -> Maven, 如果是公共模块, 就不要选Create from archetype, -> Next, 如果不是公共模块, 例如是实际的service提供模块, 则根据需要选择相应模板.

6. GroupId和版本按默认, ArtifactId 根据模块类型填写, 例如 hold-on-service-api, 或者 hold-on-service-impl, 一路Next -> Finsh

7. 编辑主项目的pom.xml, 注意要指定compile的source和target, 不然每次IDEA里面reimport之后, 配置都会被重置, 每次要改很麻烦

<?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.mydomain.holdon</groupId>
    <artifactId>vk-session</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <name>Holdon Project: Session Service</name>

    <modules>
        <module>session-service-api</module>
        <module>session-service-impl</module>
    </modules>

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

8. 编辑公共子模块的pom.xml, 注意packaging类型

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

    <parent>
        <artifactId>vk-session</artifactId>
        <groupId>com.mydomain.holdon</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <name>Holdon Project: Session Service API</name>

    <artifactId>vk-session-api</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
    </dependencies>


</project>

9. 编辑实体子模块的pom.xml, 注意这里要添加对公共模块的依赖, 不然每次reimport后, 依赖关系会丢失

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

    <parent>
        <artifactId>vk-session</artifactId>
        <groupId>com.mydomain.holdon</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <name>Holdon Project: Session Service Publish</name>

    <artifactId>vk-session-impl</artifactId>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <org.springframework.version>3.1.1.RELEASE</org.springframework.version>
    </properties>

    <dependencies>
        <!-- Spring Dependencies Start -->
        <!-- Core utilities used by other modules, Define this if you use Spring
            Utility APIs(org.springframework.core.*/org.springframework.util.*) -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <!-- Spring Dependencies End -->
        ......
        <dependency>
            <groupId>com.mydomain.holdon</groupId>
            <artifactId>vk-session-api</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

</project>

可以设置一个全局的pom, 把所有的模块都组织起来.一些公司里公用的dependency以及版本, 都配置在

这个pom只配置在svn和CI工具里, deploy的时候, 会自动checkout整个最新的主干, deploy到maven repository.

开发的时候, 只需要使用这个下面的大模块, pom里面的parent, maven会自动到repository下找依赖.

Update: 新建项目时, 用最顶层的pom建, 会自动变成多层模块的项目, 然后把顶层的项目delete掉, 然后第二层的module就变成顶层了, 而且顶层的pom.xml还在项目里.

Update 2017-04-08: 以上适用于使用svn仓库的项目, 不适用于git

Update 2018-10-11: 在Maven 中除了用继承parent来做版本控制以外, 还可以通过 scope=import 来做版本控制. 

Maven 2.0.9 添加了一个新特性: scope = import 也就是从外部导入依赖管理的依赖配置. 使用过 DependencyManagement都知道, 多模块构建项目的时候, 为了解决各模块依赖版本一致, 统一在父pom.xml文件中统一定义各个依赖jar文件的版本, 然后子模块通过引用的方式添加需要的依赖jar, 但随着项目工程越来越大, 添加的依赖文件也越来也多, 父pom.xml的 DependencyManagement 也会越来越长, 不便于管理. 另外, 因为pom.xml 是单继承的, 如果已经有一个父pom.xml了, 想要再引用外部的pom.xml, 就只能复制到父pom.xml中了. 因此采用在 DependencyManagement 中使用scope = import的方式可以分类引入更多的管理配置, 也摆脱了单继承的限制. 举例, 在子模块的<project></project>中增加这一段用于引入其他pom.xml的依赖配置

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.rockbb</groupId>
                <artifactId>my-java-pom</artifactId>
                <version>1.2-SNAPSHOT</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

而这个被引入的pom, 可以按下面的例子格式编写

<?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.rockbb</groupId>
    <artifactId>my-java-pom</artifactId>
    <version>1.2-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <kbsjava.default.version>1.2-SNAPSHOT</kbsjava.default.version>

        <springboot.version>2.0.3.RELEASE</springboot.version>
        <c3p0.version>0.9.5.2</c3p0.version>
        <mysql.version>6.0.3</mysql.version>
        <mybatis.version>3.4.1</mybatis.version>
        <mybatis-spring.version>1.3.0</mybatis-spring.version>
        ...
        <dom4j.version>2.1.1</dom4j.version>
    </properties>

    <repositories>
        <repository>
            <id>...</id>
            <name>...</name>
            <url>https://...</url>
        </repository>
    </repositories>

    <distributionManagement>
        <repository>
            <id>...</id>
            <name>...</name>
            <url>https://...</url>
        </repository>
        <snapshotRepository>
            <id>...</id>
            <name>...</name>
            <url>https://...</url>
            <uniqueVersion>false</uniqueVersion>
        </snapshotRepository>
    </distributionManagement>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${springboot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            ...
            <dependency>
                <groupId>org.dom4j</groupId>
                <artifactId>dom4j</artifactId>
                <version>${dom4j.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值