Ilya Kozhevnikov
Ilya Kozhevnikov

Reputation: 10432

Maven plugin configuration vs properties

What are pros and cons of configuring Maven plugins through properties as oppose to configuration?

For example, maven-compiler-plugin documentation explicitly shows configuring source and target as shown below, presumably going even further with pluginManagement.

https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.4</source>
          <target>1.4</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

Wouldn't it be more succinct to use user properties instead, with no dependency on specific version?

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

Upvotes: 0

Views: 1537

Answers (3)

Jesse Glick
Jesse Glick

Reputation: 25491

First of all, many parameters of many goals do not have any associated user property, so can only be set via <configuration>.

For those which do have a user property (such as these examples), it depends. A few user properties are specifically coördinated between multiple goals or even plugins, as @khmarbaise points out.

User properties can be overridden from the command line, as @kdoteu points out, which is useful in certain cases—though very likely not for Java source/target level which would normally be intrinsic to the project (something you would change in a versioned commit alongside source file changes). User properties can also be overridden from external profiles such as in settings.xml, which is occasionally important.

On the other hand, plugin configuration is more explicit: it is expressly associated with a particular goal (or all goals in a plugin using the same parameter name). Some IDEs can offer code completion. You need not worry about accidentally having some other unrelated plugin interpret a property name (though plugin authors try to use unique-looking prefixes for most user property names). Surprisingly, Maven (3.8.1) will not fail the build if you mistype the parameter name, however—it will just quietly ignore the extra element.

Upvotes: 1

khmarbaise
khmarbaise

Reputation: 97567

There are some properties which are automatically taken by plugins. One for example are the given target/source information. An other is the project.build.sourceEncoding which is taken into account of several plugins like maven-compiler-plugin, maven-resources-plugin etc. So it makes sense to use properties which reduces the size and number of your configurations for plugins.

Upvotes: 0

kdoteu
kdoteu

Reputation: 1557

You can influence the properties druing build time with commandline parameters. And you can use them in multimodule projects. So wie are using them to configure findbugs or some urls for deploying.

Upvotes: 0

Related Questions