maven添加公共jar和第三方jar包,以及杂题

本文详细介绍Maven的基础配置及使用流程,包括如何通过Maven管理项目依赖、安装第三方jar包、解决常见错误等实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 简介:

以前去面试的时候,经常会问,会使用maven么?以前总觉得maven是个很麻烦的东西,各种配置,看都看不懂;

还不如直接下载jar 拷贝进去;为什么还要去配置maven费那个劲?

自从自己使用maven管理项目之后,才发现,maven简直太强大了,保证你用过一次,你就再也不想用下载,拷贝jar包的方法了。

步骤:

1.下载maven安装

2.配置maven环境,和配置jdk差不多,自行百度

3.在开发工具里面创建maven项目,方法自行百度

4.pom.xml是maven的配置文件,所有配置都在里面,具体配置自行百度

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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.yuan</groupId>
  <artifactId>YuanxmMavenExample</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>YuanxmMavenExample 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>
   

   ------------------------------------------------- start

    <!-- spring jar begin-->
    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.7.RELEASE</version>
    </dependency>
    <!-- spring jar end-->
    ------------------------------------------------- end  你只需要添加以上配置,保存之后,maven就自动帮你下载4.3.7版本的spring , jar包了。这个配置信息可以上spring的官网查询
  

--------------------------------------------- start

 <!-- install-plugin jar begin-->
    <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.3.1</version>
    </dependency>
    <!-- install-plugin jar end-->
----------------------------------------------    end 以上配置,是为了添加maven的插件,因为大多数jar,只要有配置信息,maven就可以帮你下载,但是不排除没有配置信息的,所以要自己导入,这个插件用来导入第三方jar包

maven 插件版本查询地址:  http://mvnrepository.com/artifact/org.apache.maven.plugins

导入第三方jar包例子:  

第一步 配置maven环境之后在dos下:

mvn install:install-file -DgroupId=org.jotm(自己根据jar的意义名)  -DartifactId=j-jotm(自己根据意义命名) -Dversion=1.6.8 -Dpackaging=jar -Dfile=D:\springframeworkjar\jotm.jar(本地jar包位置)


一些参数说明如下:
-DgroupId=远程仓库对应的DgroupId
-DartifactId= 远程仓库对应的 DartifactId
-Dversion=对应版本号

第二部 在pom.xml里面加入配置maven就会导入(注意,第一步只是把jar放入了maven,这里才是把jar导入项目)

  <!-- org.jotm jar begin-->
    <dependency>
            <groupId>org.jotm</groupId>
            <artifactId>j-jotm</artifactId>
            <version>1.6.8</version>
    </dependency>
    <!-- org.jotm jar end-->
    
  <build>

完毕!

maven 仓库地址:http://mvnrepository.com 查询 依赖配置

maven的pom.xml结构:

Maven:repositories、distributionManagement、pluginRepositories中repository的区别

repositories:jar下载的仓库地址

distributionManagement:打包发布的仓库地址

pluginRepositories:插件下载的仓库地址

参考:

Maven:repositories、distributionManagement、pluginRepositories中repository的区别_netyeaxi的专栏-优快云博客_pluginrepository

====================================================

maven命令:

package :打包
install:打包、部署到本地maven仓库
deploy:打包、部署到本地maven仓库,并且推到nexus私服上

====================================================

今天新下载了一个项目,用maven编译,出现:

1.Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved...

2.Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test(default-test) on...

...

各种清理项目,各种删除出错的jar包,用maven重新update, c盘的.m2文件夹删除了3次,重新下载,还去网上的maven仓库手动下载jar来替换本地报错的jar,最后还是各种包报错。

终极解决方案:把.m2全删除,拷贝同事中能启动这个项目的.m2文件夹去替换,最后解决。

====================================================

maven一个子模块打个jar上传到nexus中,pom文件中添加对应的依赖,无法下载下来

maven报错:Could not find artifact ... in... (但是这个包明明又在nexus中)

最后解决:因为这个子模块引用了其它父模块,把项目中的其它模块一起打包deploy到

nexus中就可以了

maven install 报:Could not find artifact ...

参考:Maven异常:Could not find artifact - devilwind - 博客园

也是因为没有父依赖,先install父依赖

====================================================

maven报错:

Could not find artifact ...:pom:1.0-SNAPSHOT in snapshots

Could not find artifact ...:pom:1.0-SNAPSHOT in snapshots_SJZYLC的专栏-优快云博客

====================================================

IDEA出现Error:(4, 11) java: 程序包XXX不存在解决方法

Error:(4, 25) java: 找不到符号符号:类 xxx位置:程序包 xxx.xxx不存在。。。

  解决方案1:

解决方案2:

但是本地又可以找到这个包,此时就可以找到对应的包的位置,然后对应的java模块,用maven进行清理,再install一下。

解决方案3:

idea工具窗口点 - maven工具 - 在有个M字里点击出来 - 运行mvn idea:idea,会生成.ipr和.iws文件,2020版IDEA才有的问题

参考:

IDEA 2020报“java:程序包XXXX不存在”或“java:找不到符号”_changruhe的博客-优快云博客

==============================================================

Error running ‘xxx项目’: Command line is too long. Shorten command line for xxx or also for Spring Boot default configuration.

解决:

在IDEA中找到 Run-> Edit Configurations打开,

在 Environment-> Shorten command line 的内容配置为 JAR即可解决

参考:

【已解决】Error running 'xxx项目' Command line is too long(idea版) - 星朝 - 博客园

===============================================================

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project ...

maven工具点开,有个类似于⚡的图片,点击,也就是跳过测试,再install

===============================================================

idea 项目启动,提示找不到类,找不到符号,找不到包,全部各种install,清理缓存,

删除本地仓库的包,删除本地的项目,重新拉项目

===============================================================

process terminated 问题

网上的答案千篇一律,每个人问题不一样,你可以看控制台上如果有标红的异常,

那么你点进去看具体的错误日志,日志会告诉你一切!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值