macOS && IntelliJ IDEA- Maven integration and its application

macOS && IntelliJ IDEA- Maven integration and its application

Maven basic application

Maven Introduction

Apache Maven build tool is open source Java-based development projects, mainly for projects to compile, test, package, deploy, and other tasks.
Apache Maven

Many mainstream open source Java framework (SpringBoot, Spring Cloud Alibaba, etc.) all have Maven based build, if you have a pom.xml you browse the root directory of open source projects, says the project it is based on Maven build.
Spring Boot
Spring-Cloud-Alibaba
Spring-Data-JPA

Maven installation

First, go to the official website to download Maven Maven installation package now (2019/12/21) is the latest version of Maven 3.6.3, recommend apache-maven-3.6.3-bin.zip, and then extract it to the specified directory (/ the Users / liuguanglei / Documents / Tools / Apache-Maven-3.6.3 ) to. Because Maven is a Java-based development, before using Maven must install and configure Java JDK environment, if you do not know how to install and configure Java, you can search for headlines in "Java language core technology -macOS download, install, configure JDK11.0.5" Learn Java installation and configuration process.
Maven's official website


apache-maven-3.6.3-bin.zip

Official website requires Maven3.6.3 must be more than JDK1.7 version, the current mainstream version of Java is Java1.8, Java version if you want to see the current system, you can use the command java -versionto view.
java -version

After installation is complete Maven PATH environment variable needs to be increased in the system configuration file (/ etc / profile) of.
Its configuration and Java configuration similar to the PATH environment variable.

export MAVEN_HOME=/Users/liuguanglei/Documents/tools/apache-maven-3.6.3
export PATH=$PATH:$MAVEN_HOME/bin

Once configured only need to enter mvn -v in a terminal to verify that the installation was successful if you can export the operating system, Java and Maven version information to prove installed and configured properly.
mvn -v

Maven配置

由于Maven是国外的Apache基金会开发的,因此还需要做一些本地化的配置。
首先需要进入MAVEN_HOME\config目录,打开setting.xml文件
setting.xml

首先需要定制本地仓库的路径,默认的配置是 ${user.home}/.m2/repository,这里改为
/Users/liuguanglei/Documents/tools/maven_repository。

在settings.xml文件中添加localRepository配置即可

  <localRepository>/Users/liuguanglei/Documents/tools/maven_repository</localRepository>

后续基于Maven构建的项目所依赖的第三方jar包都会下载到该目录。

然后添加阿里云镜像配置,由于Maven默认的私服镜像在国外,国内的网络环境可能有时候下载不了项目中所依赖的第三方组件。这里引入阿里云镜像配置,可以加速第三方组件的下载,

在settings.xml文件的<mirrors></mirrors>节点配置内添加即可。

 <mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>        
    </mirror>

IntelliJ IDEA集成Maven

Maven的构建任务主要是基于命令行进行,而在日常开发中通常都是将Maven集成在IntelliJ IDEA中使用,那样点点鼠标就可以实现Maven构建项目,更加容易和方便。只有将项目部署在服务器(通常是Linux操作系统)后才会使用到Maven的命令去构建项目。

在启动IntelliJ IDEA后,找到右下角的Configure->Preferences,然后在搜索栏中输入Maven,在右边的Maven设置中配置Maven home directory,User settings file和Local repository。
Maven configuration

IntelliJ IDEA创建项目

IntelliJ IDEA 创建基于Maven的 java-core项目

基于Maven多模块的项目

Create New Project
Create new project
New Project

选择基于Maven,JDK11创建项目

New Project
New Project

New Project

设置项目名称以及项目路径和Maven坐标

New Project
New Project

在首次使用IntelliJ IDEA时,会弹出Tip of the Day,这里去掉该选项。
Tip of the Day

java-core项目概览
Project Overview

如果你了解maven项目目录结构的话,便知道src/main/java是用于存放java源代码,src/main/resources是用于存放配置文件,而src/test/java是用于存放单元测试代码。

右下角有个Maven projects need to import,这里点击Enable Auto-import即可。

由于Maven的默认编译级别是JDK5,我们可以通过在java-core根目录下的pom.xml文件中添加配置修改默认的级别以及处理乱码问题

<build>
        <plugins>


            <!-- 由于Windows默认是GBK编码,这里将源代码编码设置为UTF-8,防止出现乱码         -->

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <!--  由于maven项目默认的编译级别是JDK1.5,这里采用JDK11编译          -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.11</source>
                    <target>1.11</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

pom.xml
pom.xml

IntelliJ IDEA基于java-core创建多模块项目

由于java-core的定位是涵盖所有Java基础知识以及企业开发常用的组件,因此该项目会包含许多模块,最终的模块划
java-core

那么在IntelliJ IDEA中如何创建多模块的项目呢?其实非常简单的...

在创建多模块之前首先需要删除src目录,因为java-core是父目录,其主要作用是是用于管理子模块的依赖以及第三方组件。

选中java-core,右键new Module,还是选择基于Maven的Module
Maven Module

配置module

设置module名称为java-core-foundational

New Module
New Module

New Module

java-core-foundational 总览
java-core-foundational

In order for module division of the project more clearly, so then create sub-module java-core-foundational-object basis java-core-foundational, based on its implementation and java-core-foundational, as long as the mouse to select java-core-foundational , you can then create, create delete java-core-foundational after the completion of the src directory.
New Module

The final directory structure as shown in FIG.
java-core-foundational-object

Maven build the project

Use Maven to build the project in IntelliJ IDEA is especially convenient in
IntelliJ IDEA has been integrated in the top right Maven build common operations: cleaning, compiling, testing, installation and deployment. Just mouse clicks, if the reader has been integrated in Maven in IntelliJ IDEA, you can try.
Maven

Guess you like

Origin www.cnblogs.com/ittimeline/p/12088883.html