Maven from 0 to 1 (solve the problem that the project cannot be started)

1 what is maven?

maven is an open source project of java. jdk running environment is required for installation and operation

effect

1. jar package dependency management of Java project

2. The construction of Java project includes compilation, testing, packaging and deployment

2.maven installation

test

[the external chain picture transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-qkpmivvz-1602669625258) (maven-class.assets/image-202010130959109109109109109109. PNG)]

3.Maven configuration

Mainly modify C: \ soft \ apache-maven-3.6.0 \ conf \ settings xml

maven is the default address of the local warehouse. If the c disk control is abundant, it does not need to be configured

C:\Users\wgz.m2\repository

<localRepository>D://path/to/local/repo</localRepository>

4. Classification of warehouse

Local warehouse

Central warehouse

Public Warehouse

Private server warehouse

maven central warehouse official website

https://mvnrepository.com/

The final source of almost all jar packages is the central warehouse

Configuring alicloud images

<!--setting.xml Add the following configuration to the-->
<mirrors>
	<mirror>
        <id>aliyun</id>  
        <!-- Central warehouse mirror(image) -->
        <mirrorOf>central</mirrorOf>    
        <name>Nexus aliyun</name>
        <!-- aliyun After the warehouse address, all requests to point to the central warehouse will point to aliyun Warehouse-->
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>  
    </mirror>
</mirrors>

5.idea creation project

java project

Packaging method

Packaging method
    jar Package the current project as jar mvn package
    war Package project as war tomcat-java web Engineering use
    pom The parent-child project uses the parent project table as pom

Java EE project


There is an error here. Select webapp

Add sevlet dependency in pom

    <dependency>
      <!-- jstl support -->
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>


    <!-- introduce servelet -api rely on-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
      <!-- Required at compile time, not at run time  -->
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>provided</scope>
    </dependency>

Configure tomcat

When jar cannot be downloaded???

Solution 1

Solution 2

Finally, be sure to refresh pom again

5.jar dependent declaration cycle

Scope of jar

Test: it is necessary to compile the test, and the jar will not be entered during packaging

compile: needed at any time

provided: required at compile time, not required at run time

runtime: not required at compile time, but required at run time, test time and packaging time

 <dependencies>

    <!--test jar-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <!-- Mark current jar There are only test phases-->
      <scope>test</scope>
    </dependency>




    <dependency>
      <!-- jstl support -->
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>


    <!-- introduce servelet -api rely on-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
      <!-- Required at compile time,Not required at runtime  -->
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>provided</scope>
    </dependency>


    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.17</version>
      <!--Only required at runtime jar Call in when packing-->
      <scope>runtime</scope>
    </dependency>


  </dependencies>

6.maven life cycle

Three:

Clean when the project needs to be rebuilt, clean deletes the target

Build build phase

Compile compile

test junit unit test

package will complete the project into jar war

install installs the jar to the local warehouse

deploy will be Jared and deployed to the remote warehouse

Site deployment site

7. Private service

effect:

1. Improve jar download speed

2. Solve the problem that the company cannot directly access the external network warehouse due to code security

3. Place public jars inside the company, and colleagues reference each other's jars through private servers

The jar cannot be imported

Query all lastupdated

Tags: Java Maven

Posted by jesse24 on Thu, 12 May 2022 01:07:53 +0300