【maven】maven聚合工程如何打包 解决maven pom.xml各种报错问题 规范pom文件

导入别人项目或者自己搭建项目时,每次必不可少都会遇到maven报错问题 (当然有可能是别人的项目pom写的不规范 自信点可能不是你的问题 但我们需要学会解决)。 这些问题 百度都不知道怎么查,最近着手自己搭建了一个springcloud聚合工程的demo 应该算是比较有代表性了,当然 springboot也可差不多 主要是理解maven的使用, 终于把这些给理解了个大概。文章有点长 原创不易

项目结构介绍: MySpringCloud为父模块,本文以红框内的模块举例, file为业务模块 调用了user-api工具模块,user-api引入了framework工具模块,两个工具模块被common模块管理
【maven】maven聚合工程如何打包 解决maven pom.xml各种报错问题 规范pom文件_第1张图片

本文从多个层面分析maven综合报错问题:

  1. 首先一个比较低级的错误 一般有了开发经验不会犯 但是入门者可能会踩坑 要确保maven配置里面的maven仓库地址是可用的,典型错误:早期maven中央仓库是http传输的,后来改成了https, 网上可能还存在很多错误的mirrors配置 换句话说 仓库地址都是错误的 所以会导致所有依赖下载失败,此外 不同的仓库地址 某些依赖的groupId有可能会不同

【maven】maven聚合工程如何打包 解决maven pom.xml各种报错问题 规范pom文件_第2张图片

  1. 确保了地址是可用的 groupId 和 artifactId都是无误:

    maven遇到的主要错误 应该属以下几个

    .A  Cannot resolve xxxx 错误 
    .B  Could not find artifact 错误
    .C  must be "pom" but is "jar"错误
    .D  程序包XXX 不存在
    .E Unable to find main class
    

解决方式:

  1. 低级错误1:找到本地maven仓库路径 查找 .lastUpdated 结尾的文件 全部删除 ,出现这种文件的情况可能由于网络不好,下载错误 并且它会阻止下次下载;
  2. 低级错误2:如果网络正常 且每次下载都是这个文件,那么很可能就是依赖版本号不正确,远程仓库根本没有这个包,所以一直都是 lastUpdated 。
    【maven】maven聚合工程如何打包 解决maven pom.xml各种报错问题 规范pom文件_第3张图片
  3. 正文开始 用一个父子多模块maven项目 pom.xml文件实际举例 重点都在注释上面,顺便写了个英文版注释,因为xml比较容易乱码,项目几经周转很可能注释就乱了,这些注释就不复制出来了 研究这玩意 我累了。

父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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.3.4.RELEASEversion>
        <relativePath/> 
    parent>
    
    <packaging>pompackaging>
    <groupId>com.demogroupId>
    <artifactId>MySpringCloudartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>MySpringCloudname>
    <description>Demo project for Spring Bootdescription>

    
    <properties>
        <java.version>1.8java.version>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>

        <spring-cloud.version>Hoxton.SR8spring-cloud.version>
        <spring-cloud-alibaba.version>2.2.1.RELEASEspring-cloud-alibaba.version>

        <nacos.version>1.3.0nacos.version>
        <sentinel.version>1.8.2sentinel.version>
        <openfeign.version>2.2.5.RELEASEopenfeign.version>
        <netflix-hystrix.version>2.2.5.RELEASEnetflix-hystrix.version>

        <fastjson.version>1.2.76fastjson.version>
        <servlet.version>4.0.1servlet.version>
    properties>

    
    
    

    
    
    <dependencyManagement>

        <dependencies>
           
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                
                
                <type>pomtype>
            dependency>

            
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                <version>${spring-cloud-alibaba.version}version>
                <type>pomtype>
                
                
                
                
                <scope>importscope>
            dependency>



            
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <optional>trueoptional>
                <version>${lombok.version}version>
            dependency>

            
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>fastjsonartifactId>
                <version>${fastjson.version}version>
            dependency>

            
            <dependency>
                <groupId>com.alibaba.nacosgroupId>
                <artifactId>nacos-clientartifactId>
                <version>${nacos.version}version>
            dependency>


            
            <dependency>
                <groupId>com.alibaba.cspgroupId>
                <artifactId>sentinel-coreartifactId>
                <version>${sentinel.version}version>
            dependency>

            
            <dependency>
                <groupId>com.alibaba.cspgroupId>
                <artifactId>sentinel-transport-simple-httpartifactId>
                <version>${sentinel.version}version>
            dependency>

            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-openfeignartifactId>
                <version>${openfeign.version}version>
            dependency>

            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
                <version>${netflix-hystrix.version}version>
            dependency>

            
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>javax.servlet-apiartifactId>
                <version>${servlet.version}version>
            dependency>
            
           
            <dependency>
                <groupId>org.apache.skywalkinggroupId>
                <artifactId>apm-toolkit-traceartifactId>
                
                <version>${skywalking.version}version>
            dependency>

            
            <dependency>
                <groupId>org.apache.skywalkinggroupId>
                <artifactId>apm-toolkit-logback-1.xartifactId>
                <version>${skywalking.version}version>
            dependency>

        dependencies>
    dependencyManagement>

    
    <modules>
        <module>commonmodule>
        <module>usermodule>
        <module>filemodule>
    modules>

	 


















project>


子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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>com.demogroupId>
        <artifactId>MySpringCloudartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>

    
    <packaging>jarpackaging>
    <groupId>com.qkjgroupId>
    <artifactId>fileartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>filename>
    <description>Demo project for Spring Bootdescription>
    <properties>
        <java.version>1.8java.version>
    properties>
    <dependencies>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-dependenciesartifactId>
            
            
            
            
            <version>${spring-cloud.version}version>
            <type>pomtype>
        dependency>

        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-alibaba-dependenciesartifactId>
            <version>${spring-cloud-alibaba.version}version>
            <type>pomtype>
        dependency>

        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>${fastjson.version}version>
        dependency>

        
        <dependency>
            <groupId>com.alibaba.nacosgroupId>
            <artifactId>nacos-clientartifactId>
        dependency>

        
        <dependency>
            <groupId>com.alibaba.cspgroupId>
            <artifactId>sentinel-coreartifactId>
        dependency>

        
        <dependency>
            <groupId>com.alibaba.cspgroupId>
            <artifactId>sentinel-transport-simple-httpartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
        dependency>


        
        
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>

        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
        dependency>

        
        <dependency>
            <groupId>org.apache.skywalkinggroupId>
            <artifactId>apm-toolkit-traceartifactId>
        dependency>

        
        <dependency>
            <groupId>org.apache.skywalkinggroupId>
            <artifactId>apm-toolkit-logback-1.xartifactId>
        dependency>
        <dependency>
            <groupId>com.qkjgroupId>
            <artifactId>user-apiartifactId>
            <version>0.0.1-SNAPSHOTversion>
            
            
            <scope>compilescope>
        dependency>


    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombokgroupId>
                            <artifactId>lombokartifactId>
                        exclude>
                    excludes>
                configuration>
                <version>2.3.4.RELEASEversion>
            plugin>
        plugins>
    build>

project>




工具类父pom :common.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>com.demogroupId>
        <artifactId>MySpringCloudartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>

    <packaging>pompackaging>

    <groupId>com.qkjgroupId>
    <artifactId>commonartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>commonname>
    <description>Demo project for Spring Bootdescription>
    <properties>
        <java.version>1.8java.version>
    properties>

    <modules>
        <module>file-apimodule>
        <module>user-apimodule>
        <module>frameworkmodule>
    modules>


    
project>

工具类子pom : user-api.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>com.qkjgroupId>
        <artifactId>commonartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>

    <packaging>jarpackaging>
    <artifactId>user-apiartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>user-apiname>

    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>


        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>

        <dependency>
            <groupId>com.qkjgroupId>
            <artifactId>frameworkartifactId>
            <version>0.0.1-SNAPSHOTversion>
            
            
            <scope>compilescope>

        dependency>

        
    dependencies>



project>

工具类子pom: framework.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.qkj</groupId>
        <artifactId>common</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <packaging>jar</packaging>
    <artifactId>framework</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>framework</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--feign 依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!-- servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>

    </dependencies>


</project>

什么时候可以不写version?

父pom dependencyManagement 里面声明了version, 而子pom继承了 父pom(parent 节点声明继承关系) ,子pom依赖不声明版本,则使用父pom依赖版本,这在多子模块项目中方便管理。 而整个pom不存在parent时,version是一定要写的,如果不写则会报错

type: 默认为jar 因为我们的是聚合工程,所以父模块要声明为pom,否则会报must be “pom” but is "jar"错误

总结:

  1. maven仓库地址有误或下载缓慢 网络不好 (推荐使用阿里仓库)
  2. 版本号错误 或缺少版本号
  3. 多模块项目中未正确声明父模块groupId artifactId
  4. pom工程中 未正确声明type
  5. 父模块中声明了maven打包插件 而子模块含有工具类模块

你可能感兴趣的:(spring,maven,maven,spring)