Maven 03-pom.xml core Configuration

Source: Internet
Author: User

The Maven Pom. xml configuration file is not just a configuration dependency package. It can configure more attributes, including plug-in items.

Define environment attributes
  • Define some basic variable information in the Pom. xml file
  • You can declare the project name, version number, and other related attributes as variables.
  • The reference of these variables can be used throughout the configuration to change the content.
<! -- Define environment properties --> <Properties> <project. build. sourceencoding> UTF-8 </project. build. sourceencoding> <JDK. version> 1.8 </JDK. version> <JUnit. version> 4.12 </JUnit. version> <spring. version> 5.1.1.release </spring. version> <servlet. version> 4.0.1 </servlet. version> </Properties> <! -- Reference variable --> <dependencies> <dependency> <groupid> JUnit </groupid> <artifactid> JUnit </artifactid> <version >$ {JUnit. version }</version> </dependency> </dependencies>
Plug-in configuration
  • All Maven configurations in myeclipse have no practical significance. They are only valid for the current work zone and projects currently in use.
  • To make the entire project effective for a long time, you can set some related plug-ins in the Pom. xml file.
1. Modify the Pom. xml file for JDK compilation plug-in configuration
<Properties> <project. Build. sourceencoding> UTF-8 </Project. Build. sourceencoding> <JDK. version> 1.8 </JDK. version> <! -- Define a public attribute describing the JDK version --> </Properties> <build> <finalname> m2work </finalname> <plugins> <! -- Define the plug-in to be used. There are many plug-ins in Maven --> <plugin> <groupid> org. Apache. Maven. plugins </groupid> <! -- Define the plug-in group information --> <artifactid> Maven-compiler-plugin </artifactid> <! -- Compile the plug-in --> <configuration> <source >$ {JDK. version} </source> <target >$ {JDK. version} </Target> <encode >$ {project. build. sourceencoding} </encode> </configuration> </plugin> </plugins> </build>
2. Overall project update: Alt + F5
  • Although myeclipse has already configured a plug-in for the project, this plug-in has no actual effect at this time.
  • If you want the configuration to take effect, you also need to update the overall project.

3. You can directly package the program.

4. Output source code jar package
  • Modify the Pom. xml configuration file and then package the program directly.
<plugin>    <groupId>org.apache.maven.plugins</groupId>    <artifactId>maven-source-plugin</artifactId>    <configuration>        <encode>${project.build.sourceEncoding}</encode>    </configuration>    <executions>        <execution>            <id>sources</id>            <goals>                <goal>jar</goal>            </goals>        </execution>    </executions></plugin>
5. generate relevant program documents, provided that the Code has detailed comments
  • Modify the Pom. xml configuration file and then package the program directly.
<plugin>    <groupId>org.apache.maven.plugins</groupId>    <artifactId>maven-javadoc-plugin</artifactId>    <configuration>        <encode>${project.build.sourceEncoding}</encode>    </configuration>    <executions>        <execution>            <id>javadocs</id>            <goals>                <goal>jar</goal>            </goals>        </execution>    </executions></plugin>

Package Scope
  • WEB Project Development packs all development programs into a *. War file.
  • In this war file, all third-party development kits will exist.
  • For some important development packages, save them in the lib directory.
  • If you do not need an SDK, you do not want it to be output to Lib.
  • For example, JUnit is only used for testing, but does not want to be output to the war file when the actual project is released.
<dependencies>    <dependency>        <groupId>junit</groupId>        <artifactId>junit</artifactId>        <version>${junit.version}</version>        <scope>test</scope>    </dependency>    <dependency>        <groupId>javax.servlet</groupId>        <artifactId>javax.servlet-api</artifactId>        <version>${servlet.version}</version>        <scope>provided</scope>    </dependency>    <dependency>        <groupId>org.springframework</groupId>        <artifactId>spring-core</artifactId>        <version>${spring.version}</version>    </dependency></dependencies>
  • Scope is the scope of the development kit.
  • JUnit is only a test environment, so a test scope is used.
  • Spring-core does not have any configuration. The default value is compile.
  • The servlet package itself is provided by the Tomcat container. Therefore, this package is only valid during project compilation and is not stored in the lib directory.
  • Compile
    • Default scope of Dependencies
    • Dependencies are running, testing, and compiling.
  • Runtime
    • The dependency is required only during running.
  • Test
    • Dependencies are only required during testing.
  • Provided
    • Dependencies are only required during compilation and testing. The JDK or runtime container provides
  • System
    • The dependency is provided by you and does not need to be downloaded from the maven repository.
Exclusion of dependency packages
  • When you control the dependency of a package, you will find other Dependencies with repeated dependencies.
  • At this time, only one public commons-logging development package exists in the entire project.
<dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-core</artifactId>    <version>${spring.version}</version>    <exclusions>        <exclusion>            <groupId>commons-logging</groupId>            <artifactId>commons-logging</artifactId>        </exclusion>    </exclusions></dependency>
Inheritance relationship
  • In a project, basic configurations such as attributes and related plug-ins are required for almost every Pom. xml file.
  • At this time, all the core configuration options are defined in the parent project, and then each sub-project needs to reference the definition in this parent project.
<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> <groupid> CN. liang </groupid> <artifactid> base </artifactid> <version> 0.0.1-Snapshot </version> <packaging> pom </packaging> <! -- Define a parent Pom. xml file --> <Name> base </Name> <URL> http://maven.apache.org </URL> <! -- In future development, all version controls will be handed over to the base for completion --> <Properties> <project. build. sourceencoding> UTF-8 </project. build. sourceencoding> <JDK. version> 1.8 </JDK. version> <! -- Define a public attribute describing the JDK version --> <JUnit. version> 4.12 </JUnit. version> <spring. version> 5.1.1.release </spring. version> <servlet. version> 4.0.1 </servlet. version> </Properties> <build> <finalname> base </finalname> <plugins> <plugin> <groupid> Org. apache. maven. plugins </groupid> <artifactid> Maven-compiler-plugin </artifactid> <configuration> <source >$ {JDK. version} </source> <target >$ {JDK. version} </Target> <encode >$ {project. build. sourceencoding} </encode> </configuration> </plugin> <groupid> Org. apache. maven. plugins </groupid> <artifactid> Maven-source-plugin </artifactid> <configuration> <encode >$ {project. build. sourceencoding }</encode> </configuration> <executions> <execution> <ID> sources </ID> <goals> <goal> jar </goal> </goals> </execution> </executions> </plugin> <groupid> Org. apache. maven. plugins </groupid> <artifactid> Maven-javadoc-plugin </artifactid> <configuration> <encode >$ {project. build. sourceencoding }</encode> </configuration> <executions> <execution> <ID> javadocs </ID> <goals> <goal> jar </goal> </goals> </execution> </executions> </plugin> </plugins> </build> </Project>
  • Pom. xml configuration file for the subitem
  • Relevant Properties and configured plug-ins are automatically inherited from the parent project.
<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>  <parent>      <groupId>cn.liang</groupId>      <artifactId>base</artifactId>      <version>0.0.1-SNAPSHOT</version>      <relativePath>../base/pom.xml</relativePath>  </parent>  <artifactId>m2work</artifactId>  <packaging>jar</packaging>  <version>0.0.1</version>   <name>m2work</name>   <url>http://maven.apache.org</url>   <dependencies>      <dependency>          <groupId>junit</groupId>          <artifactId>junit</artifactId>          <version>${junit.version}</version>      </dependency>               <dependency>          <groupId>javax.servlet</groupId>          <artifactId>javax.servlet-api</artifactId>          <version>${servlet.version}</version>      </dependency>                <dependency>          <groupId>org.springframework</groupId>          <artifactId>spring-core</artifactId>          <version>${spring.version}</version>      </dependency>    </dependencies>    <build>      <finalName>m2work</finalName>  </build></project>
  • Import the required development kit in the Pom. xml file of the parent class.
<dependencyManagement>  <dependencies>      <dependency>          <groupId>junit</groupId>          <artifactId>junit</artifactId>          <version>${junit.version}</version>      </dependency>  </dependencies> </dependencyManagement>
  • However, for sub-projects, you also need to import the SDK, but you do not need to manage version numbers.
<dependencies>      <dependency>          <groupId>junit</groupId>          <artifactId>junit</artifactId>      </dependency>  </dependencies>

Maven 03-pom.xml core Configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.