Maven和POM.xml中的插件

时间:2020-12-08 23:55:57

I just started using Maven and I read that plugins are additional components that can be used.
A typical structure of pom.xml file is

我刚开始使用Maven,我读到插件是可以使用的附加组件。 pom.xml文件的典型结构是

<project>
  <groupId>org.koshik.javabrains</groupId>
  <artifactId>JarName</artifactId> (A fldernamed JarName was created) 
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>JarName</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Question: Where should I insert a plugin tag? such as the following:

问题:我应该在哪里插入插件标签?如下:

<plugin>
  <groupId>org.jibx</groupId>
  <artifactId>jibx-maven-plugin</artifactId>
  <version>1.2.4</version>
  <executions>
    <execution>
      <goals>
        <goal>bind</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Before the dependency or after the dependency tag? Does it matter?

在依赖之前或依赖标记之后?有关系吗?

6 个解决方案

#1


43  

<project>
    <groupId>org.koshik.javabrains</groupId>
    <artifactId>JarName</artifactId> (A fldernamed JarName was created) 
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>JarName</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jibx</groupId>
                <artifactId>jibx-maven-plugin</artifactId>
                <version>1.2.4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>bind</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

You can also place plugins in the <build> section of <profile> if you use maven profiles. The order doesn't matter.

如果使用maven配置文件,也可以将插件放在 部分中。订单无关紧要。

#2


8  

A late clarification on two important points

关于两个要点的最后澄清

Where to place plugin
A plugin should indeed be added in most of the cases within the build/plugins section, however there is an important difference between placing it within plugins against placing it within pluginManagement/plugins.

放置插件的位置在构建/插件部分的大多数情况下确实应该添加插件,但是将它放在插件中与将其放在pluginManagement / plugins中之间存在重要区别。

This misunderstanding is often the cause of a non invoked plugin in Maven or an harder troubleshooting:

这种误解通常是Maven中未调用插件的原因或更难以排除的故障:

  • Plugins under build/plugins are directly part of the default Maven build, if they specify an execution or if they configure something for the default build (see below)
  • 构建/插件下的插件直接是默认Maven构建的一部分,如果它们指定执行或者它们为默认构建配置了什么(见下文)
  • Plugins under build/pluginManagement/plugins are not necessarely part of the default Maven build, that is, is a management, it's an hint to maven: it you happen to use this plugin, then please use the version, the configuration, the executions I specify here, in this management.

    build / pluginManagement / plugins下的插件不一定是默认Maven构建的一部分,也就是说,它是一个管理,它是maven的一个提示:你碰巧使用这个插件,然后请使用版本,配置,执行我在此管理中指定此处。

    But what happen to use means? Means: if the same plugin is also present in the build/plugins section, then apply this management (and only then it will be effective); or if the plugin is invoked by default by Maven, then also apply it.

    但是使用的意思是什么?意思是:如果构建/插件部分中也存在相同的插件,则应用此管理(只有这样才有效);或者如果Maven默认调用插件,那么也应用它。

    But how is a plugin invoked by default? That's part of the main philosophy behind maven: convention over configuration. By convention, when you specify a certain packaging (default jar, but it can be war for example), you want certain plugins to be invoked. To build a jar, by default invoke the maven-jar-plugin; to build a war, by default invoke the maven-war-plugin and so on. So, if you specify a plugin configuration in the build/pluginManagement/plugin for a plugin which has a default binding to the Maven build, then it will be also be used.

    但是默认情况下如何调用插件?这是maven背后的主要哲学的一部分:约定优于配置。按照惯例,当您指定某个包装(默认jar,但它可以是war)时,您希望调用某些插件。要构建一个jar,默认情况下会调用maven-jar-plugin;建立一个战争,默认情况下调用maven-war-plugin等等。因此,如果您在build / pluginManagement / plugin中为插件指定了一个插件配置,该插件具有与Maven构建的默认绑定,那么它也将被使用。

Ordering
Concerning the ordering of sections within the pom.xml file, a further clarification is required: it's indeed irrelevant in most of the cases, however the order of plugin element wihtin the build/plugins section may be important. Since Maven 3.0.3 (MNG-2258), different plugin executions attached to the same Maven phase will be invoked in their order of declaration in the pom.xml file. That is, ordering is important in this case, since it may affect the behavior of the build.

排序关于pom.xml文件中各节的排序,需要进一步说明:在大多数情况下它确实无关紧要,但是插件元素与构建/插件部分的顺序可能很重要。从Maven 3.0.3(MNG-2258)开始,附加到同一Maven阶段的不同插件执行将按照pom.xml文件中的声明顺序调用。也就是说,在这种情况下,排序很重要,因为它可能会影响构建的行为。

Additionally, also order of dependency declarations may affect your build towards Dependency Mediation, that is, the first declared dependency wins in case of conflict against a transitive dependency. So, once again, ordering is important in certain cases.

此外,依赖声明的顺序也可能会影响您对依赖性中介的构建,也就是说,在与传递依赖性冲突的情况下,第一个声明的依赖性获胜。因此,在某些情况下,订购再次非常重要。

Last but not least, although ordering is not important for other sections of the pom.xml file, good habit is to follow official Maven recommendations and, as a simplified version, follow this order of declaration:

最后但并非最不重要的是,尽管排序对于pom.xml文件的其他部分并不重要,但良好的习惯是遵循官方的Maven建议,并且作为简化版本,遵循以下声明顺序:

<project>
  <modelVersion/>

  <parent/>

  <groupId/>
  <artifactId/>
  <version/>
  <packaging/>

  <properties/>

  <dependencyManagement/>
  <dependencies/>

  <build/>

  <reporting/>

  <profiles/>
</project>

The sortpom-maven-plugin can also be used to automatically apply this standard ordering, simply invoking the following on the concerned pom.xml file:

sortpom-maven-plugin也可用于自动应用此标准排序,只需在相关的pom.xml文件上调用以下内容:

mvn com.github.ekryd.sortpom:sortpom-maven-plugin:2.5.0:sort \
     -Dsort.keepBlankLines -Dsort.predefinedSortOrder=recommended_2008_06 

For further reading:

进一步阅读:

#3


4  

<plugin>should be placed into <plugins> section which should be placed into <build> or <pluginManagement> section. The order of <dependency> or <build> section doesn't matter.

应将 放入 部分,该部分应放在 部分中。 部分的顺序无关紧要。

The full reference about pom.xml is here: http://maven.apache.org/pom.html

有关pom.xml的完整参考资料,请访问:http://maven.apache.org/pom.html

#4


3  

If you want to use the plugin for build you can use the below structure.

如果要使用该插件进行构建,可以使用以下结构。

<project>
 <build>
  <plugins>
  </plugins>
 </build>
</project>

#5


2  

You can insert your second snippet anywhere in the pom.xml file between two <plugins> </plugins> tags.

您可以将第二个代码段插入两个 标记之间的pom.xml文件中的任何位置。

#6


2  

Sections order in POM doesn't matter. In general, there are build plugins and reporting plugins in Maven. Your case is to use build plugin so you have to put this <plugin> block into <project><build><plugins>... section.

POM中的章节顺序无关紧要。通常,Maven中有构建插件和报告插件。您的情况是使用构建插件,因此您必须将此 块放入 ...部分。

Look at this for some basics about plugins.

看看这个有关插件的一些基础知识。

#1


43  

<project>
    <groupId>org.koshik.javabrains</groupId>
    <artifactId>JarName</artifactId> (A fldernamed JarName was created) 
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>JarName</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jibx</groupId>
                <artifactId>jibx-maven-plugin</artifactId>
                <version>1.2.4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>bind</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

You can also place plugins in the <build> section of <profile> if you use maven profiles. The order doesn't matter.

如果使用maven配置文件,也可以将插件放在 部分中。订单无关紧要。

#2


8  

A late clarification on two important points

关于两个要点的最后澄清

Where to place plugin
A plugin should indeed be added in most of the cases within the build/plugins section, however there is an important difference between placing it within plugins against placing it within pluginManagement/plugins.

放置插件的位置在构建/插件部分的大多数情况下确实应该添加插件,但是将它放在插件中与将其放在pluginManagement / plugins中之间存在重要区别。

This misunderstanding is often the cause of a non invoked plugin in Maven or an harder troubleshooting:

这种误解通常是Maven中未调用插件的原因或更难以排除的故障:

  • Plugins under build/plugins are directly part of the default Maven build, if they specify an execution or if they configure something for the default build (see below)
  • 构建/插件下的插件直接是默认Maven构建的一部分,如果它们指定执行或者它们为默认构建配置了什么(见下文)
  • Plugins under build/pluginManagement/plugins are not necessarely part of the default Maven build, that is, is a management, it's an hint to maven: it you happen to use this plugin, then please use the version, the configuration, the executions I specify here, in this management.

    build / pluginManagement / plugins下的插件不一定是默认Maven构建的一部分,也就是说,它是一个管理,它是maven的一个提示:你碰巧使用这个插件,然后请使用版本,配置,执行我在此管理中指定此处。

    But what happen to use means? Means: if the same plugin is also present in the build/plugins section, then apply this management (and only then it will be effective); or if the plugin is invoked by default by Maven, then also apply it.

    但是使用的意思是什么?意思是:如果构建/插件部分中也存在相同的插件,则应用此管理(只有这样才有效);或者如果Maven默认调用插件,那么也应用它。

    But how is a plugin invoked by default? That's part of the main philosophy behind maven: convention over configuration. By convention, when you specify a certain packaging (default jar, but it can be war for example), you want certain plugins to be invoked. To build a jar, by default invoke the maven-jar-plugin; to build a war, by default invoke the maven-war-plugin and so on. So, if you specify a plugin configuration in the build/pluginManagement/plugin for a plugin which has a default binding to the Maven build, then it will be also be used.

    但是默认情况下如何调用插件?这是maven背后的主要哲学的一部分:约定优于配置。按照惯例,当您指定某个包装(默认jar,但它可以是war)时,您希望调用某些插件。要构建一个jar,默认情况下会调用maven-jar-plugin;建立一个战争,默认情况下调用maven-war-plugin等等。因此,如果您在build / pluginManagement / plugin中为插件指定了一个插件配置,该插件具有与Maven构建的默认绑定,那么它也将被使用。

Ordering
Concerning the ordering of sections within the pom.xml file, a further clarification is required: it's indeed irrelevant in most of the cases, however the order of plugin element wihtin the build/plugins section may be important. Since Maven 3.0.3 (MNG-2258), different plugin executions attached to the same Maven phase will be invoked in their order of declaration in the pom.xml file. That is, ordering is important in this case, since it may affect the behavior of the build.

排序关于pom.xml文件中各节的排序,需要进一步说明:在大多数情况下它确实无关紧要,但是插件元素与构建/插件部分的顺序可能很重要。从Maven 3.0.3(MNG-2258)开始,附加到同一Maven阶段的不同插件执行将按照pom.xml文件中的声明顺序调用。也就是说,在这种情况下,排序很重要,因为它可能会影响构建的行为。

Additionally, also order of dependency declarations may affect your build towards Dependency Mediation, that is, the first declared dependency wins in case of conflict against a transitive dependency. So, once again, ordering is important in certain cases.

此外,依赖声明的顺序也可能会影响您对依赖性中介的构建,也就是说,在与传递依赖性冲突的情况下,第一个声明的依赖性获胜。因此,在某些情况下,订购再次非常重要。

Last but not least, although ordering is not important for other sections of the pom.xml file, good habit is to follow official Maven recommendations and, as a simplified version, follow this order of declaration:

最后但并非最不重要的是,尽管排序对于pom.xml文件的其他部分并不重要,但良好的习惯是遵循官方的Maven建议,并且作为简化版本,遵循以下声明顺序:

<project>
  <modelVersion/>

  <parent/>

  <groupId/>
  <artifactId/>
  <version/>
  <packaging/>

  <properties/>

  <dependencyManagement/>
  <dependencies/>

  <build/>

  <reporting/>

  <profiles/>
</project>

The sortpom-maven-plugin can also be used to automatically apply this standard ordering, simply invoking the following on the concerned pom.xml file:

sortpom-maven-plugin也可用于自动应用此标准排序,只需在相关的pom.xml文件上调用以下内容:

mvn com.github.ekryd.sortpom:sortpom-maven-plugin:2.5.0:sort \
     -Dsort.keepBlankLines -Dsort.predefinedSortOrder=recommended_2008_06 

For further reading:

进一步阅读:

#3


4  

<plugin>should be placed into <plugins> section which should be placed into <build> or <pluginManagement> section. The order of <dependency> or <build> section doesn't matter.

应将 放入 部分,该部分应放在 部分中。 部分的顺序无关紧要。

The full reference about pom.xml is here: http://maven.apache.org/pom.html

有关pom.xml的完整参考资料,请访问:http://maven.apache.org/pom.html

#4


3  

If you want to use the plugin for build you can use the below structure.

如果要使用该插件进行构建,可以使用以下结构。

<project>
 <build>
  <plugins>
  </plugins>
 </build>
</project>

#5


2  

You can insert your second snippet anywhere in the pom.xml file between two <plugins> </plugins> tags.

您可以将第二个代码段插入两个 标记之间的pom.xml文件中的任何位置。

#6


2  

Sections order in POM doesn't matter. In general, there are build plugins and reporting plugins in Maven. Your case is to use build plugin so you have to put this <plugin> block into <project><build><plugins>... section.

POM中的章节顺序无关紧要。通常,Maven中有构建插件和报告插件。您的情况是使用构建插件,因此您必须将此 块放入 ...部分。

Look at this for some basics about plugins.

看看这个有关插件的一些基础知识。