How do I configure the Maven environment in IDEA?

Keywords: Maven Apache xml nexus

1. Agreements

  • Classified storage
    • The X:/applications directory holds development tools such as IDEA, Eclipse, Typora
    • The X:/Environments directory holds the development environment, such as JDK, MinGW-w64, cmder
    • X:/IdeaProjects directory for IDEA projects
    • The X:/workspaces directory holds Eclipse's workspace
    • X:/Repository directory used as repository for apache-maven
    • The X:/Repository/Synchronization directory is used as the local warehouse location for apache-maven synchronization from the central warehouse This directory, which corresponds to the localRepository configured in apache-maven's conf/settings.xml
    • The X:/Repository/ThirdParty directory is used to store jar files (such as ojdbc.jar) that do not exist in the maven central repository.

Note that the X disk here can be a C disk, a D disk, etc. on the hard disk.

  • Code
    • Eclipse uses UTF-8 encoding for all text files
    • Line break using Unix (New text file line delimiter)

2,Maven

2.1, Download

from apache-maven website Download the latest version of maven, on the download page of the official website:

  • Binary zip archive (apache-maven-3.6.0-bin.zip) represents a compressed package that has been compiled for direct use
  • Source zip archive (apache-maven-3.6.0-src.zip) represents the source package

2.2, Decompression

By convention, apache-maven-3.6.0-bin.zip should be decompressed into the X:/applications directory.

2.3. Configuration

By convention, find the conf directory in the X:/applications/apache-maven-3.6.0 directory and open the settings.xml file in it with tools such as EditPlus, EmEditor, Notepad+.

2.3.1, Replacement

Replace everything in the conf/settings.xml file with the following:

<?xml version="1.0" encoding="UTF-8"?>

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
   -->
  <localRepository>/path/to/local/repo</localRepository>
  
  <pluginGroups></pluginGroups>

  <proxies></proxies>

  <servers></servers>

  <mirrors>
   
  </mirrors>
 
  <profiles></profiles>

</settings>

2.3.2, Modify local warehouse

It is recommended that, by convention, the local warehouse location in the conf/settings.xml file be modified as follows:

<localRepository>D:/Repository/Synchronization</localRepository>

2.3.3, Modify mirror

   <mirrors>
	<!-- Provided by Ali Yun Maven Central warehouse mirror -->
	<mirror>
		<id>nexus-aliyun</id>
		<name>Nexus aliyun</name>
		<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
		<mirrorOf>*</mirrorOf>
	</mirror> 

	<!-- from Apache Provided warehouse -->
	<mirror>
		<id>apache-repo</id>  
		<name>apache Repository</name>  
		<url>https://repository.apache.org/content/groups/public/</url> 
		<mirrorOf>apache-repo</mirrorOf> <!-- Note that you must write here apache-repo -->
	</mirror> 
  </mirrors>

2.3.4, final settings.xml

<?xml version="1.0" encoding="UTF-8"?>


<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
   -->
  <localRepository>D:/Repository/Synchronization</localRepository>
  
  <pluginGroups></pluginGroups>

  <proxies></proxies>

  <servers></servers>

  <mirrors>
	<!-- Provided by Ali Yun Maven Central warehouse mirror -->
	<mirror>
		<id>nexus-aliyun</id>
		<name>Nexus aliyun</name>
		<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
		<mirrorOf>*</mirrorOf>
	</mirror> 

	<!-- from Apache Provided warehouse -->
	<mirror>
		<id>apache-repo</id>  
		<name>apache Repository</name>  
		<url>https://repository.apache.org/content/groups/public/</url> 
		<mirrorOf>apache-repo</mirrorOf> <!-- Note that you must write here apache-repo -->
	</mirror> 
  </mirrors>
 
  <profiles></profiles>

</settings>

3,IDEA

3.1. Configuring maven

3.1.1, Open Setup Interface

Click the triangle to the right of Configure in the lower right corner of the following interface, and then click the Settings option to open the settings interface.

3.1.2, Setting Software Themes and Fonts

In the Settings for New Projects window, select Appearance under Appearance & Behavior.

In the "Theme" option on the right, you can toggle the theme through a drop-down list.

After checking "Use custom font", you can set "font" and font size

3.1.3, Configuring maven in IDEA

In the Settings for New Projects window, select Maven under Build Tools under Build, Execution, Deployment, as shown below:

On the right''Maven home directory'', on the rightmost () button on the same line, click and select the apache-maven home directory.

The so-called apache-maven home directory is the decompressed directory of apache-maven when it is decompressed.

On the rightmost side of the same line as the User settings file, check the Override check box, then click the Folder icon in the input box before the check box and select the apache-maven configuration file (by convention, it should be X:\applications\apache-maven-3.6.0conf\settings.xml)

The modified interface is as follows:

Finally, click the Apply button in the lower right corner of the whole interface, and then click the OK button.

3.2, Create maven-webapp

3.2.1, Open the New Project interface

In the welcome screen (image below), click Create New Project to bring up the New Project interface.

New Project interface:

3.2.2, Select a maven prototype

3.2.2.1, Select maven

Select maven from the list on the left side of the New Project interface:

3.2.2.2, Specify Project SDK

Since this is the first time IDEA has been used to develop Java projects, including maven projects, the "Project SDK" here is not yet configured, so the SDK needs to be configured.

Just above the "Project SDK" on the right side of the same line, click the New... button and select JDK to follow the directory.

"Project SDK" is the corresponding JDK after selection.

3.2.2.3, select maven-webapp in the archetype list

Check Create from Archtype in the New Project interface.

Then select "org.apache.maven.archetypes:maven-archetype-webapp", as shown in the following figure:

Then click the Next button to proceed to the next step (as shown below).

Here, enter GroupId, ArtifactId, respectively

  • GroupId : cn.edu.ecut
  • ArtifactId : hello

Tap the Next button directly to proceed to the next step (as shown in the image below).

Continue clicking Next to move on (as shown below)

Here, by convention, the "Project location" should be changed to X:\IdeaProjects\hello, with the modified interface as shown in the following figure:

Finally, click Finish to start creating the project.

During the construction process, the lower right corner of the IDEA software interface may prompt:

Click Enable Auto-Import.

Then enter the hot project construction process, and finally see "BUILD SUCCESS" under the software interface, which means the project is built successfully.

3.3. Configuring maven-webapp

Open the pom.xml file under the current project, where:

  <groupId>cn.edu.ecut</groupId>
  <artifactId>hello</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

Is the most basic information of the current project.<packaging>Used to specify how a project should be packaged.

3.3.1, Modify the properties section

Replace the original contents of <properties>in pom.xml with the following:

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- According to your own Project SDK Determine version -->
    <maven.compiler.source>12</maven.compiler.source>
    <maven.compiler.target>12</maven.compiler.target>
    <maven.compiler.compilerVersion>12</maven.compiler.compilerVersion>
  </properties>

3.3.2, Modify dependencies section

Replace the original contents of <dependencies>in pom.xml with the following:

  <dependencies>

    <!-- Add pairs Servlet Support -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
    </dependency>

    <!-- MySQL drive -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.45</version>
    </dependency>

  </dependencies>

3.3.3, Modify the build section

Replace the original contents of <build>in pom.xml with the following:

  <build>
    <finalName>${project.artifactId}</finalName>
    <plugins>

      <!-- Maven Compiler Plugin -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <source>${maven.compiler.source}</source>
          <target>${maven.compiler.target}</target>
          <compilerVersion>${maven.compiler.compilerVersion}</compilerVersion>
          <encoding>${project.build.sourceEncoding}</encoding>
        </configuration>
      </plugin>

      <!-- Tomcat Maven Plugin -->
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <port>8080</port>
          <path>/</path>
          <uriEncoding>UTF-8</uriEncoding>
          <useBodyEncodingForURI>true</useBodyEncodingForURI>
        </configuration>
      </plugin>

    </plugins>

  </build>

3.3.4, Append repositories configuration

Add the following to </project>inside the pom.xml file:

  <repositories>
    <repository>
      <id>nexus-aliyun</id>
      <name>Nexus aliyun</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
    <repository>
      <id>apache-repo</id>
      <name>apache Repository</name>
      <url>https://repository.apache.org/content/groups/public/</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
  </repositories>

Posted by saco721 on Sat, 07 Sep 2019 21:50:52 -0700