Maven configuration and use

Keywords: Maven

ⅩⅥ.# ⅩⅥ.Maven

Maven configuration

1. Decompress maven compressed package (3.2.1)

2. Set the localrepository as your local repository (where the jar package is placed, usually the same level directory as maven), and set the mirror as the image of Alibaba cloud's central repository. conf–>settings.xml

<!--Modify your local jar Location of warehouse-->  
<localRepository>E:\maven\repository</localRepository>
<!--Set central warehouse image-->
 <mirrors>
     <!--Add Alibaba cloud central warehouse-->
     <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>*</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror> 
</mirrors>

3. Add MAVEN_HOME environment variable. The variable value is the folder directory where Maven is located

4. Configure the path of maven into idea, and pay attention to the path

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-leiyidv5-163554258) (C: \ users \ administrator \ appdata \ roaming \ typora \ user images \ image-20211011154021592. PNG)]

5. To create a maven project, select the options that start with Maven and end with webapp*

6. Set groupid (name of development team, generally com. Company name) and artifactid (project name) to go to the next step

7. Improve the project structure, create Java (put java files) and resources (put non java files, xml and properties) under main, mark them, and create a test folder at the same level as main to put unit test classes

8. Add relevant dependencies, search for required dependencies in the central warehouse, find a popular one, copy his dependency, and post it under dependencies. At the same time, paste the server plug-in under plugins, both jetty and tomcat. Note that whichever server you use will run, such as jetty:run. At the initial stage, if there are not many local jar packages, it may take time. Please wait patiently

 <groupId>org.example</groupId><!-- Development team name -->
  <artifactId>MavenDemo</artifactId><!-- entry name -->
  <version>1.0-SNAPSHOT</version><!-- edition -->
  <packaging>war</packaging><!-- Package as war File, packaged and placed in tomcat Run in project file tomcat,tomcat Will automatically war File decompression -->

  <name>MavenDemo Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

<!--pom.xml-->
<dependencies>
    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!--This is an add junit Dependency. If you need to add other dependencies, start from Maven Warehouse acquisition, adding to dependencies Under label-->
</dependencies>
<!--pom.xml-->

 <build>
    <finalName>MavenDemo</finalName>
     <!--finalName Is the project name-->
    <plugins>
        <!--plugins Add plug-in under-->
      <!--maven Plug in 3.8.1 edition, jdk1.7 Version, encoding format UTF—8.-->
       <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
        <!--jetty Plug in microserver plug-in-->
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.3.14.v20161028</version>
      </plugin>
        <!--tomcat7 Plug in microserver plug-in-->
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <port>8080</port>
          <path>/</path>
          <uriEncoding>UTF-8</uriEncoding>
          <server>tomcat7</server>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

9. Please note that the header declaration of web.xml should be changed to version above 3.0, that is, it is consistent with the version in your pom, otherwise servlet annotation and el expression are not supported

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">
    <!--web3.0 Version header file-->
</web-app>

10. To use Lombok, first install plug-ins for your idea, setting = = > plugins = = > marketplace = = > install = = = > Restart idea
Add dependencies in pom@ Use with caution

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.20</version>
      <scope>provided</scope>
    </dependency>

@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
public class Implas {
    private int id;
    private String name;
    private String pwd;
}
//@AllArgsConstructor creates the construction method of all parameters
//@NoArgsConstructor creates a parameterless construction method
//@Setter creates a set method for all properties
//@Getter creates a get method for all properties
//Basically, these four are enough
//@The Data annotation will override toString(), hashCode(), equals() and other methods, which will conflict with the spring framework, so use it with caution

maven installs the oracle driver jar package to the local maven library

Due to the commercial copyright problem of oracle, maven cannot directly download the jar package through the central resource library. If you want to use the jar package, you need to handle it manually.

1, If the Oracle database is installed locally, you can install it from the installation path

D:\oracle11g\product.2.0\dbhome_1\jdbc\lib (my local installation path) select the appropriate jar package:

[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-zszi9dfm-163554270)( file:///C: \Users\ADMINI~1\AppData\Local\Temp\ksohtml13084\wps1.jpg)]

2, Load into local maven Library:

Right click shift + to the maven installation path

[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-yptqhjhh-163554274)( file:///C: \Users\ADMINI~1\AppData\Local\Temp\ksohtml13084\wps2.png)] Open the command window and paste and copy the following commands directly

mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.1.0 -Dpackaging=jar -Dfile=D:\oracle11g\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar 


//D:\oracle11g\product.2.0\dbhome_1\jdbc\lib\ojdbc6.jar is the location of the jar package

The red is the real installation path of the local JDBC jar

Pom.xml can be set as follows

<dependency>

​			<groupId>com.oracle</groupId>

​			<artifactId>ojdbc6</artifactId>

​			<version>11.2.0.1.0</version>

</dependency>

Maven set parameters - properties

<properties>
    <java.version>1.8</java.version>
    <spring-framework.version>4.3.18.RELEASE</spring-framework.version>
</properties>
<!--establish java.version,spring-framework.version Parameter to make the following dependent version numbers consistent and easy to manage-->

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring-framework.version}</version>
</dependency>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Maven common dependencies

		
<!--Spring Common basic dependencies-->
		<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.4</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.3.4</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.4</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.4</version>
            <scope>test</scope>
        </dependency>
<!--mybatis rely on-->
 <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.1</version>
    </dependency>

<!--mybatis-plus rely on-->
<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.3.2</version>
        </dependency>

<!--JSTL rely on-->
 <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.1.2</version>
    </dependency>

<!--javaWeb rely on-->
<dependency>
      <groupId>org.apache.tomcat</groupId>
      <artifactId>tomcat-api</artifactId>
      <version>8.5.72</version>
    </dependency>
   <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>
  <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
      <scope>provided</scope>
    </dependency>

<!--log4j Log dependency-->
 <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.16</version>
    </dependency>

<!--JDBC Connection dependency-->
<dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.0.5</version>
    </dependency>

<!--File upload is dependent and needs to be created by yourself jar Package introduction-->
<dependency>
      <groupId>smartupload</groupId>
      <artifactId>smartupload</artifactId>
      <version>1.0</version>
    </dependency>
<!--lombok Code simplification compilation dependency-->
<dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.20</version>
      <scope>provided</scope>
    </dependency>

Maven configuration

1. Decompress maven compressed package (3.2.1)

2. Set the localrepository as your local repository (where the jar package is placed, usually the same level directory as maven), and set the mirror as the image of Alibaba cloud's central repository. conf–>settings.xml

<!--Modify your local jar Location of warehouse-->  
<localRepository>E:\maven\repository</localRepository>
<!--Set central warehouse image-->
 <mirrors>
     <!--Add Alibaba cloud central warehouse-->
     <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>*</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror> 
</mirrors>

3. Add MAVEN_HOME environment variable. The variable value is the folder directory where Maven is located

4. Configure the path of maven into idea, and pay attention to the path

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-dlcylu9p-1635413548220) (C: \ users \ administrator \ appdata \ roaming \ typora \ typora user images \ image-20211011154021592. PNG)]

5. To create a maven project, select the options that start with Maven and end with webapp*

6. Set groupid (name of development team, generally com. Company name) and artifactid (project name) to go to the next step

7. Improve the project structure, create Java (put java files) and resources (put non java files, xml and properties) under main, mark them, and create a test folder at the same level as main to put unit test classes

8. Add relevant dependencies, search for required dependencies in the central warehouse, find a popular one, copy his dependency, and post it under dependencies. At the same time, paste the server plug-in under plugins, both jetty and tomcat. Note that whichever server you use will run, such as jetty:run. At the initial stage, if there are not many local jar packages, it may take time. Please wait patiently

 <groupId>org.example</groupId><!-- Development team name -->
  <artifactId>MavenDemo</artifactId><!-- entry name -->
  <version>1.0-SNAPSHOT</version><!-- edition -->
  <packaging>war</packaging><!-- Package as war File, packaged and placed in tomcat Run in project file tomcat,tomcat Will automatically war File decompression -->

  <name>MavenDemo Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

<!--pom.xml-->
<dependencies>
    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!--This is an add junit Dependency. If you need to add other dependencies, start from Maven Warehouse acquisition, adding to dependencies Under label-->
</dependencies>
<!--pom.xml-->

 <build>
    <finalName>MavenDemo</finalName>
     <!--finalName Is the project name-->
    <plugins>
        <!--plugins Add plug-in under-->
      <!--maven Plug in 3.8.1 edition, jdk1.7 Version, encoding format UTF—8.-->
       <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
        <!--jetty Plug in microserver plug-in-->
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.3.14.v20161028</version>
      </plugin>
        <!--tomcat7 Plug in microserver plug-in-->
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <port>8080</port>
          <path>/</path>
          <uriEncoding>UTF-8</uriEncoding>
          <server>tomcat7</server>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

9. Please note that the header declaration of web.xml should be changed to version above 3.0, that is, it is consistent with the version in your pom, otherwise servlet annotation and el expression are not supported

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">
    <!--web3.0 Version header file-->
</web-app>

10. To use Lombok, first install plug-ins for your idea, setting = = > plugins = = > marketplace = = > install = = = > Restart idea
Add dependencies in pom@ Use with caution

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.20</version>
      <scope>provided</scope>
    </dependency>

@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
public class Implas {
    private int id;
    private String name;
    private String pwd;
}
//@AllArgsConstructor creates the construction method of all parameters
//@NoArgsConstructor creates a parameterless construction method
//@Setter creates a set method for all properties
//@Getter creates a get method for all properties
//Basically, these four are enough
//@The Data annotation will override toString(), hashCode(), equals() and other methods, which will conflict with the spring framework, so use it with caution

maven installs the oracle driver jar package to the local maven library

Due to the commercial copyright problem of oracle, maven cannot directly download the jar package through the central resource library. If you want to use the jar package, you need to handle it manually.

1, If the Oracle database is installed locally, you can install it from the installation path

D:\oracle11g\product.2.0\dbhome_1\jdbc\lib (my local installation path) select the appropriate jar package:

[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-9oztsfzu-163554224)( file:///C: \Users\ADMINI~1\AppData\Local\Temp\ksohtml13084\wps1.jpg)]

2, Load into local maven Library:

Right click shift + to the maven installation path

[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-idluaed6-1635413548230)( file:///C: \Users\ADMINI~1\AppData\Local\Temp\ksohtml13084\wps2.png)] Open the command window and paste and copy the following commands directly

mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.1.0 -Dpackaging=jar -Dfile=D:\oracle11g\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar 


//D:\oracle11g\product.2.0\dbhome_1\jdbc\lib\ojdbc6.jar is the location of the jar package

The red is the real installation path of the local JDBC jar

Pom.xml can be set as follows

<dependency>

​			<groupId>com.oracle</groupId>

​			<artifactId>ojdbc6</artifactId>

​			<version>11.2.0.1.0</version>

</dependency>

Maven set parameters - properties

<properties>
    <java.version>1.8</java.version>
    <spring-framework.version>4.3.18.RELEASE</spring-framework.version>
</properties>
<!--establish java.version,spring-framework.version Parameter to make the following dependent version numbers consistent and easy to manage-->

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring-framework.version}</version>
</dependency>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Maven common dependencies

		
<!--Spring Common basic dependencies-->
		<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.4</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.3.4</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.4</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.4</version>
            <scope>test</scope>
        </dependency>
<!--mybatis rely on-->
 <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.1</version>
    </dependency>

<!--mybatis-plus rely on-->
<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.3.2</version>
        </dependency>

<!--JSTL rely on-->
 <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.1.2</version>
    </dependency>

<!--javaWeb rely on-->
<dependency>
      <groupId>org.apache.tomcat</groupId>
      <artifactId>tomcat-api</artifactId>
      <version>8.5.72</version>
    </dependency>
   <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>
  <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
      <scope>provided</scope>
    </dependency>

<!--log4j Log dependency-->
 <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.16</version>
    </dependency>

<!--JDBC Connection dependency-->
<dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.0.5</version>
    </dependency>

<!--File upload is dependent and needs to be created by yourself jar Package introduction-->
<dependency>
      <groupId>smartupload</groupId>
      <artifactId>smartupload</artifactId>
      <version>1.0</version>
    </dependency>
<!--lombok Code simplification compilation dependency-->
<dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.20</version>
      <scope>provided</scope>
    </dependency>

Posted by Angry Lettuce on Thu, 28 Oct 2021 01:47:33 -0700