• 这次弄一下maven 多模块项目,用vscode新建一下,便于管理项目


    首先 创建一个mvn项目, 直接在命令行执行, 原型生成:

    mvn archetype:generate

    选一个maven quick start的template, 然后删除src和target文件夹

    在pom.xml里面version 下面加上pom (maven 的三种打包方式: pom为打包依赖方式, jar文件,或者 war文件)

    在此目录中再次执行mvn archetype:generate, 构件artifactId选为child1, 完成后自动在mvnparent目录的Pom中添加了 节点

    再次在mvnparent目录执行mvn archetype:generate 生成child2项目, 会在mvnparent的pom.xml中添加child2的module, 把里面没用的build 节点都删掉

    复制代码
    "1.0" encoding="UTF-8"?>
    "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">
      4.0.0
      
        mvnparent
        org.caloch
        1.0-SNAPSHOT
      
    
      child2
      jar
    
      child2
      
      http://www.example.com
    
      
        UTF-8
        1.7
        1.7
      
    
    
    
      
    
    复制代码

    mvnchild1引用了child2项目的pom.xml, 里面删除groupid和version两个,它们会继承parent, packaging改为jar

    复制代码
    "1.0" encoding="UTF-8"?>
    "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">
      4.0.0
      
        mvnparent
        org.caloch
        1.0-SNAPSHOT
      
    
      mvnchild1
      jar
    
      mvnchild1
      
      http://www.example.com
    
      
        UTF-8
        1.7
        1.7
      
    
      
        
          junit
          junit
          4.11
          test
        
    
        
          org.caloch
          child2
          ${project.version}
        
    
    
      
    
      
    
    复制代码

     

     

    复制代码
    "1.0" encoding="UTF-8" standalone="no"?>
    "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">
      4.0.0
    
      org.caloch
      mvnparent
      1.0-SNAPSHOT
      pom
    
      mvnparent
      
      http://www.example.com
      
    
      
        UTF-8
        1.7
        1.7
      
    
      
        
          junit
          junit
          4.11
          test
        
      
    
      mvnchild1
        child2
      
    
    复制代码

     将项目导入idea中可以调试, 运行为 java -cp "加上另外项目的class文件路径"

     D:\source\repos\mvn_multi_module\mvnparent\mvnchild1> java -cp "D:\source\repos\mvn_multi_module\mvnparent\mvnchild1\target\classes;D:\source\repos\mvn_multi_module\mvnparent\child2\target\classes;" child1.App

     mvn compile

    mvn package

    需要指定main class

    指定时 java -jar xx.jar可以执行

    不指定main class时, 使用java -cp xx.jar {maiclass} 来执行

    构建, 在child1的pom里面添加build

    复制代码
      <build>
        <plugins>
    
            
            <plugin>
                <artifactId>maven-assembly-pluginartifactId>
                <configuration>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
    
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependenciesdescriptorRef>
                    descriptorRefs>
                configuration>
                <executions>
                    <execution>
                        <id>make-assemblyid> 
                        <phase>packagephase> 
                        <goals>
                            <goal>singlegoal>
                        goals>
                    execution>
                executions>
            plugin>
    
           
    
    
        plugins>
    build>
    复制代码

     

    运行,注意要运行和dependency一起打包的jar才可以不需要额外添加另外一个包的class path, 否则还是要加class path -cp选项:

    cd D:\source\repos\mvn_multi_module\mvnparent\mvnchild1\target
    
    java -cp .\mvnchild1-1.0-SNAPSHOT-jar-with-dependencies.jar child1.App
    Hello util in child2

     关于引用两个jar,里面的包名和类名都相同的问题: 按搜索,只需要改变引用的顺序就会造成后一个覆盖前一个的结果

    建好的项目文件在:

    https://files.cnblogs.com/files/hualiu0/mvnparent.7z?t=1698752418&download=true

  • 相关阅读:
    OpenCV C++ 图像处理实战 ——《OCR字符识别》
    APP 备案公钥、签名 MD5、SHA-1、SHA-256获取方法。
    vue的学习笔记(1):v-on的使用
    Flink 内存模型
    在数据分析中,对缺失值解决方案的分析
    【FPGA零基础学习之旅#13】串口发送模块设计与验证
    Ubuntu 20.04 安装NVIDIA显卡驱动+cuda 11.7+cudnn
    mission planner通过串口连接3DR数传,远程飞控
    云服务器租用价格表概览_阿里云腾讯云华为云
    01-基于IDEA,Spring官网,阿里云官网,手动四种方式创建SpringBoot工程
  • 原文地址:https://www.cnblogs.com/hualiu0/p/17801039.html