For more information about pom file, it is practical

http://ifeve.com/maven-pom/amp/

What is POM?

POM (project object model) contains configuration information and detail engineering projects, Maven POM file to use to build the project. POM file contains most of the default value engineering. For example, target is the default build directory, src / main / java is the default source directory, src / test / java is the default test source directory, and so on.

Maven2 in pom.xml is Maven1 in project.xml. Compared to contain executable goal in maven.xml, and now goals and plugins can be configured in pom.xml. When performing a task or goal, Maven pom.xml will look for and read to obtain configuration information, and then execute goal in the current directory.

The statement can be configured in pom.xml including engineering dependent (project dependencies), plug-ins (plugins), executable target (goals), build configuration (build profiles) and so on. Other information, such as engineering version, description, developers, mailing lists, etc. can also be declared in pox.xml in.

 

Super POM

Super POM is Maven POM file by default, unless you declare inheritance shown otherwise, all of POM files are expanded on the basis of Super POM, that is to say, the Super POM configuration will be created in your project the pom.xml inheritance. The Super POM Maven 2.0.x code is as follows:

<project>
  <modelVersion>4.0.0</modelVersion>
  <name>Maven Default Project</name>
 
  <repositories>
    <repository>
      <id>central</id>
      <name>Maven Repository Switchboard</name>
      <layout>default</layout>
      <url>http://repo1.maven.org/maven2</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>
 
  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Maven Plugin Repository</name>
      <url>http://repo1.maven.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
  </pluginRepositories>
 
  <build>
    <directory>target</directory>
    <outputDirectory>target/classes</outputDirectory>
    <finalName>${artifactId}-${version}</finalName>
    <testOutputDirectory>target/test-classes</testOutputDirectory>
    <sourceDirectory>src/main/java</sourceDirectory>
    <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>src/test/java</testSourceDirectory>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>src/test/resources</directory>
      </testResource>
    </testResources>
  </build>
 
  <reporting>
    <outputDirectory>target/site</outputDirectory>
  </reporting>
 
  <profiles>
    <profile>
      <id>release-profile</id>
 
      <activation>
        <property>
          <name>performRelease</name>
        </property>
      </activation>
 
      <build>
        <plugins>
          <plugin>
            <inherited>true</inherited>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
 
            <executions>
              <execution>
                <id>attach-sources</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
 
            <executions>
              <execution>
                <id>attach-javadocs</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
 
            <configuration>
              <updateReleaseInfo>true</updateReleaseInfo>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
 
</project>

The following is Maven 2.1.x of Super POM:

<project>
  <modelVersion>4.0.0</modelVersion>
  <name>Maven Default Project</name>
 
  <repositories>
    <repository>
      <id>central</id>
      <name>Maven Repository Switchboard</name>
      <layout>default</layout>
      <url>http://repo1.maven.org/maven2</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>
 
  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Maven Plugin Repository</name>
      <url>http://repo1.maven.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
  </pluginRepositories>
 
  <build>
    <directory>${project.basedir}/target</directory>
    <outputDirectory>${project.build.directory}/classes</outputDirectory>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
    <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
    <!-- TODO: MNG-3731 maven-plugin-tools-api < 2.4.4 expect this to be relative... -->
    <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
    <resources>
      <resource>
        <directory>${project.basedir}/src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>${project.basedir}/src/test/resources</directory>
      </testResource>
    </testResources>
   <pluginManagement>
       <plugins>
         <plugin>
           <artifactId>maven-antrun-plugin</artifactId>
           <version>1.3</version>
         </plugin>       
         <plugin>
           <artifactId>maven-assembly-plugin</artifactId>
           <version>2.2-beta-2</version>
         </plugin>         
         <plugin>
           <artifactId>maven-clean-plugin</artifactId>
           <version>2.2</version>
         </plugin>
         <plugin>
           <artifactId>maven-compiler-plugin</artifactId>
           <version>2.0.2</version>
         </plugin>
         <plugin>
           <artifactId>maven-dependency-plugin</artifactId>
           <version>2.0</version>
         </plugin>
         <plugin>
           <artifactId>maven-deploy-plugin</artifactId>
           <version>2.4</version>
         </plugin>
         <plugin>
           <artifactId>maven-ear-plugin</artifactId>
           <version>2.3.1</version>
         </plugin>
         <plugin>
           <artifactId>maven-ejb-plugin</artifactId>
           <version>2.1</version>
         </plugin>
         <plugin>
           <artifactId>maven-install-plugin</artifactId>
           <version>2.2</version>
         </plugin>
         <plugin>
           <artifactId>maven-jar-plugin</artifactId>
           <version>2.2</version>
         </plugin>
         <plugin>
           <artifactId>maven-javadoc-plugin</artifactId>
           <version>2.5</version>
         </plugin>
         <plugin>
           <artifactId>maven-plugin-plugin</artifactId>
           <version>2.4.3</version>
         </plugin>
         <plugin>
           <artifactId>maven-rar-plugin</artifactId>
           <version>2.2</version>
         </plugin>        
         <plugin>                
           <artifactId>maven-release-plugin</artifactId>
           <version>2.0-beta-8</version>
         </plugin>
         <plugin>                
           <artifactId>maven-resources-plugin</artifactId>
           <version>2.3</version>
         </plugin>
         <plugin>
           <artifactId>maven-site-plugin</artifactId>
           <version>2.0-beta-7</version>
         </plugin>
         <plugin>
           <artifactId>maven-source-plugin</artifactId>
           <version>2.0.4</version>
         </plugin>         
         <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.4.3</version>
         </plugin>
         <plugin>
           <artifactId>maven-war-plugin</artifactId>
           <version>2.1-alpha-2</version>
         </plugin>
       </plugins>
     </pluginManagement>
  </build>
 
  <reporting>
    <outputDirectory>${project.build.directory}/site</outputDirectory>
  </reporting>
  <profiles>
    <profile>
      <id>release-profile</id>
 
      <activation>
        <property>
          <name>performRelease</name>
          <value>true</value>
        </property>
      </activation>
 
      <build>
        <plugins>
          <plugin>
            <inherited>true</inherited>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-sources</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-javadocs</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <configuration>
              <updateReleaseInfo>true</updateReleaseInfo>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
 
</project>

Minimize POM

A minimal requirement POM file is as follows:

  • project label as the top label
  • modelVersion - should be set 4.0.0
  • Unique id project development team - groupId
  • artifactId - workpiece (artifact) or engineering (projrct) unique id
  • version - the version number

Here is an example to minimize the POM:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>
</project>

In the POM file need to declare groupId, artifactId and version. These three values ​​<groupId>: <artifactId>: <version> in the form of statements that make up the complete name of the project. Such as the example above, the full name is "com.mycompany.app:my-app1".

As first said, if the configuration details are not displayed settings, Maven will use the default configuration inherited from the Super POM. Type (packaging type) one of the default value is in packets, each packet has a Maven project types, if not set in the POM, the default is "jar".

Previous Minimal POM, where  repositories  this value is not set, if you use the minimal POM to build your project, it will use the Super POM inheritance of  repositories  value (http://repo.maven.apache.org/maven2), when Maven POM find rely on, and it will go to this address to download dependencies.

Project inheritance

POM element can be configured as follows:

  • Dependent on (dependencies)
  • Developers and contributors (developers and contributors)
  • Plug-in list, including the report (plugin lists, including reports)
  • Id corresponding plug-execution (plugin executions with matching ids)
  • Plugin configuration (plugin configuration)
  • Resources (resources)

Super POM is an example of a project inherited. You can also be used as the basis by specifying the parent element in the POM to import your own POM. As in the following example:

Example 1

scene

In this example, we still stick com.mycompany.app:my-app:1 name. Now let's introduce another piece, com.mycompany.app: my-module: 1.

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-module</artifactId>
  <version>1</version>
</project>

Specify their directory structure is as follows:

.
|-- my-module
| `-- pom.xml
`-- pom.xml

Note:  My-Module / pom.xml is com.mycompany.app:my-module:1 POM file, and pom.xml is com.mycompany.app:my-app:1 the POM file.

solution

Now, if we com.mycompany.app:my-app:1 designated as com.mycompany.app:my-module:1 Father of the workpiece (parent artifact), we need to modify com.mycompany.app:my-module: POM file 1 are as follows:

<project>
  <parent>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-module</artifactId>
  <version>1</version>
</project>

Note that we need to add a parent node, this node allows us to specify the current parent POM POM. By specifying the full name of the parent POM (ie groupId, artifactId, version three tags), our module (module) can inherit the properties of the parent POM.

In addition, if we want the same version groupId and modules and their parents, you can remove the current modules in the POM groupId and version label.

<project>
  <parent>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <artifactId>my-module</artifactId>
</project>

Doing so can make the current module integration and groupid parent POM's version.

Example 2

scene

In the parent project has been installed in a local warehouse or specified directory structure (parent POM to the parent directory of the module POM) when in, you can do so too.

But if the parent works or is not installed directory institution like it?

.
|-- my-module
|   `-- pom.xml
`-- parent
`-- pom.xml

solution

To correct this directory structure, we will add <relativePath> element in the parent node.

<project>
  <parent>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1</version>
    <relativePath>../parent/pom.xml</relativePath>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <artifactId>my-module</artifactId>
</project>

As the name suggests, this element specifies a relative path from the parent to the POM POM module.

Engineering polymer

Engineering polymer engineering and inheritance is very similar, but not specified parent POM from sub-modules, but from a parent POM designated sub-module. To do so, the parent project will know that there is a sub-module, and if Maven command called from the parent project, but also the smooth implementation of the sub-module. The practice of engineering polymer requirements are as follows:

  • The parent POM's packageing property to "pom"
  • Specified directory module (sub POM) in the parent POM

Example 3

scene

Or the last POM and directory structure

com.mycompany.app:my-app:1’s POM

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>
</project>

com.mycompany.app:my-module:1’s POM

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-module</artifactId>
  <version>1</version>
</project>

directory structure

.
|-- my-module
|   `-- pom.xml
`-- pom.xml

solution

If we're going to my-module aggregated into my-app, we only need to modify my-app.

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>
  <packaging>pom</packaging>
 
  <modules>
    <module>my-module</module>
  </modules>
</project>

In the POM modified, increasing the packaging and modular portions, packaging value is set to "pom", increasing the section module <module> my-module </ module>. <Module> value is the relative path of the POM com.mycompany.app:my-module:1 com.mycompany.app:my-app:1 to (in practice, as we artifactId module directory name).

Now, when Maven commands are executed in com.mycompany.app:my-app:1, the same obviously also executed in com.mycompany.app:my-module:1 in. In addition, some commands (goals soecifically) in a different manner in the case of engineering polymer.

Example 4

scene

If we change this directory structure

.
|-- my-module
|   `-- pom.xml
`-- parent
`-- pom.xml

How should a parent POM designated sub-module it?

solution

The answer is - as with Example 3, specify the path just fine.

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>
  <packaging>pom</packaging>
 
  <modules>
    <module>../my-module</module>
  </modules>
</project>

Inheritance and engineering engineering polymer

If you have several Maven projects, and these projects have similar configuration, you can configure the same by placing a parent project to reconstruct. In this case, you have to do is to let your Maven project inherit the parent project, which will be able to configure common in all the projects.

If you have a project to build a group together and run, you can create a parent project, and declare these projects in the works for its parent module, to do so, as long as you build the parent project, sub-project will also build.

Of course, you can do simultaneous engineering works inheritance and aggregation. This means that you can specify a parent works for all of your modules, while the parent project can be specified in the rest of the Maven project for its sub-modules. You only need to apply these three rules:

  • Specify their parent POM POM in all child.
  • The value is set to "pom" packaging elements of the parent POM.
  • Submodule specified directory (sub POM) in the parent POM.

Example 5

scene

Or the last POM,

com.mycompany.app:my-app:1’s POM

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>
</project>

com.mycompany.app:my-module:1’s POM

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-module</artifactId>
  <version>1</version>
</project>

Directory Structure

.
|-- my-module
|   `-- pom.xml
`-- parent
`-- pom.xml

solution

Inheritance while doing engineering and engineering polymer, as long as you apply the three rules.

com.mycompany.app:my-app:1’s POM

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>
  <packaging>pom</packaging>
 
  <modules>
    <module>../my-module</module>
  </modules>
</project>

com.mycompany.app:my-module:1’s POM

<project>
  <parent>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1</version>
    <relativePath>../parent/pom.xml</relativePath>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <artifactId>my-module</artifactId>
</project>

Note : The same configuration inherited policies and POM inherited policies

Engineering variables and rewrite

Maven encourages the practice is not to reinvent the wheel (do not repeat yourself). But there are always using the same property in different places. In order to ensure that the property value of the specified time, Maven allows you to use your own pre-defined variable or variables in POM.

For example, in order to use project.version this variable, so you can reference:

<version>${project.version}</version>

Note that these variables will be processed after the inheritance. This means that if a parent project to use a variable that define and are defined in the sub-project in the parent project will be different, that was last used.

Available variables

Engineering model variables

A Model of any field is a separate element of the value of the variable can be used as reference. For example, $ {project.groupId}, $ {project.version}, $ {project.build.sourceDirectory} and so on. Reference POM reference include all the properties. These variables are used "project" prefix it. You can look at pom references prefixed, or completely omit the prefix - these forms are now obsolete no longer used.

Special Variables

project.basedir The directory where the current project
project.baseUri The current project directory is located, expressed as after a URI, Maven 2.1.0
maven.build.timestamp After a time stamp indicating the start time to build, Maven 2.1.0-M1

Construction timestamp format can customize maven.build.timestamp.format properties, for example:

<project>
  ...
  <properties>
    <maven.build.timestamp.format>yyyy-MM-dd'T'HH:mm:ss'Z'</maven.build.timestamp.format>
  </properties>
  ...
</project>

Formatting mode must comply with the rules given in the API documentation. If this attribute is not present, the default value is the value given in the example

Attributes

You can also define any property in the project as a variable reference, look at the following examples:

<project>
  ...
  <properties>
    <mavenVersion>2.1</mavenVersion>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-artifact</artifactId>
      <version>${mavenVersion}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-project</artifactId>
      <version>${mavenVersion}</version>
    </dependency>
  </dependencies>
  ...
</project>

 

 

 

 

Published 168 original articles · won praise 16 · views 90000 +

Guess you like

Origin blog.csdn.net/ajax_yan/article/details/104824586