Execute maven plugin in children, not grandchildren or parent - java

I have a maven Mojo plugin that I'd like to execute only in child pom, but not in the current pom or grandchildren poms. My plugin runs at compile time and essentially reads files, and for my use case if it runs in the child pom, it is redundant to run in grandchildren poms.
For example, I have a maven Mojo plugin that gets called by a parent pom file ParentFile. This pom file is inherited in other repositories, which I don't have write access to. In these repositories, there is a root module with a pom file ChildFile, whose parent is ParentFile. There are also other submodules in the repository that contain pom files (GrandchildrenFiles) whose parent is ChildFile.
I would like to execute my Mojo plugin from ChildFile, but not from GrandchildrenFiles or ParentFile. Is there a way for me to do this without write access to the repositories containing ChildFile and GrandchildrenFiles?

You probably cannot stop your plugin from being executed, but you could check, as first step in your plugin, if your parent is the parent you expected. For that, you can use project.getParent().
If not, you just skip the rest of the execution.

Related

There is a way to execute just a Maven subproject?

I am wondering if there is a way to run a Maven subproject alone. I mean, in several code samples you can find a group of maven projects with a common parent. There is a way to replace parent in order to be able to run just a subproject.
Lets suppose this repository https://github.com/in28minutes/spring-boot-examples
There is way to run just spring-boot-tutorial-soap-web-services/ project?
Thanks in advance.
maven projects are identified by pom.xml and you can run any maven project individually. (Any maven goal).
cd into_a_maven_project(identified by pom.xml).
mvn install(this will run install phase for the project).
There are several relationships among maven projects.
Parent-child
Submodule
Dependency
Parent-child: This relationship is used in defining a pom. For instance, When a set of pom shares a lot, you can define a parent pom(parent maven project) and reference from the child project. (reference is done from child to parent)
When you run child project, the parent project is used only to inherit the pom(copy the content of parent pom). Child pom overrides the configuration in parent pom.(child pom contains little because of the fact that required configurations copied from the parent pom)
Submodule: This relationship is used when it makes sense to build multiple projects together. In this case, you run build in the project referencing submodules. (This is not a parent child relationships, the pom is not inherited if the project is not also a parent). The result is that the submodule projects become part of the build. (The order of the build is determined by dependencies between them)
Dependency: This relationship is used when the code in a project depended by your project. The build order is calculated using this relationship.

Install third party jar in local maven before maven resolves its dependency?

I am working on a multi-module maven project and have third party jar which isn't available in central or public repository, I also even don't want to place on public repo. I am providing following plugin directive in my parent pom to install jar in my local maven repository before its dependency is resolved in child project.
Now I provide dependency in child project as;
But I build the project, it successfully adds dependency in local maven repository (places third party jar in .m2 folder) but the at the same time it gives following error. Looks like, it searches this jar file in child projects libs folder as well, while I have already placed it on root (in myproject.parent/libs).
Failed to execute goal org.apache.maven.plugins:maven-install-plugin:3.0.0-M1:install-file (install-
external-non-maven1-jar) on project myProject.core: The specified file 'C:\Users\myuser\git\MyProjectRepository\myproject.parent\myproject.core\libs\be-ixf-java-sdk-jar-with-dependencies.jar' not exists.
I already know scope and systemPath option but I don't want to use it for some reason.
Kindly help in identifying what I am missing ?
The best approach that you could have if your project have a centralized maven repo like nexus setup is to have your third party library also added to the repo. Now , you are having the bin file added to your project and it's not preferable.
If you already have the required jar under your project code in like : libs\*, then why can't you refer the dependency directly in your pom.xml instead of having to install it in your local maven repo and then use it .
<dependency>
<groupId>anything</groupId>
<artifactId>anything</artifactId>
<version>anything</version>
<scope>system</scope>
<systemPath>${basedir}/lib/jar_name.jar</systemPath>
</dependency>
providing the location of the dependency in your project directory should resolve it during build itself.Look at Maven System dependencies.
Since you do not want to change your current setup . Please bear in mind the following about maven pom structure :
Project Aggregation is similar to Project Inheritance. But instead of
specifying the parent POM from the module, it specifies the modules
from the parent POM. By doing so, the parent project now knows its
modules, and if a Maven command is invoked against the parent project,
that Maven command will then be executed to the parent's modules as
well
So the maven-install-plugin execution that you added in main pom.xml might be getting triggered for all your modules because of this reason. You might want to move that to the module that you actually need this lib to be included in.Read maven doc.
Related : force Maven to copy dependencies into target/lib

Parent POM dependency tree discovery for sub modules (Maven)

I have created a maven goal that outputs a dependency tree for a given maven project to a YAML file. Using the m2eclipse plugin for Eclipse Photon.
Currently when I run the goal against a parent project (using type pom when setting up dependency), the only dependency returned is on the pom for the specific parent. Is there any way to prevent me needing to run this goal against all of the projects underneath the parent?
Might need a little more information on this, but are your children poms of the parent specified as modules within the parent pom?
Example:
<parentPom>
<modules>
<module>/path/to/childA</module>
</modules>
</parentPom>

Maven deploy without <parent> tag

Project structure:
src
- pom.xml (parent POM)
- core (lot of folders, sub-folders, source files)
- deploy
- DTO
- pom.xml
The DTO folder:
uses shade plugin to collect a few classes from core and create a new jar
this new jar has simple POJOs and does not need any dependencies
hence the deployed POM for this jar should not have any parent or dependencies as this jar is used by many other clients (android is one)
however, since the POM in DTO folder is inherited from the parent pom, this parent inheritence is an issue
Question: how can we deploy this jar with a simplified custom POM without any parent?
Possible solution: DTO POM can be without parent. However, we lose many inheritences, such as version, properties, repository etc., which need to be redefined in the DTO POM if it does not have any parent.
Is there a better way of doing this?

Integration testing of Maven Plugin submodule

I have Maven project with multiple submodules. One of these submodules, let's say submodule X depends on all other submodules, as it is Maven Plugin that integrates everything else.
Now I want to use this Maven Plugin during verify stage in my parent POM to do real run of it over entire project (kind of eating own dogfood).
Unfortunatelly I'm getting error:
The projects in the reactor contain a cyclic reference
So how can I make such dogfood integration test for a Maven Plugin submodule?
I would review aggregation and inheritance: they are indeed two different concepts in Maven often used in combination but which can however be used in a complete separated manner as well.
In this case, your plugin submodule is certainly defined as a module in the aggregator pom (that is, it is a submodule). But I also presume the plugin submodule also has as parent pom the aggregator pom as well (that is, the aggregator pom is also the parent pom, which is a normal approach, but not always required).
Is the latter required? You could keep on having the multi-module/aggregator approach without necessarely having the aggregator as parent of the plugin submodule. As such, the plugin subModule would still be a module of the aggregator pom but it would not have as a parent the aggregator pom, decoupling it from it and as such breaking the cyclic dependencies.
Possible drawback: in the aggregator pom you were also defining common things (dependencies management, properties) required also in the plugin submodule. If the case, you would then need to duplicate/review these common settings only for the concerned submodule.
Small suggestion from my side. Cyclic means you have some transitive dependency in your modules. Can please check the dependency tree of your modules and resolve the circular dependency first.

Categories