When do I need the maven-compiler-plugin? - maven

I understand that the maven-compiler plugin is used to compile the code. Some of my project does not have that plugin in the pom file? When is it required?

I am trying to answer my own question based on what I learned since posted this question. If this answer is correct or incorrect please comment. Thanks.
If this plugin is not defined, the Maven Super POM contains all the default plugins you will be using. It works fine for small and non-serious projects. However, the best practice is to define these plugins in a company-wise POM and so that when you upgrade maven, you would not end up using a different version of plugin.

Have a look at the Goals Overview Section in Apache Maven Compiler Plugin
The Compiler Plugin has two goals. Both are already bound to their proper phases within the Maven Lifecycle and are therefore, automatically executed during their respective phases.
You only have to add it if you want to change the default parameters

Related

Gradle equivalent for Maven Java formatter that uses com.googlecode.maven-java-formatter-plugin

I'm switching a large project from Maven to Gradle. Existing Maven project uses com.googlecode.maven-java-formatter-plugin to format the Java code. Looked for the equivalent in Gradle. Found https://plugins.gradle.org/plugin/com.github.sherter.google-java-format, but it spewed a bunch of errors out, was really slow, and didn't generate the same output as the Maven plugin.
Also looked here: https://github.com/google/google-java-format - no help.
Is there a Gradle plugin which will give me same result as the Maven plugin?
There is this other Gradle plugin, which uses the Eclipse formatter. Maybe you can tweak it to achieve the same results as with the maven plugin you mentioned (I'm not using this plugin myself).
Note that it is an explicit non-goal of google-java-format and the corresponding Gradle plugin to be configurable. If you are not happy with the formatting style then this tool is probably not appropriate for you.
(if you have technical problems with my Gradle plugin, feel free to provide more details or open a ticket on Github)

Maven plugins and resource substitution

I have a regular requirement to execute several goals in Maven so I decided to write a plugin for it. It seemed easiest to define a new lifecycle for this, each phase executing the relevant plugin goal. I need to pass configuration to the plugin, specifically a directory and a version number.
I found that if I use a variable in lifecycle.xml, such as ${projname.directory}, the variable appears to be resolved not at plugin compile time but at project compile time. I'm guessing that lifecycle.xml is used within the project and not touched by the plugin. Is there any reference for understanding exactly how this works?
Also, I'd like to be able to use a default directory name if the projname.directory property is not set - storing this in the plugin source somewhere. I have no idea how to go about this - is there an easy way to do it?
Thanks,
-Dave

How can I find out hidden versions of dependencies and plugins in Maven?

We do not need to specify the versions and other parameters of Maven dependencies and plugins we use. We can do it only once in parent POM or may be in some other way and inherit that properties in our children POMs.
Unfortunately for many times I have met the projects where I cannot find the real parameters of dependencies and plugins that easily. I.e. the versions of dependencies are empty, but in parent POM there is no information about this.
So my questions are:
What are all the possible ways to specify the parameters of dependency and plugin in Maven? I.e. where can I look for version number if it is not present neither in plugin in child POM nor in plugin management in parent pom?
Is there any Maven command to find out where Maven takes these parameters from? For example, I may use the dependency:tree command. It will print a list of dependencies which will help me to understand the version dependency I am stuck about. But this is a hack, and it cannot help me with plugins. May be there is something better?
Before executing any command, Maven builds the "effective POM", see this stackoverflow answer for more details. You may view the effective POM for any project by running mvn help:effective-pom. Typically when I run this command I redirect the output into a file so I can view and search it in a text editor.
You asked about specific parts of the POM, dependencies and plugins. You're on the right track, the maven-dependency-plugin helps. The goals I use most often are dependency:tree, dependency:resolve, and dependency:resolve-plugins. The first two assist with project dependencies, the last one with plugins. Add the -U option to force Maven to update dependencies regardless of the update policies/repository metadata values.
It is also helpful when troubleshooting to start with an empty local repository.
Last but certainly not least, Maven will give warnings when a build uses a plugin without a specific version. ALWAYS specify an explicit version to fix the warning and avoid issues like this one.

Cannot compile due to M2Eclipse which doesn't find connector

There is a section in the pom.xml file that requires the Maven-Replacer-Plugin and (I believe) GitDescribe.
Here are the errors it gives me:
Plugin execution not covered by lifecycle configuration: com.lukegb.mojo:gitdescribe-maven-plugin:1.3:gitdescribe (execution: default, phase: compile)
Plugin execution not covered by lifecycle configuration: com.google.code.maven-replacer-plugin:maven-replacer-plugin:1.3.8:replace (execution: default, phase: generate-resources)
This message comes up because m2e doesn't know what to do at this phase. The build should work with command line maven, but m2e wants you to specify whether you want it to run the plugin or not.
The fix depends on what you want to happen. You can either have eclipse ignore the plugin or execute it as part of the build.
The documentation explains it in further detail here, but I'm copying the easy answer below.
https://www.eclipse.org/m2e/documentation/m2e-execution-not-covered.html
This changes the pom to add some eclipse metadata. If you're just trying to get it to build this will work.
HINT: Use quick fix to create “ignore” mapping, then replace
action with . M2Eclipse 1.3 and newer assume safer
runOnIncremental=false by default. It is recommended to always
specific desired runOnIncremental value explicitly in lifecycle
mapping configuration.
You can also change your preferences in newer eclipse/m2e versions. It's the other quick fix option. You can change this one under Project Properties->Maven->Lifecycle Mapping.
This error was well described in their documentation.
This feature was added in M2Eclipse 1.0 (now m2e).
It caused much trouble, like reported in this blog post.
One solution was to pollute the pom.xml with data about the plugin lifecycle mapping metadata, but it was not very satisfying.
In m2e 1.1, plugin developers could embed a file named lifecycle-mapping-metadata.xml in their plugins to make it work in Eclipse (see M2E compatible maven plugins).
Fortunately, since m2e 1.2, things have evolved and we can now configure it in Eclipse Preferences (see Eclipse workspace lifecycle mapping metadata).

Maven plugin to check if a module's build it recent, and skip if thats the case

I have a huge multi-module project , which is being built using maven.
Most of them are core modules , which are used in other modules for various purposes.
But , 9 of 10 times, there is no change in the core modules and there is just some change in the depending modules.
Sometimes, there might be a small change in 1 core module of the many.
The issue is, I want maven to know, if the current build in target folder of the core modules is the latest accroding to the code, i.e no changes were made to the core for that.If thats the case, then I want maven to skip building that module during the maven phases.except the assembly plugin phase, which takes care of assembling all my modules at one place.
Is there a neat way to do this.I.e some maven plugin already taking care of stuff like this??
Some light on this will be great help.
Thanks,
Neeraj
May be you can use the maven incremental build plugin or use Hudson/Jenkins to do an incremtal build.
Just use Hudson - it will scan your source repository for changes and only run a build once that has occurred. It works well with Maven out of the box too.

Resources