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

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

Related

includeSystemScope Parameter in pom.xml not working

I'm tring to include a custom jar in my Spingboot application. In my case the additional jar contains a custom font for jasper Report.
This is my "system" decendency
<dependency>
<groupId>jasperFontOverrides</groupId>
<artifactId>jasperFontOverrides</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/MyriadPro.jar</systemPath>
</dependency>
...
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
The system package is visible while I debug my application in my IDE but when I'm done and I what to generate the package for production deploy
mvn install -DskipTests
My system package is not included the final jar.
Is there anything missing in my maven configuration?
If this is a multi-module project, you probably need to define the configuration section in the parent pom.
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
I have spent almost a day debugging this and it seems the configuration set in the child pom is not enough to get this to work. My guess is that the dependencies are calculated early in the build and the includeSystemScope option set in the child module is processed too late to be applied.

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

Maven Compiler Plugin

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).

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.

two plugins difference in maven's pom

Maybe at the beginning that question is stupid and i get minuses but..
please tell me which one plugin code should i use ?
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
or
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<!-- put your configurations here -->
</configuration>
</plugin>
</plugins>
The groupId is optional since org.apache.maven.plugins is the default
groupID, yet I would recommend keeping it around anyway
You should always use the version tag to pin down the plugin
version and avoid surprises regarding regressions introduced in newer
plugins.
If you are sure about the JDK you develop for, you should definitely
specify the source and target version
So use a combination of the above IMHO
You should use the second one, especially if you use maven 3.
It is a good practice to explicitly specify the groupId so that there is no confusion. Some plugins are available from org.codehaus.mojo, for instance. Maven does resolve the plugin as documented here.
version is relevant if you are using maven3, as described here.
You would still specify the source/target versions in either case, if you want it to be different from the jdk you are using.

Resources