maven管理多模块系统

1.创建mydemo项目
cd d:\mywork
mvn archetype:generate -DgroupId=com.example.mydemo -DartifactId=mydemo

生成mydemo目录,删除该目录下src目录,修改pom.xml,将packaging值修改为:
<packaging>pom</packaging>

2. 然后分别创建mydemo-core、mydemo-web两个模块
cd mydemo
mvn archetype:create -DgroupId=www.example.mydemo.core -DartifactId=mydemo-core
mvn archetype:create -DgroupId=www.example.mydemo.web -DartifactId=mydemo-web -DarchetypeArtifactId=maven-archetype-webapp

3. 修改d:\myworkpom.xml文件,添加
<modules>
 <module>mydemo-core</module>
 <module>mydemo-web</module>
</modules>

4. 分别在mydemo-core和mydemo-web目录下的pom.xml文件中添加:

<parent>
 <groupId>cn.webwork.mydemo</groupId>
 <artifactId>mydemo</artifactId>
 <version>1.0-SNAPSHOT</version>
</parent>

5. 然后在d:\mywork\mydemo-webpom.xml中添加依赖 mydemo-core包的信息,如下:

<dependency>
  <groupId>cn.webwork.mydemo.core</groupId>
  <artifactId>mydemo-core</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

关于pom.xml配置文件的详细情况,请参考:
Project Setting:http://maven.apache.org/maven-model/maven.html
Introduction to POM:http://maven.apache.org/guides/introduction/introduction-to-the-pom.html

6. 创建完成后,就会分别生成两个目录,然后分别对其生成相应的eclipse工程文件,方法为:
cd mydemo-core
mvn eclipse:eclipse

cd ..
cd mydemo-web
mvn eclipse:eclipse -DdownloadSource=true -Dwtpversion=1.0 (指定wtpversion属性,用于生成wtp的工程文件)

创建完成这两个eclipse工程文件后,就可以在eclipse中打开了。

7. 回到 d:mywork目录下,执行mvn compile 就可以对这两个文件进行编译,还有mvn install ,mvn package等命令。

mydemo文件下载:
http://project.livedoor.cn/~roger/mydemo.zip

关于mvn命令:
在上面的过程中,大家看到诸如 mvn archetype:create / mvn eclipse:eclipse等这些命令,如何查询这些命令更多帮助信息?
请参考:http://maven.apache.org/plugins/index.html
其实象archetype:create eclipse:eclipse等都是maven的插件,默认安装的时候,这些插件都已经安装,还有很多第三方的插件,
具体使用方式,都可以从上面的地址中查到。

如,site插件, Generate a site for the current project 
当你执行mvn site的时候,就会自动生成一个web site,用于展示你的项目信息。

POM.XML样例:

1. 完整项目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>de.codecentric</groupId>
    <artifactId>movie-database</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.0.RELEASE</version>
    </parent>

    <name>movie-database</name>
    <description></description>
    
    <modules>
        <module>movie-database-actors</module>
        <module>movie-database-commons</module>
        <module>movie-database-movies</module>
        <module>movie-database-navigation</module>
        <module>movie-database-security</module>
        <module>movie-database-shop-rest</module>
        <module>movie-database-monitoring</module>
    </modules>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-starter-admin-client</artifactId>
                <version>1.1.1</version>
            </dependency>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-server</artifactId>
                <version>1.1.1</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.hamcrest</groupId>
                <artifactId>hamcrest-all</artifactId>
                <version>1.3</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-all</artifactId>
                <version>2.0.2-beta</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

2. 子模块样例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>

    <parent>
        <groupId>de.codecentric</groupId>
        <artifactId>movie-database</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>movie-database-movies</artifactId>
    <packaging>jar</packaging>

    <name>movie-database-movies</name>
    <description></description>

    <dependencies>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>movie-database-commons</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>movie-database-security</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-starter-admin-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>2.2.1</version>
        </dependency>
    </dependencies>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <start-class>de.codecentric.MoviesApplication</start-class>
        <java.version>1.7</java.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

你可能感兴趣的:(多模块)