IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

Maven学习总结(八)——使用Maven构建多模块项目

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

  在平时的Javaweb项目开发中为了便于后期的维护,我们一般会进行分层开发,最常见的就是分为domain(域模型层)、dao(数据库访问层)、service(业务逻辑层)、web(表现层),这样分层之后,各个层之间的职责会比较明确,后期维护起来也相对比较容易,今天我们就是使用Maven来构建以上的各个层。

  项目结构如下:

  system-parent
    |—-pom.xml
    |—-system-domain
        |—-pom.xml
    |—-system-dao
        |—-pom.xml
    |—-system-service
        |—-pom.xml
    |—-system-web
        |—-pom.xml

一、创建system-parent项目

  创建system-parent,用来给各个子模块继承。

  进入命令行,输入以下命令:

mvn archetype:create -DgroupId=me.gacl -DartifactId=system-parent -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  如下图所示:

  77_1.png

  命令执行完成之后可以看到在当前目录(C:\Documents and Settings\Administrator)生成了system-parent目录,里面有一个src目录和一个pom.xml文件,如下图所示:

  77_2.png

  将src文件夹删除,然后修改pom.xml文件,将jar修改为pom,pom表示它是一个被继承的模块,修改后的内容如下:

 <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>me.gacl</groupId>
   <artifactId>system-parent</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>pom</packaging>

   <name>system-parent</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>

二、创建sytem-domain模块

  在命令行进入创建好的system-parent目录,然后执行下列命令:

mvn archetype:create -DgroupId=me.gacl -DartifactId=system-domain -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  如下图所示:

  77_3.png

  命令执行完成之后可以看到在system-parent目录中生成了system-domain,里面包含src目录和pom.xml文件。如下图所示:

  77_4.png

  77_5.png

  同时,在system-parent目录中的pom.xml文件自动添加了如下内容:

<modules>
    <module>system-domain</module>
</modules>

  这时,system-parent的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>me.gacl</groupId>
   <artifactId>system-parent</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>pom</packaging>

   <name>system-parent</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>system-domain</module>
   </modules>
 </project>

  修改system-domain目录中的pom.xml文件,把me.gacl1.0-SNAPSHOT去掉,加上jar,因为groupId和version会继承system-parent中的groupId和version,packaging设置打包方式为jar

  修改过后的pom.xml文件如下:

 <?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>me.gacl</groupId>
     <artifactId>system-parent</artifactId>
     <version>1.0-SNAPSHOT</version>
   </parent>

   <artifactId>system-domain</artifactId>
   <packaging>jar</packaging>

   <name>system-domain</name>
   <url>http://maven.apache.org</url>
 </project>

三、创建sytem-dao模块

  在命令行进入创建好的system-parent目录,然后执行下列命令:

mvn archetype:create -DgroupId=me.gacl -DartifactId=system-dao -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  如下图所示:

  77_6.png

  命令执行完成之后可以看到在system-parent目录中生成了system-dao,里面包含src目录和pom.xml文件。如下图所示:

  77_7.png77_8.png

  同时,在system-parent目录中的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>me.gacl</groupId>
   <artifactId>system-parent</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>pom</packaging>

   <name>system-parent</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>system-domain</module>
     <module>system-dao</module>
   </modules>
 </project>

  修改system-dao目录中的pom.xml文件,,把me.gacl1.0-SNAPSHOT去掉,加上jar,因为groupId和version会继承system-parent中的groupId和version,packaging设置打包方式为jar,同时****添加对system-domain模块的依赖,修改后的内容如下:

 <?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>me.gacl</groupId>
     <artifactId>system-parent</artifactId>
     <version>1.0-SNAPSHOT</version>
   </parent>

   <artifactId>system-dao</artifactId>
   <packaging>jar</packaging>

   <name>system-dao</name>
   <url>http://maven.apache.org</url>
   <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   <dependencies>
     <!--system-dao需要使用到system-domain中的类,所以需要添加对system-domain模块的依赖-->
21      <dependency>
       <groupId>me.gacl</groupId>
       <artifactId>system-domain</artifactId>
       <version>${project.version}</version>
     </dependency>
   </dependencies>
 </project>

四、创建system-service模块

  在命令行进入创建好的system-parent目录,然后执行下列命令:

mvn archetype:create -DgroupId=me.gacl -DartifactId=system-service -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  如下图所示:

  77_9.png

  命令执行完成之后可以看到在system-parent目录中生成了system-service,里面包含src目录和pom.xml文件。如下图所示:

  77_10.png77_11.png

  同时,在system-parent目录中的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>me.gacl</groupId>
   <artifactId>system-parent</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>pom</packaging>

   <name>system-parent</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>system-domain</module>
     <module>system-dao</module>
     <module>system-service</module>
   </modules>
 </project>

  修改system-service目录中的pom.xml文件,,把me.gacl1.0-SNAPSHOT去掉,加上jar,因为groupId和version会继承system-parent中的groupId和version,packaging设置打包方式为jar,同时****添加对system-dao模块的依赖,system-service依赖system-dao和system-domain,但是我们只需添加system-dao的依赖即可,因为system-dao已经依赖了system-domain。修改后的内容如下:

 <?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>me.gacl</groupId>
     <artifactId>system-parent</artifactId>
     <version>1.0-SNAPSHOT</version>
   </parent>

   <artifactId>system-service</artifactId>
   <packaging>jar</packaging>

   <name>system-service</name>
   <url>http://maven.apache.org</url>
   <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   <dependencies>
     <!--
     system-service依赖system-dao和system-domain,
     但是我们只需添加system-dao的依赖即可,因为system-dao已经依赖了system-domain
     -->
      <dependency>
       <groupId>me.gacl</groupId>
       <artifactId>system-dao</artifactId>
       <version>${project.version}</version>
     </dependency>
   </dependencies>
 </project>

五、创建system-web模块

  在命令行进入创建好的system-parent目录,然后执行下列命令:

mvn archetype:create -DgroupId=me.gacl -DartifactId=system-web -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

  如下图所示:

  77_12.png

  命令执行完成之后可以看到在system-parent目录中生成了system-web,里面包含src目录和pom.xml文件。如下图所示:

  77_13.png77_14.png

  在\system-web\src\main\webapp目录中还生成了一个简单的index.jsp,如下图所示:

  77_15.png

  里面的内容为

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

  system-web\src\main\webapp\WEB-INF目录中生成了web.xml

  77_16.png

  同时,在system-parent目录中的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>me.gacl</groupId>
   <artifactId>system-parent</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>pom</packaging>

   <name>system-parent</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>system-domain</module>
     <module>system-dao</module>
     <module>system-service</module>
     <module>system-web</module>
   </modules>
 </project>

  修改system-web目录中的pom.xml文件,,把me.gacl1.0-SNAPSHOT去掉,因为groupId和version会继承system-parent中的groupId和version,同时****添加对system-service模块的依赖,修改后的内容如下:

 <?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>me.gacl</groupId>
     <artifactId>system-parent</artifactId>
     <version>1.0-SNAPSHOT</version>
   </parent>

   <artifactId>system-web</artifactId>
   <packaging>war</packaging>

   <name>system-web Maven Webapp</name>
   <url>http://maven.apache.org</url>
   <dependencies>
     <!--
     system-web依赖system-service
     -->
      <dependency>
       <groupId>me.gacl</groupId>
       <artifactId>system-service</artifactId>
       <version>${project.version}</version>
     </dependency>
   </dependencies>
   <build>
     <finalName>system-web</finalName>
   </build>
 </project>

   注意,web项目的打包方式是war

六、编译运行项目

  经过上面的五个步骤,相关的模块全部创建完成,怎么运行起来呢。由于最终运行的是system-web模块,所以我们对该模块添加jetty支持,方便测试运行。修改system-web项目的pom.xml如下:

 <?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>me.gacl</groupId>
     <artifactId>system-parent</artifactId>
     <version>1.0-SNAPSHOT</version>
   </parent>

   <artifactId>system-web</artifactId>
   <packaging>war</packaging>

   <name>system-web Maven Webapp</name>
   <url>http://maven.apache.org</url>
   <dependencies>
     <!--
     system-web依赖system-service
     -->
      <dependency>
       <groupId>me.gacl</groupId>
       <artifactId>system-service</artifactId>
       <version>${project.version}</version>
     </dependency>
   </dependencies>
   <build>
     <finalName>system-web</finalName>
     <plugins>
         <!--配置Jetty插件-->
         <plugin>
             <groupId>org.mortbay.jetty</groupId>
             <artifactId>maven-jetty-plugin</artifactId>
         </plugin>
     </plugins>
   </build>
 </project>

  在命令行进入system-parent目录,然后执行下列命令:

mvn clean install

  如下图所示:

  77_17.png

  77_18.png

  命令执行完后,在system-web目录下多出了target目录,里面有了system-web.war,如下图所示:

  77_19.png

  命令行进入sytem-web目录,执行如下命令,启动jetty

mvn jetty:run

  如下图所示:

  77_20.png

  77_21.png

  启动jetty服务器后,访问http://localhost:8080/system-web/ 运行结果如下图所示:

  77_22.png

七、导入Eclipse中进行开发

  操作步骤如下所示:

  77_23.png

  77_24.png

  77_25.png

  77_26.png

  77_27.png

来源:http://dwz.date/2WH

文章永久链接:https://tech.souyunku.com/?p=15864


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(92) 打赏



未经允许不得转载:搜云库技术团队 » Maven学习总结(八)——使用Maven构建多模块项目

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367