maven创建多模块项目——命令行方式

来源:互联网 发布:动画知已图片 编辑:程序博客网 时间:2024/06/03 20:25

maven创建多模块项目——命令行方式

如题,下面上货。
之所以使用多模块项目是为了扩展和重用时的方便。

1、首先创建一个普通的maven项目:
mvn archetype:generate -DarchetypeCatalog=internal
2、创建完成后,进入到项目文件夹中,修改pom文件
<groupId>com.xueyou</groupId><artifactId>mutilModuleParent</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging>

3、在文件中执行命令
mvn package

4、在文件夹内在创建一个项目
mvn archetype:generate -DarchetypeCatalog=internal

<groupId>com.xueyou.mutilmodulecore</groupId><artifactId>mutilmodule-core</artifactId><version>1.0-SNAPSHOT</version><name>mutilmodule-core</name>

5、进入mutimodule-core文件夹下执行命令:
mvn package

6、回到上层文件夹,创建一个项目
mvn archetype:generate -DarchetypeCatalog=internal

<groupId>com.xueyou.mutilmoduleextension</groupId><artifactId>mutilmodule-extension</artifactId><version>1.0-SNAPSHOT</version><name>mutilmodule-extension</name>

7、进入mutilmodule-extension文件夹中,执行命令:
mvn package

8、使用idea打开项目。
效果如下图所示。


9、删除最外层的src文件夹


10、修改最外层的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.xueyou</groupId>    <artifactId>mutilModuleParent</artifactId>    <version>1.0-SNAPSHOT</version>    <packaging>pom</packaging>    <name>mutilModuleParent</name>    <url>http://maven.apache.org</url>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <junit.version>3.8.1</junit.version>    </properties>    <dependencyManagement>        <dependencies>            <dependency>                <groupId>junit</groupId>                <artifactId>junit</artifactId>                <version>${junit.version}</version>                <scope>test</scope>            </dependency>        </dependencies>    </dependencyManagement>    <modules>        <module>mutilmodule-core</module>        <module>mutilmodule-extension</module>    </modules></project>

这里在dependencies的外层添加了dependencyManagement。添加之后在子pom中需要jar包的版本就都根据父pom中的版本来。

下面是两个子pom
<?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.xueyou</groupId>        <artifactId>mutilModuleParent</artifactId>        <version>1.0-SNAPSHOT</version>    </parent>    <groupId>com.xueyou.mutilmodulecore</groupId>    <artifactId>mutilmodule-core</artifactId>    <version>1.0-SNAPSHOT</version>    <name>mutilmodule-core</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>            <scope>test</scope>        </dependency>    </dependencies></project>

<?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.xueyou</groupId>        <artifactId>mutilModuleParent</artifactId>        <version>1.0-SNAPSHOT</version>    </parent>    <groupId>com.xueyou.mutilmoduleextension</groupId>    <artifactId>mutilmodule-extension</artifactId>    <version>1.0-SNAPSHOT</version>    <name>mutilmodule-extension</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>            <scope>test</scope>        </dependency>    </dependencies></project>

这时需要注意的是如何判断搭建的多模块maven项目成功了?通过右边的maven属性来确定。


这里能够清晰的看出模块和项目之间的关系。

11、打包问题。
对于多模块项目来说,只需要在父层次上进行打包操作,对应的子模块中就能够自动打包了。好下面看一下如何配置子模块的打包(其实与maven单个项目的打包配置相同)。

<?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.xueyou</groupId>        <artifactId>mutilModuleParent</artifactId>        <version>1.0-SNAPSHOT</version>    </parent>    <groupId>com.xueyou.mutilmodulecore</groupId>    <artifactId>mutilmodule-core</artifactId>    <version>1.0-SNAPSHOT</version>    <name>mutilmodule-core</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>            <scope>test</scope>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <configuration>                    <source>1.8</source>                    <target>1.8</target>                    <encoding>utf-8</encoding>                </configuration>            </plugin>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-shade-plugin</artifactId>                <version>3.0.0</version>                <executions>                    <execution>                        <phase>package</phase>                        <goals>                            <goal>shade</goal>                        </goals>                        <configuration>                            <transformers>                                <transformer                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">                                    <mainClass>com.xueyou.mutilmodulecore.App</mainClass>                                </transformer>                            </transformers>                        </configuration>                    </execution>                </executions>            </plugin>        </plugins>    </build></project>
<?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.xueyou</groupId>        <artifactId>mutilModuleParent</artifactId>        <version>1.0-SNAPSHOT</version>    </parent>    <groupId>com.xueyou.mutilmoduleextension</groupId>    <artifactId>mutilmodule-extension</artifactId>    <version>1.0-SNAPSHOT</version>    <name>mutilmodule-extension</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>            <scope>test</scope>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <configuration>                    <source>1.8</source>                    <target>1.8</target>                    <encoding>utf-8</encoding>                </configuration>            </plugin>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-shade-plugin</artifactId>                <version>3.0.0</version>                <executions>                    <execution>                        <phase>package</phase>                        <goals>                            <goal>shade</goal>                        </goals>                        <configuration>                            <transformers>                                <transformer                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">                                    <mainClass>com.xueyou.mutilmoduleextension.App</mainClass>                                </transformer>                            </transformers>                        </configuration>                    </execution>                </executions>            </plugin>        </plugins>    </build></project>


这样之后我们在最外层pom所在文件夹中执行命令:
mvn package.



分别执行jar包。




好,这样就能愉快的使用多模块的maven项目了。

后记:
如果需要上传私仓,那么需要在最外层的pom文件中添加如下内容:
<distributionManagement>        <repository>            <id>nexus</id>            <name>Releases</name>            <url>http://192.168.0.120:8081/repository/nexus-releases</url>        </repository>        <snapshotRepository>            <id>nexus</id>            <name>Snapshot</name>            <url>http://192.168.0.120:8081/repository/nexus-snapshots</url>        </snapshotRepository>    </distributionManagement>

这里的私仓地址需要写成你配置的私仓地址。