在本教程中,我们将学习如何用简单的步骤使用命令行创建一个简单的Java多模块Maven项目
多模块项目由引用一个或多个子模块的父POM定义
在本教程中,我们将创建一个具有不同模块的博客web应用程序
让我们创建一个博客作为父项目,它的3个子模块是:
以下是创建父模块和子模块项目的步骤。
让我们使用以下命令创建父Maven项目:
mvn archetype:generate -DgroupId=com.companyname.blogger -DartifactId=blogger
注意,整个命令应该是一行。构建成功后,我们将在命令行控制台中看到以下输出。
[INFO] Parameter: package, Value: com.companyname.blogger
[INFO] Parameter: groupId, Value: com.companyname.blogger
[INFO] Parameter: artifactId, Value: blogger
[INFO] Parameter: packageName, Value: com.companyname.blogger
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\Ramesh_Study\maven\guides\blogger
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:09 min
[INFO] Finished at: 2018-06-20T13:40:40+05:30
[INFO] ------------------------------------------------------------------------
打开pom上面创建的Parent-Maven项目(blogger)的xml并将打包更改为“pom”。
<packaging>pom</packaging>
完整的pom.xml
<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.companyname.blogger</groupId>
<artifactId>blogger</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>blogger</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>
如果不更改pom.xml
,则会出现以下构建失败错误:
[INFO] Parameter: groupId, Value: com.companyname.blogger
[INFO] Parameter: artifactId, Value: blogger-core
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.companyname.blogger
[INFO] Parameter: packageInPathFormat, Value: com/companyname/blogger
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.companyname.blogger
[INFO] Parameter: groupId, Value: com.companyname.blogger
[INFO] Parameter: artifactId, Value: blogger-core
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:49 min
[INFO] Finished at: 2018-06-20T13:49:18+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:3.0.1:generate (default-cli) validPackaging: **Unable to add module to the current project as it is not of packaging type 'pom'** -> [Help 1]
通过以下命令
cd blogger
更改目录
让我们在blogger
父项目中创建子模块。
1.创建blogger-core
模块。
mvn archetype:generate -DgroupId=com.companyname.blogger -DartifactId=blogger-core
1.创建blogger-common
模块
mvn archetype:generate -DgroupId=com.companyname.blogger -DartifactId=blogger-common
1.创建blogger-web
模块,它是一个包含war
文件的web应用程序。maven web应用程序的原型是-DarchetypeArtifactId=maven-archetype-webapp
mvn archetype:generate -DgroupId=com.companyname.blogger -DartifactId=blogger-web -DarchetypeArtifactId=maven-archetype-webapp
现在,如果打开blogger父项目pom.xml
,您会发现其中添加了所有三个模块。
<modules>
<module>blogger-core</module>
<module>blogger-common</module>
<module>blogger-web</module>
</modules>
此外,在每个子模块pom.xml中添加父节点。
<parent>
<artifactId>blogger</artifactId>
<groupId>com.companyname.blogger</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
打开blogger-core
模块的pom.xml
,并用jar
:
<packaging>jar</packaging>
更新打包
打开blogger-common
模块的pom.xml
,并用jar
更新打包:
<packaging>jar</packaging>
打开blogger-web
模块的pom.xml
,并用war
更新打包:
<packaging>war</packaging>
让我们看看所有父模块和子模块的完整更新pom.xml
文件。
1.博客父项目pom.xml:
<?xmlversion="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.companyname.blogger</groupId>
<artifactId>blogger</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>blogger</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>
<modules>
<module>blogger-common</module>
<module>blogger-web</module>
<module>blogger-core</module>
</modules>
</project>
2.博客核心子模块pom.xml:
<?xmlversion="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>blogger</artifactId>
<groupId>com.companyname.blogger</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.companyname.blogger</groupId>
<artifactId>blogger-core</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>blogger-core</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3.blogger公共子模块pom.xml:
<?xmlversion="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>blogger</artifactId>
<groupId>com.companyname.blogger</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.companyname.blogger</groupId>
<artifactId>blogger-common</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>blogger-common</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
4.blogger web子模块pom.xml:
<?xmlversion="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.companyname.blogger</groupId>
<artifactId>blogger</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.companyname.blogger</groupId>
<artifactId>blogger-web</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>blogger-web 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>blogger-web</finalName>
</build>
</project>
要测试所有多模块项目设置是否正确,请在blogger父项目的maven-common下运行:
mvn clean install
上述命令的输出
[INFO] Reactor Summary:
[INFO]
[INFO] blogger 1.0-SNAPSHOT ............................... SUCCESS [ 0.746 s]
[INFO] blogger-common ..................................... SUCCESS [ 5.727 s]
[INFO] blogger-core ....................................... SUCCESS [ 1.775 s]
[INFO] blogger-web Maven Webapp 1.0-SNAPSHOT .............. SUCCESS [ 0.809 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.289 s
[INFO] Finished at: 2018-06-20T14:20:05+05:30
[INFO] ------------------------------------------------------------------------
就是这样
现在可以将此项目导入Eclipse IDE在Eclipse中,文件->导入->Maven->现有Maven项目
在本教程中,我们以blogger
项目为例,学习了如何使用命令行创建maven Multi-Module
。
这个多模块项目的源代码可以在GitHub上找到。
Github存储库:Maven Multi Module Project。
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.javaguides.net/2018/06/how-to-create-maven-multi-module-using-command-line.html
内容来源于网络,如有侵权,请联系作者删除!