Common configuration of Maven's setting.xml and pom.xml

What is Maven:

The official definition of Maven is: Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.
Translation: Apache Maven is a software project management and understanding tool. Based on the concept of the Project Object Model (POM), Maven can manage project builds, reports and documentation from central information.
Maven is developed by java. For java, everything is an object, and our project is also an object. Maven defines and configures a project through the pom.xml file, and then builds and manages our project by parsing the pom file. Maven and springboot also follows the concept of convention over configuration, and has done a lot of default configuration and management for us.

Maven's setting.xml and pom.xml:

setting.xml

setting.xml is a global configuration of maven on the currently installed physical machine. As the main focus of java development, there are two places:

**localRepository**: It is to configure the machine that is currently being developed. jar Where the package is placed, which is what we usually call the local warehouse
**mirrors**: mirror list, when our local repository's jar The package does not support the build and runtime of our current project, at this time maven It will download what we need through the mirror configured in the mirror list jar Bag

Of course, if we are the architect or R&D director of the company, maybe we need to create a private maven repository of the current company. At this time, we need to pay attention to:

**servers**: Authentication configuration,maven Configure accounts, passwords, groups, permissions, where to access the file directory
**proxies**: Http proxy configuration,When private repositories don't have what we need jar package, it will access the external through the proxy maven repository to download jar Bag

Of course, if we need to have a deeper understanding of all setting configurations, we can check the official maven information of the corresponding version. The maven version I use is 3.6.0, and the address is:

https://maven.apache.org/ref/3.6.0/maven-settings/settings.html

The following are some configuration annotations of the default setting.xml file in maven

<?xml version="1.0" encoding="UTF-8"?>
<!-- maven of xml The specification that the configuration file follows, many frameworks or projects will xml When a file is used as a configuration file, it will have its own specification. Your configuration must follow this specification. When the framework or project parses the configuration file at work, it will also parse according to this specification. -->
<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 repository, that is, what you installed maven the address on the disk of the physical machine, maven download jar will be stored here, the project is built jar The package is taken from here -->
<localRepository>G:\mvn_localRepository</localRepository>

  <!-- maven The global plug-in group, generally rarely configured here, is in the project xml document plugins medium configuration, such as our springboot packaged plugin for spring-boot-maven-plugin -->
  <pluginGroups>
    
  </pluginGroups>

  <!-- HTTP Where the proxy is set, generally a local maven As a team's private server, you need to configure this proxy, and you can't find it in the private server. jar The file will access the external network through this proxy to download what we need jar Bag -->
  <proxies>
    
  </proxies>

  <!-- Authentication configuration, when we put the maven As a private server or a privileged mirror, when you need to configure the privilege group and the user name and password under the group, it is configured here -->
  <servers>
    
  </servers>
 
 <!-- Mirror list, that is, we download jar place for package files -->
  <mirrors>
	
	<!-- specific mirror -->
	 <mirror>
		<!-- mirror Id,As the unique identifier of the image, it cannot be repeated -->
		<id>alimaven</id>
		<!-- Mirror name, just install your own preferences -->
		<name>aliyun maven</name>
		<!-- The address of the mirror, that is, we download jar package visit place -->
		<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
		<mirrorOf>central</mirrorOf>        
	</mirror>
  </mirrors>
  
  <!-- The global configuration information group is generally not recommended to be configured globally here, it is all in the specific maven engineering pom file to configure -->
  <profiles>
	 <!-- configuration information -->
	 <profile>
		  <!-- The unique identifier of a specific configuration information -->
		  <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>
  </profiles>
  <!-- The configuration information that is currently in effect is generally not configured in the global configuration. -->
  <activeProfiles/>
  
</settings>

pom.xml

As java development is more concerned but not very clear:
modelVersion The version of the POM file model is derived from the version of the super POM of maven installed on the current physical machine. The properties that we have not configured in the pom.xml under the project are all taken from the value in the super pom file. This super file is in the maven In maven-model-builder-3.6.0.jar in the lib folder of the installation directory

The places in the super pom file that are highly related to our usual work:

What else is there specifically, you can go to your maven directory to find this file to see
Scope: the scope of the current dependency, the default is: compile, other options: runtime, test, system, provided, what is the role, the official introduction is as follows:

Official address:

https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

The address of the official configuration information of the pom file of maven3.6.0:

https://maven.apache.org/ref/3.6.0/maven-model/maven.html
<?xml version="1.0" encoding="UTF-8"?>
<!-- maven Engineering POM file followed xml specification -->
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<!-- POM The version of the file model, derived from the version installed on the current physical machine maven the super POM version of -->	 
    <modelVersion>4.0.0</modelVersion>
	<!-- Child project declares parent project's information -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
	<!-- Information about the current project -->
	<!-- Inversion of the domain name of the company or organization of the current project+The name of the project or plan, of course, can also be virtual, such as the name of a large company spring It's according to this rule -->
    <groupId>com.example</groupId>
	<!-- module name -->
    <artifactId>demo</artifactId>
	<!-- version number -->
    <version>0.0.1-SNAPSHOT</version>
	<!-- project name -->
    <name>demo</name>
	<!-- project description -->
    <description>Demo project for Spring Boot</description>
    
	<!-- Version management of unified dependencies of the current project -->
    <properties>
        <java.version>1.8</java.version>
    </properties>

	<!-- maven project dependent jar Bag,Will go to the local warehouse to find out if there is any specific jar package, there is no way to download what we need from the configured warehouse jar Bag -->
    <dependencies>
		<!-- specific dependencies -->
        <dependency>
			<!-- of currently dependent components -->
			<!-- Inversion of the domain name of the company or organization of the current project+The name of the project or plan, of course, can also be virtual, such as the name of a large company spring It's according to this rule -->
            <groupId>org.springframework.boot</groupId>
			<!-- The name of the currently dependent module -->
            <artifactId>spring-boot-starter-test</artifactId>
			<!-- The version number of the currently dependent module -->
			<version></version>
			<!-- The scope of the current dependency, the default is: compile,Other options: runtime, test, system, provided -->
            <scope>test</scope>
			<!-- Configuration to exclude not imported jar,There are many under each dependent module jar package, which may also be introduced in other dependencies of the current project jar package, but the versions are different, maybe these two jar There will be conflicts, you can specify in one of the dependencies to be excluded jar -->
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>


	<!-- maven Project build configuration -->
    <build>
		<!-- Plugins used by the current project build -->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
	
	<!-- Aggregation configuration, for example, microservices need to have various components and services. I want to give a folder outside to declare what this whole big project is for. At this time, aggregation will be used. -->
	<!-- Aggregation configuration, for example, one of my modules is more complicated. I need to split it into multiple projects, and this module also needs to be aggregated. At this time, this configuration will take effect. When aggregation is configured, other projects that are referenced will be automatically typed when packaging. become jar bag into lib Under contents -->
	<modules>
		<!-- Concrete modules owned by the aggregate -->
        <module></module>
    </modules>
	
	<!-- Environmental management -->
	<issueManagement>
        
    </issueManagement>
	
	<!-- For the configuration of dependency management, for example, I have a parent project, and each subproject under the parent project has the same dependency import. At this time, I can do unified dependency version configuration management here, and introduce the same dependency of the unified version, of course, it will take effect. The dependencies are still the following
	Each sub-project's own POM The dependencies in the file, but if the version of the dependency is specified in this place, then if the subproject does not have a given version, the version number of this place will be used as the version number of the dependency introduced by the subproject. -->
	<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId></groupId>
                <artifactId></artifactId>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

Use of Maven:

maven creates projects. maven creates projects that are similar to the scaffolding of front-end vue projects. There are two types we commonly use:

maven-archetype-quickstart
maven-archetype-webapp

Of course, there are two ways for maven to create a project. One is to execute the maven command in the directory where you want to create the project:

mvn -B archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4

The other is under development tools such as idea and eclipse

**maven The directory structure of the project:**
**pom**:pom It's us maven For a definition and configuration file for the entire project, maven It is through this file to build and manage our project
**src**:src Stored below are our code and some configuration files, as well as resource files, mian Below is the source code related, test The following is related to the test
**tartget**: Compile, run, test, package and generate related files will be stored in this directory


Commands commonly used by maven, if the maven command is not executed in the development tool, be sure to enter the directory where the pom file of the project you want to execute the command is located and execute these commands:

**mvn clean**: clean up target content
**mvn compile**: project compilation
**mvn test**:Execute current project test directory with Junit Annotated test cases will also generate results in target Under contents
**mvn package**: according to pom The packaging method of the file and the plug-in packaging, the generated package will be in the target Under contents
**mvn install**: Package the current project and publish the packaged files of the current project to the local repository
**mvn deploy** Package the current project and publish it to the remote warehouse. When the project team works together, the basic jar The update of the package must have this operation, otherwise the introduction of other projects will report an error, of course, you need to configure the address of the corresponding release warehouse

Tags: Java Operation & Maintenance Maven Spring Boot Back-end

Posted by ChibiGuy on Tue, 03 May 2022 20:15:50 +0300