使用VSCode实现Java项目管理 Maven相关插件及配置(Maven换源)

文章目录

  • 前言
  • 插件
  • Maven配置
    • 一、maven的安装
    • 二、路径配置
    • 三、修改Maven依赖下载源
    • 四、修改库文件路径
    • 五、配置VSCode
  • Maven使用

前言

之前一直使用VSCode开发C、Go两种语言,现在需要对java进行学习,面对java的idea工具相对陌生,依旧想继续使用vscode作为开发工具,因此有了本篇文章

插件

无论是idea还是vscode,最核心的功能可能就是编辑了,很多工具的优秀特性也都是来自于优秀的插件,因此首先需要配置VSCode的插件

  • Language Support for Java™ by Red Hat
  • Debugger for Java
  • Java Test Runner
  • Maven for Java
  • Java Dependency Viewer
  • Spring Boot Tools
  • Spring Initializr Java Support
  • Spring Boot Dashboard
  • Tomcat
  • Jetty
  • CheckStyle
  • Java Linter
  • Java Decompiler
  • Lombok Annotations Support
  • Java Properties

    在这里插入图片描述

Maven配置

一、maven的安装

首先,先到官网去下载maven。这里是官网的地址:http://maven.apache.org/download.cgi 请选择最新的版本下载:

在这里插入图片描述

解压

在这里插入图片描述

二、路径配置

右键“计算机”,选择“属性”,之后点击“高级系统设置”,点击“环境变量”,来设置环境变量

在这里插入图片描述

有以下系统变量需要配置:

新建

变量名   MAVEN_HOME  

变量值:D:\Program Files\apache-maven-3.9.0

在这里插入图片描述

编辑系统变量 Path 添加变量值:

%MAVEN_HOME%\bin

在这里插入图片描述

最后检验配置是否成功:用win键+R,来打开命令行提示符窗口,即Dos界面,输入

mvn -version 

若出现以下情况说明配置成功

在这里插入图片描述

三、修改Maven依赖下载源

1、首先打开自己的maven安装目录,下面找到conf文件夹,打开settings.xml文件

在这里插入图片描述

2、直接复制下面的内容覆盖,切换为阿里源


    <!-- localRepository
     | The path to the local repository maven will use to store artifacts.
     |
     | Default: ${user.home}/.m2/repository
    /path/to/local/repo
    -->
    
        <!-- mirror
         | Specifies a repository mirror site to use instead of a given repository. The repository that
         | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
         | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
         |
        
          mirrorId
          repositoryId
          Human Readable Name for this Mirror.
          http://my.repository.com/repo/path
        
         -->
 
        
            alimaven
            aliyun maven
            http://maven.aliyun.com/nexus/content/groups/public/
            central
        
 
        
            uk
            central
            Human Readable Name for this Mirror.
            http://uk.maven.org/maven2/
        
 
        
            CN
            OSChina Central
            http://maven.oschina.net/content/groups/public/
            central
        
 
        
            nexus
            internal nexus repository
            <!-- http://192.168.1.100:8081/nexus/content/groups/public/-->
            http://repo.maven.apache.org/maven2
            central
        
 
    

四、修改库文件路径

maven作为非常强大的一个组织和管理工具,但是它的默认仓库放在C盘文档目录下,这样万一重装电脑会将下载的

jar包全部消除,而且永久以后库文件积累太多,容易造成电脑缓慢。对于项目来说重新部署虽然不是难事,但是我们

可以做到将仓库搬到另一个位置,这样就可以一劳永逸了。

使用ctrl+f在settings.xml寻找localrepository字样,出现如下所示,下面将这个标签改为你想要的路径:

在这里插入图片描述

例如

 D:/Program Files/apache-maven-repository/repository

完整settings.xml


    <!-- localRepository
     | The path to the local repository maven will use to store artifacts.
     |
     | Default: ${user.home}/.m2/repository
    /path/to/local/repo
    -->
    D:/Program Files/apache-maven-repository/repository
    
        <!-- mirror
         | Specifies a repository mirror site to use instead of a given repository. The repository that
         | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
         | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
         |
        
          mirrorId
          repositoryId
          Human Readable Name for this Mirror.
          http://my.repository.com/repo/path
        
         -->
 
        
            alimaven
            aliyun maven
            http://maven.aliyun.com/nexus/content/groups/public/
            central
        
 
        
            uk
            central
            Human Readable Name for this Mirror.
            http://uk.maven.org/maven2/
        
 
        
            CN
            OSChina Central
            http://maven.oschina.net/content/groups/public/
            central
        
 
        
            nexus
            internal nexus repository
            <!-- http://192.168.1.100:8081/nexus/content/groups/public/-->
            http://repo.maven.apache.org/maven2
            central
        
 
    

创建你在settings.xml中指定真实仓库路径,并将修改后的settings.xml文件复制一份放在当前的目录下(原来的不要删除掉)

在这里插入图片描述

五、配置VSCode

(Ctrl+,) ,搜索

java.configuration.maven

输入maven的settings.xml路径

注意:这里的路径都是之前新建的依赖下载位置的路径

在这里插入图片描述

搜索

maven.executable.path

在这里插入图片描述

注意:这里填写的路径是mvn可执行文件的绝对路径

上述过程也可以直接添加json代码到setting中

"java.configuration.maven.globalSettings": "D:\\Program Files\\apache-maven-repository\\settings.xml",
"java.configuration.maven.userSettings": "D:\\Program Files\\apache-maven-repository\\settings.xml",
"maven.executable.path": "D:\\Program Files\\apache-maven-3.9.0\\bin\\mvn",

Maven使用

经过我们的不屑努力,现在就可以通过vscode实现对maven的管理了

在这里插入图片描述

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/bfb864e088.html