Java教程

[Java-01] Mac-Maven-VSCode-配置&编译

本文主要是介绍[Java-01] Mac-Maven-VSCode-配置&编译,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Mac-Maven-VSCode-配置&编译

1: mac 安装java 1.8

参考链接:https://www.jianshu.com/p/5b5dcbf3f7d8

  • 下载jdk
    http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

  • .bash_profile 配置环境变量

编辑java全局路径
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
  • 打开终端、测试
#  java -version
bmwdeMacBook-Pro:~ bmw$ java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
bmwdeMacBook-Pro:~ bmw$ 

2. 下载maven

参考链接:https://blog.csdn.net/weixin_43974907/article/details/107916386

  • apache 官网
    https://maven.apache.org/download.cgi

  • 配置Maven配置环境

  • 将下载的apache-maven-3.8.2 解压在mac 目录下
# /Users/bmw/apache-maven-3.8.2 为解压目录路径PATH
export MAVEN_HOME=/Users/bmw/apache-maven-3.8.2
export PATH=$MAVEN_HOME/bin:$PATH
  • 打开终端,验证maven
# mvn -v
bmwdeMacBook-Pro:~ bmw$ mvn -v
Apache Maven 3.8.2 (ea98e05a04480131370aa0c110b8c54cf726c06f)
Maven home: /Users/bmw/apache-maven-3.8.2
Java version: 1.8.0_181, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.16", arch: "x86_64", family: "mac"

3. 配置VSCode

参考链接: http://www.littleblog.cn/javaee/7089.html

  • 下载plugin
    • Maven for Java
    • Java Extension Pack
    • Spring Boot Extension Pack
    • Language Support for Java(TM) by Red Hat
# 下载完基本上这些插件都会存在.
Language Support for Java(TM) by Red Hat
Debugger for Java
Java Test Runner
Maven for Java
  • 配置vscode 中 java_home 地址
#  步骤
1:打开设置(command + ,),搜索maven
2: 选择 Extension -> Maven for Java -> Edit in setting.json
3: 填写 "java.home": "/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home"

# mac java.home 地址获取:
bmwdeMacBook-Pro:~ bmw$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home

4. 首次使用VSCode创建第一个java项目

  • 1: cmd+shift+p 输入maven
  • 2: 选择Maven: create maven project
  • 3: 选择maven-qrchetype-quickstart快速创建
  • 4: 创建过程中需要输入groupid, artifactid, version, package
groupid 一般分为多个段,这里我只说两段,第一段为域,第二段为公司名称, 虽然产生的项目结构文件与groupId无关,但是大家公认的规范是填写项目包名,如com.xx.testArchetypeGenerate(公司网址反写+项目名)
artifactid 填写你负责的模块名,如项目名-模块名
version 版本号
package 填写你创建的类所在的包名

例如:
groupid: com.bmw.createjavademo01
artifactid: createjavademo01-login
version: 1.0.0
package: 默认回车
  • 5: 创建完成

5: 开始build java 项目

  • 1: 选择launch.json 配置
// 如:
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "name": "Debug(Launch) - Current File",
            "request": "launch",
            "mainClass": "${file}"
        },
        {
            "type": "java",
            "name": "Debug(Launch)-App<target>",
            "request": "launch",
            "mainClass": "target.App",
            "projectName": "target"
        }
    ]
}
  • 2: build
    • 按F5或点击run试试看
      如:
      bmwdeMacBook-Pro:createjavademo01-login bmw$ cd /Users/bmw/Desktop/java-demo/createjavademo01-login ; /usr/bin/env /Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/bin/java -agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:57198 -Dfile.encoding=UTF-8 -cp /Users/bmw/Desktop/java-demo/createjavademo01-login/target/classes com.bmw.createjavademo01.App
      Hello World!
这篇关于[Java-01] Mac-Maven-VSCode-配置&编译的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!