maven series 04 - build tag

https://www.cnblogs.com/whx7762/p/7911890.html

In Maven's pom.xml file, there are two kinds of < build >:

(1) Global configuration (project build)

Valid for all situations of the whole project

(2) Configuration (profile build)

For different profile configurations

<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">  
  ...  
  <!-- "Project Build" contains elements of the BaseBuild set and the Build set-->  
  <build>...</build>  
   
  <profiles>  
    <profile>  
      <!-- "Profile Build" contains elements of the BaseBuild set only -->  
      <build>...</build>  
    </profile>  
  </profiles>  
</project> 

Explain:

A kind of < build > is called Project Build, which is the direct child element of < project >.

Another kind of < build > is called Profile Build, which is the direct child element of < profile >.

Profile Build contains basic build elements, while Project Build also contains two special elements, namely various <... Directory > and < extensions >.

1. Basic elements

Examples are as follows

<build>  
  <defaultGoal>install</defaultGoal>  
  <directory>${basedir}/target</directory>  
  <finalName>${artifactId}-${version}</finalName> 
  <filters>
   <filter>filters/filter1.properties</filter>
  </filters> 
  ...
</build>

1)defaultGoal
When you execute the build task, if you do not specify a target, the default value of is used.
As configured above: if mvn is executed on the command line, it is equivalent to mvn install

2)directory
The directory where the build target files are stored. The default directory is ${basedir}/target

3)finalName
The name of the build target file. The default is ${artifactId}-${version}

4)filter
Define the *. Properties file, which contains a list of properties that will be applied to the resources that support the filter.
In other words, the name=value key value pair defined in the filter file will be applied to resources for ${name} value in the build era.   
The default filter folder for maven is ${basedir}/src/main/filters

2. Resources configuration

Used to include or exclude some resource files

<build>  
        ...  
       <resources>  
          <resource>  
             <targetPath>META-INF/plexus</targetPath>  
             <filtering>true</filtering>  
            <directory>${basedir}/src/main/plexus</directory>  
            <includes>  
                <include>configuration.xml</include>  
            </includes>  
            <excludes>  
                <exclude>**/*.properties</exclude>  
            </excludes>  
         </resource>  
    </resources>  
    <testResources>  
        ...  
    </testResources>  
    ...  
</build>  

1)resources
A list of resources elements. Each describes what and where the files associated with the project are

2)targetPath
Specify the folder where the resource after build is stored. The default is basedir.
The target path of resources that are usually packaged in jar is META-INF

3)filtering
true/false indicates whether the resource and filter are activated

4)directory
Define the folder where the resource file is located. The default is ${basedir}/src/main/resources

5)includes
Specifies which files will be matched with * as a wildcard

 6)excludes
Specify which files will be ignored

7)testResources
The definition is similar to resource, but only used in test

3. plugins configuration

Use to specify the plug-in to use

<build>  
    ...  
    <plugins>  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-jar-plugin</artifactId>  
            <version>2.0</version>  
            <extensions>false</extensions>  
            <inherited>true</inherited>  
            <configuration>  
                <classifier>test</classifier>  
            </configuration>  
            <dependencies>...</dependencies>  
            <executions>...</executions>  
        </plugin>  
    </plugins>  
</build>  

4. Plugin management configuration

The configuration of plugin management is the same as that of plugins. It is only used for inheritance so that it can be used in child pom.

Father pom:

<build>  
    ...  
    <pluginManagement>  
        <plugins>  
            <plugin>  
              <groupId>org.apache.maven.plugins</groupId>  
              <artifactId>maven-jar-plugin</artifactId>  
              <version>2.2</version>  
                <executions>  
                    <execution>  
                        <id>pre-process-classes</id>  
                        <phase>compile</phase>  
                        <goals>  
                            <goal>jar</goal>  
                        </goals>  
                        <configuration>  
                            <classifier>pre-process</classifier>  
                        </configuration>  
                    </execution>  
                </executions>  
            </plugin>  
        </plugins>  
    </pluginManagement>  
    ...  
</build>  

In the sub pom, we only need to configure:

<build>  
    ...  
    <plugins>  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-jar-plugin</artifactId>  
        </plugin>  
    </plugins>  
    ...  
</build>  

This greatly simplifies the configuration of children's pom

114 original articles published, praised by 130, visited 900000+
His message board follow

Tags: Maven Apache xml

Posted on Fri, 06 Mar 2020 20:40:06 -0800 by Fergal Andrews