Article From:https://www.cnblogs.com/coffees/p/9967626.html

MavenIt is an open source project management tool developed in pure Java based on the concept of Project Object Model (POM). Mainly used to manage Java projects, dependency management (jar package management, can automatically analyze the dependency packages needed by the project, and download them to Maven warehouse) and project construction (Project packaging and deployment. In addition, it can be developed in blocks to improve the efficiency of development.

  This article will start from the following three aspects:

  1、MavenRole

  2、MavenInstallation configuration

  3、IDEAConfigure Maven

QQFigure 2: 20181031143508 uuuuuuuuuuuu

  1、MavenWhy use Maven

  (1)Create projects, automatically associate and download dependent jar packages.

  (2)Upgrade the version of the framework. Upgrade the version of a framework, but do not know which jars associated with it need to follow the upgrade, and upgrade to which version.

  (3)Automatic deployment. With Maven, you can automatically deploy the project to a remote server with just one command.

  (4)Project compilation, packaging and export. This can be done by any IDE, so if Maven is to do this, the possible scenario is to do it in an environment without an IDE, such as in a server-side Linux environment.

  2、MavenInstallation configuration

  1、Download maven’s address: http://maven.apache.org/download.cgi

  2、Unzip, and then add the following information to “My Computer” – – “Properties” – – “Advanced System Settings” – – “Environment Variables”:

  a、Installation folder for Maven with the value of new variable MAVEN_HOME

  b、Add:% MAVEN_HOME%\bin to PATH variable

  3、Verify that the configuration is successful, open CMD and enter: mvn-version

  MavenConfiguration file (settings. xml) specification

  1.localRepository:Set up local warehouse:

  <localRepository>C:\software\repository</localRepository>

  2.pluginGroups:Plug-in combination

  3.proxies:agent

  4.serversServer (where username and password are the username and password of private service, followed by configuration of private service instructions)

  <server>

  <id>user-releases</id>

  <username>admin</username>

  <password>123456</password>

  </server>

  <server>

  <id>user-snapshots</id>

  <username>admin</username>

  <password>wangwujuan</password>

  </server>

  5.mirrors:Mirror path

  <!– Alibaba mirror –>

  <mirror>

  <id>alimaven</id>

  <name>aliyun maven</name>

  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>

  <mirrorOf>central</mirrorOf>

  </mirror>

  6.profiles:Server configuration

  <!–Server Configuration – & gt;

  <profile>

  <id>nexusProfile</id>

  <repositories>

  <repository>

  <id>nexus</id>

  <name>nexus</name>

  <url>http://localhost:8081/nexus/content/groups/public/</url>

  <releases>

  <enabled>true</enabled>

  </releases>

  <snapshots>

  <enabled>true</enabled>

  </snapshots>

  </repository>

  </repositories>

  <pluginRepositories>

  <!– Plug-in warehouse, the operation of Maven depends on plug-ins, also need to download plug-ins from private service – & gt;

  <pluginRepository>

  <!– The ID of plug-in repository does not allow duplication, and if the configuration is repeated, it will cover the front – & gt;

  <id>public</id>

  <name>Public Repositories</name>

  <url>http://localhost:8081/nexus/content/groups/public/</url>

  </pluginRepository>

  </pluginRepositories>

  </profile>

  <!– javaCompiler plug-in, compiler version of JDK – & gt;

  <profile>

  <id>jdk-1.8</id>

  <activation>

  <activeByDefault>true</activeByDefault>

  <jdk>1.8</jdk>

  </activation>

  <properties>

  <maven.compiler.source>1.8</maven.compiler.source>

  <maven.compiler.target>1.8</maven.compiler.target>

  <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>

  </properties>

  </profile>

  7.activeProfiles:Activate private clothes

  <activeProfiles>

  <activeProfile>nexusProfile</activeProfile>

  </activeProfiles>

  MavenWarehouse description

  Warehouse for storing jar (3 warehouses)

  1.Local Warehouse: Stored on local disk, default on System Disk C disk username/.m2/xx

  Change the local repository of the local repository through settings. XML

  2.Remote Warehouse: Usually domestic mirrors or companies build their own private clothes. Usually Nexus is used.

  OBJECTIVE: To accelerate download speed

  3.Maven(Central) Warehouse: The jar package warehouse maintained by the Maven team at http://repo1.maven.org/maven2/

  Use this to check POM: http://mvnrepository.com/

  Retrieval order: first retrieve the local warehouse, then retrieve the remote warehouse, and finally retrieve the central warehouse

  MavenManage project cycle

  1. Clean:Cleaning up before project construction

  2. Default: Core Life Cycle Core Processes: Compilation, Running, Packaging, etc.

  3. Site:Publishing sites, generating reports, etc.

  3、IDEAConfigure Maven

  IDEAMaven is integrated internally, or can be configured to install Maven on your own.

  File —-> Settings —-> Find Maven and configure Maven home directory

Picture 2

  Create Maven project

  File —-> New —-> Project —-> Maven

  Check Create from archetype

  Select maven-archetype-webapp

  Click Next

Picture 3

  Enter GroupId, ArtifactId and click Next

Picture 4

  Select version of Maven

Picture 5

  Enter the project name and directory, and click Finish

Picture 6

  After creating the project, click the Import Changes button to download the required jar packages and plug-ins

Picture 7

  MavenProject description

  src/main/java:The. java file that stores the project

  src/main/resources:Deposit project resource files. How to Spring, Hibernate Profile

  src/test/java:Store all test. Java files, such as Junit test classes

  pom.xml(mavenProject Core Profile)

  pom.xmlDefault Content Introduction:

  <!– mavenThe version number of the middle model – & gt;

  <modelVersion>4.0.0</modelVersion>

  <!– Company name or organization name or personal name – & gt;

  <groupId>com.sky</groupId>

  <!–The name of the project is –>

  <artifactId>FirstMaven</artifactId>

  <!– Version number –>

  <version>1.0</version>

  <!– Packaging: Project type

  POM:MAVENProjects, often used in inheritance and aggregation

  JAR:JAVASEProjects, no web pages, nothing to do with the server

  WAR:JAVAEEProject, it’s nothing to do with the server – & gt;

  <packaging>war</packaging>

  MavenEngineering add dependency

  Recommended website: http://mvnrepository.com/

  Example:

  <dependencies>

  <!– https://mvnrepository.com/artifact/org.hibernate/hibernate-core –>

  <dependency>

  <groupId>org.hibernate</groupId>

  <artifactId>hibernate-core</artifactId>

  <version>5.2.10.Final</version>

  </dependency>

  </dependencies>

  Add the POM file content description to the project (switch pom. XML to the Dependencies directory):

  type:type

  Value Description:

  jar:jarpackage

  Scope:Declare the lifetime of the jar package

  Value Description:

  1、provided:Requirements for compilation and testing

  2、compile:Compile, test, run, package requirements

  3、runtime:Test, run, package requirements

  4、test:test

  5、system:When compiling and testing, you need to display references, and Maven will not retrieve them.

Leave a Reply

Your email address will not be published. Required fields are marked *