Maven Basics

Pain point of the original project (process control problem)

  • Software testing, document generation, packaging and release - it's best to be automated (one click release)
  • The project relies on a large number of third-party jar packages, which is complex and difficult to manage - POM xml
  • jar is a different manufacturer, and there are too many download sources - warehouse
  • Module dependency

summary

  • Maven is a software project management and comprehension tool
  • Software engineering management tools
  • maven official website

Installation + configuration

  • download

  • Unzip the compressed package you just downloaded
  • Configure environment variables



Architecture project -- Take Hello as an example

  • Convention is more important than configuration -- it has its own directory structure

1. Default configuration

  • Manually create directory structure
pom.xml Content:
	<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	 
	  <modelVersion>4.0.0</modelVersion>
	  <!-- coordinate : Location of this item in the warehouse -->
	  <groupId>com.yc.test</groupId>
	  <artifactId>my-app</artifactId>
	  <version>1.0-SNAPSHOT</version>
	 
	  <name>my-app</name>
	</project>
  • test


2. Test various goal s

  • Different commands will automatically download different plug-ins. maven runs in the form of plug-ins
  • mvn clean: clear the output directory, that is, delete the target directory
  • mvn compile: compile, that is, generate the target directory
  • mvn test: Test
  • mvn package: package it into a jar file and put it in the target directory
  • mvn install: copy a copy of the jar package to the local repository
  • mvn deploy: install jar s into private servers
  • mvn build

3. Change the warehouse from user directory to user-defined directory

setting.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">
	
	<!-- Local warehouse location-->
	<localRepository>D:\MavenRepository</localRepository>

	<pluginGroups>
	</pluginGroups>

	<proxies>
		<!--  Proxy server configuration :   
		               By default, we use  apache Provided maven Warehouse (Central warehouse),  But this warehouse is sometimes unstable, 
					   Several more proxy servers can be configured here. When the central warehouse is not available, download from the proxy server.   -->
	</proxies>

	<!--  Configure the server where the warehouse is located( Private service   nexus )Login user name and password  -->
	<servers>
	    <!--
		<server>
			<id>nexus-releases</id>
			<username>admin</username>
			<password>admin123</password>
		</server>
		<server>
			<id>nexus-snapshots</id>
			<username>admin</username>
			<password>admin123</password>
		</server>
		-->
	</servers>

	<!--    Configure the address of the private server so that you don't have to download from the public server jar Package, but under the private server. The private server does not. Download from the private server to the public server.   -->
	<mirrors>
	    
		
	</mirrors>

	<profiles>
	
		<profile>
			<id>dev</id>
			<repositories>
				<repository>
					<id>aliyun</id>
					<url>http://maven.aliyun.com/nexus/content/groups/public</url>
					<releases>
						<enabled>true</enabled>
						<updatePolicy>always</updatePolicy>
					</releases>
					<snapshots>
						<enabled>true</enabled>
						<updatePolicy>always</updatePolicy>
					</snapshots>
				</repository>
				<repository>
					<id>central</id>
					<url>http://central.maven.org/maven2/</url>
					<releases>
						<enabled>true</enabled>
						<updatePolicy>always</updatePolicy>
					</releases>
					<snapshots>
						<enabled>true</enabled>
						<updatePolicy>always</updatePolicy>
					</snapshots>
				</repository>
				<repository>
					<id>clojars</id>
					<name>clojars</name>
					<url>http://clojars.org/repo/</url>
					<releases>
						<enabled>true</enabled>
					</releases>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</repository>
				<repository>
					<id>redhat</id>
					<name>redhat</name>
					<url>https://maven.repository.redhat.com/ga/</url>
					<releases>
						<enabled>true</enabled>
					</releases>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</repository>

			</repositories>

			<pluginRepositories>
				<pluginRepository>
					<id>aliyun</id>
					<url>http://maven.aliyun.com/nexus/content/groups/public</url>
					<releases>
						<enabled>true</enabled>
						<updatePolicy>always</updatePolicy>
					</releases>
					<snapshots>
						<enabled>true</enabled>
						<updatePolicy>always</updatePolicy>
					</snapshots>
				</pluginRepository>
				<pluginRepository>
					<id>central</id>
					<url>http://central.maven.org/maven2/</url>
					<releases>
						<enabled>true</enabled>
						<updatePolicy>always</updatePolicy>
					</releases>
					<snapshots>
						<enabled>true</enabled>
						<updatePolicy>always</updatePolicy>
					</snapshots>
				</pluginRepository>
				<pluginRepository>
					<id>clojars</id>
					<name>clojars</name>
					<url>http://clojars.org/repo/</url>
					<releases>
						<enabled>true</enabled>
					</releases>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</pluginRepository>

			</pluginRepositories>
		</profile>
		

		<profile>
			<!-- to configure maven Used jdk Version, because if it is not configured, it is automatically adopted jdk1.4, In this way, the plug-in will be downloaded based on jdk1.4 Plug-in, running error  -->
			<id>jdk1.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>
	</profiles>
	<activeProfiles>
		<activeProfile>dev</activeProfile>
		<activeProfile>jdk1.8</activeProfile>
	</activeProfiles>
</settings>

term

1. Warehouse

  • Central warehouse
  • Private server: image warehouse (self built) - nexus
  • Local warehouse

2. Level of configuration file

  • Highest: only works for the current project, for example, POM xml
  • Intermediate: only works for the current user, for example ${user. Home} / m2/settings. xml
  • Low level: Works for projects using the current maven, for example, conf / settings. In the Maven installation package xml

3.settings.xml configuration

  • localRepository: location of the local repository
  • mirror
  • repository
  • agent
  • jdk version

4. goal commonly used by maven

  • Different commands will automatically download different plug-ins. maven runs in the form of plug-ins
  • mvn clean: clear the output directory, that is, delete the target directory
  • mvn compile: compile, that is, generate the target directory
  • mvn test: Test
  • Package in target: jar directory
  • mvn install: copy a copy of the jar package to the local repository
  • mvn deploy: install jar s into private servers
  • mvn build

5. Life cycle

  • Clean Lifecycle does some cleaning before a real build
pre-clean: Perform some tasks that need to be clean Previously completed work
clean: Remove all files generated by the last build
post-clean: Perform some tasks that need to be clean Work done immediately after
  • The core part of Default Lifecycle construction, including compilation, testing, packaging, deployment, etc
  • Site Lifecycle generates project reports, sites, and publishes sites

6. Scope of dependence

Use with IDEA

1. Modify the configuration


2. Create Maven project

3. Publish to target directory

4. Introduce junit dependency


Keywords: Java Maven Spring intellij-idea

Added by Smruthi on Fri, 11 Feb 2022 14:44:41 +0200