Maven Compiler Plugin - maven

I know Maven compiler plugin by DEFAULT is bind to :
compile
test compile
life cycles, in general without specifying addition configuration, we don't have to
explicitly define it in our POM, but I still seen experienced developer putting things like
this in their POM, e.g
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
I wander what's the point? and why is he doing this?

For the shade plugin, he is probably using POM inheritance. Look in the parent POM hierarchy for a pluginManagement section, there is probably shade plugin configuration there that he is pulling into this module.
For the compiler plugin, I do not know. You are correct, for jar/war/ear/ejb projects Maven will pull in the compiler config automatically, even if he has defined specific configuration in a parent POM's pluginManagement section.

If they put such things in their pom's they don't understand Maven. You should define the version of the plugins your are using in your build. This is done by:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin<artifactId>
<version>3.1</version>
<configuration>
<target>1.6</target>
<source>1.6</source>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin<artifactId>
<version>2.3</version>
</plugin>
</plugins>
</pluginManagement>
</build>
This should be usually located into a pom file known under Company POM which defines versions of plugins for projects within a company.
Furthermore based on the life-cycle definition in Maven the Maven Super POM which contains the default bindings there you could see that particular plugins versions are defined.This means if you upgrade your Maven version you start using different plugin versions which is fact not the best related to build reproducibility. So the best practice is to define all used plugins like here as an example.. Based on the definition you shouldn't need to mention anything in your build-tag area if you have a defined packaging type (This is one of those Convention Over Configuration paradigm hints).

Related

when to use <build> plugin in maven pom.xml?

In our project, we have configured jetty inside build plugin in pom, i want to understand configuration settings in pom/ what and all we can configure in pom.
what is <build><plugin> section in pom, when to use.
difficult to understand from tutorials because lot of different examples which is making confuse.
Please can somebody explain for the above in detail?
Plugins defined in your buildsection plugins tag will be executed during the build of your project.
There are many plugins that do something with your build.
For example the maven-compiler-plugin which allows you to set the Java version for your project.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
a list of maven build plugins supported by maven itself

Should pluginManagement be used in submodules?

I am reading sonatype's tutorial on multimodule projects and I see that in submodule they use the <pluginManagement> configuration like this:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
This confuses me as I thought that <pluginManagement> is to be used in parent POM to provide common plugin configuration for submodules. What are the reasons for using plugin managment in children poms ?
Most of the time <pluginManagement> is used in parent pom files.
It configures plugins. But makes them not active part of the build. Therefor you need to add them to the <plugins> part of the Maven pom file. Most not done in parent pom files, but done in the (sub) modules, using / refering to that (parent) pom.
I think this is special to the use of the surefire plugin.
Since you execute the plugin with mvn test the surefire plugin will be executed with the configuration specified in the pluginManagement block. Other plugins are not executed directly but by binding them to another lifecycle phase and thus must be specified in the plugin block.
See the usage page of the surefire plugin.

maven-jar-plugin vs maven-jar dependency

I am trying to run standalone java application with maven and to add maven-jar-plugin.
here is my pom.xml
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<type>maven-plugin</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
</plugin>
</plugins>
</build>
how could this plugin be added as dependency and plugin? how can I use this as dependency?
thanks
Well, a <dependency>in <dependencyManagement> section allows you to specify plugins versions in one place and then inherit it across several (children) modules simply by name, w/o further version specification, thus simplifying updating a plugin. A dependency is, roughly, a jar in your application classpath.
A <plugin> in <build><pluginManagement> allows to create a configuration of the plugin shared across several (children) modules. The plugin is used by maven itself to perform its tasks, so it's, frankly speaking, a jar in maven classpath. There could be also also <reporting> section for maven reporting, etc.
A <plugin> entry in <build> turns on this plugin for a particular module. Usually it's specified w/o version since its inherited. Also plugin configuration inherited from the parent (or from <pluginManagement> section within this pom.xml) can be overridden here.

Difference between 'plugin' section and 'dependency' section in pom. Which one to use when?

While searching for maven-check-style plugin information online I found that it can be added both as a <dependency> tag like this:
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.5</version>
</dependency>
and also under <plugins> tag like this:
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>config/sun_checks.xml</configLocation>
</configuration>
</plugin>
</plugins>
</reporting>
I would like to know the difference between each and which one to use when. Please guide.
As far as I know, plugins are also artifacts, so they can be added as a dependency to a project. However adding plugin artifact as a dependency, doesnt bind its execution to any phase of maven build, therefore it cannot be executed.
Here you can find some answers:
https://www.quora.com/In-Maven-what-is-the-difference-between-dependency-and-plugins
"A plugin is an extension to Maven, something used to produce your artifact (maven-jar-plugin for an example, is used to, you guess it, make a jar out of your compiled classes and resources).
A dependency is a library that is needed by the application you are building, at compile and/or test and/or runtime time."Olivier Demeijer

Maven - Parent Pom - Child Inheritance

I am attempting to make a maven parent pom setup where I don't have to declare any plugin information in my child pom, everything is taken from the parent pom.
I essentially have it working where I've put all my plugins with there configurations in to the parent pom. Then in the child poms I have to declare the plugin still, but without the version and configuration information.
I don't want to have to declare the plugin in the child at all. This way I can add new features (such as pmd, freebugs, etc) to my parent pom and all my projects now have them working. How can I accomplish this?
Parent Pom
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.0</version>
<inherited>true</inherited>
<configuration>
<providerImplementations>
<cvs>cvs_native</cvs>
</providerImplementations>
<systemProperties>
<property>
<name>maven.scm.perforce.clientspec.name</name>
<value>${perforceClientSpec}</value>
</property>
</systemProperties>
</configuration>
</plugin>
Child Pom still needs this but I don't want to have to do this if I can avoid it:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
</plugin>
<pluginManagement> section is intended to configure project builds that inherit from this one. However, this only configures plugins that are actually referenced within the plugins element in the children (so you have to explicitly specify them, as you indicated). See more here.
If you want to avoid this, you can put this information into <build> section like this:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.0</version>
<configuration>
<...>
</configuration>
<executions>
<...>
</executions>
</plugin>
</plugins>
</build>
Instead of using pluginManagement, try using just <plugins> tag. It should be auto inherited. You may optionally override configuration in child pom. Check that by mvn help:effective-pom
You can't avoid naming the plugin in the child pom, cause how should maven know which plugin are you using. The pluginManagement section is intended for defining the versions of plugin furthermore it's used to define a default configuration.

Resources