Maven "also-make"'s behavior - maven

Let's suppose there are two projects, A and B, and B has a dependency on A.
Then I write
mvn verify --also-make --projects "B"
Maven help claims that also-make build projects required, so project A will be built.
But what does "build" mean? What maven phase will be applied to project A? Compile, verify or another?

It will apply the command or phase that you gave in the Maven command.

Related

Setting profile for dependency of multipart project command line

I have a multi-module project. Basically, I have a Project A, and Project B (An integration test). Project B relies on Project A to run. So when we run project B, we do something like: mvn clean package -B -pl :project-B -am. The -am part of the command instructs maven to also compile the dependency (Project A) and to include it. Problem is... When we run the dependency for project A, it doesn't seem to be pulling the correct profile. How do we set the profile to be used for compilation for dependency A in this command?
Thanks!

How to create maven project as dependent jar to other projects without dependencies

I have a java project called "A" it has some custom annotations implemented in spring aop way and it is packaged as jar using maven jar plug-in, which contains only classes specific to project of A without any dependencies.now i have installed it in local maven repository using install command (mvn install:instlal-file) and used it as dependency in another maven project called "B".
I can able to get those annotations in project "B" but i cannot get dependencies automatically from pom file in dependent jar(project "A").due to which building project "B" is failing.
Q1: should we put all dependencies which we are using for project "A" in project "B" pom as well?
Q2: or it could get automatically downloaded from dependent jar pom file?
Note: am not interested in fat jar? but its working fine if we mention all dependencies in project "B". is there any way to get all dependencies when we build project "B" automatically?
Please help me in this.
I guess your error was to use mvn install:install-file. If you do not add the POM as parameter, an (almost) empty POM is created for you.
The right way:
Go to project A and run mvn clean install.
Then use A in B, and enjoy full transitive dependency resolution.
So, to summarize, the answer for Q1 is "no", and the answer for Q2 is "yes, use mvn clean install instead of mvn install:install-file.

Gradle Multi-project build - project dependencies and build order

I need to migrate a multi-project build from maven to gradle and maintain the way inter-project dependencies and build order work. I'd like to use the maven plugin in gradle and continue to publish artifacts to both local and remote maven repositories.
The multi-project structure is like so:
root/
--Project-A/
----Project-A1/
----Project-A2/
--Project-B/
----Project-B1/
----Project-B2/
In maven Project-A2 has a dependency on Project-A1. If I run mvn install_ from the Project-A2 directory it will only build/install that project and pull it's dependency on Project-A1 from the local/remote maven repository. If I run mvn install from Project-A it will build/install both Project-A1 and A2 and calculate the build order based on the above mentioned dependency. How can this same behavior be achieved in gradle?
Additionally, Project-B2 has a dependency on Project-A2. If I run mvn install from the Project-B2 or Project-B directories this dependency should be pulled from the local/remote maven repository. If I run mvn install from the root directory it should calculate the build order such that Project-A1 builds, Project-A2 builds, and then _Project-B2 builds.
That build order, as far as I know, isn't exactly possible with gradle. If you're building A2 and A1 has changed, gradle will build A1. If A1 hasn't changed then it won't be built. Same goes for the second scenario.

Maven build second level+ child projects using reactor option -pl

My maven project structure is as below
Project A
pom.xml
- ProjectB
pom.xml
- ProjectC
pom.xml
- ProjectD
pom.xml
- ProjectY
pom.xml
By using maven reactor options i can
clean install -pl projectB or clean install -pl projectY
But while trying to build the second level child modules using clean install -pl projectC, maven throws
org.apache.maven.MavenExecutionException: Could not find the selected project in the reactor: projectC
how to build the second level+ child modules using maven reactor options
From the documentation for the -pl option it states the following:
-pl,--projects <arg> Comma-delimited list of specified
reactor projects to build instead
of all projects. A project can be
specified by [groupId]:artifactId
or by its relative path.
The important part for you is: "or by its relative path".
So to build projectC, you simply need to refer to it by its relative path (projectB/projectC). So the command you need is:
mvn clean install -pl projectB/projectC
This is an answer from a similar question that is also relevant here.
By using artifactIds you don't have to know the structure of your project.
If you only use the artifactIds of the given project you have to define that correctly on command line:
help output of Maven (mvn --help)
Comma-delimited list of specified reactor projects to build of all
projects. A project can be specified by [groupId]:artifactId or by its
relative path
This means in your case you have to define:
mvn clean install --projects :projectC,:ProjectY
Notice the : that is prepended to the artifactIds to indicate you omit the groupId
Just in case someone else has this one too:
I also encountered this error message. The reason was that by accident I had been in one of my (sub-)modules in the path in terminal.
Of course the command has to be executed in the project's root hierarchy. According to the example above you have to assure that you execute a command like:
clean install -pl projectB
at Poject A
not e.g. at ProjectY or somewhere else deeper in the project structure.
Correct:
user:~/workspace/IdeaProjects/pojecta{master}$ clean install -pl projectB
Wrong:
user:~/workspace/IdeaProjects/pojecta/projecty{master}$ clean install -pl projectB

Maven Nested Multi-module project

I am building a multi-module project where one of the modules is also a multi-module project. When I try to build the nested multi-module independently using -pl <multi-module child>, Maven only builds the pom(the top-level module of the child) and does not build the rest of the modules specified in the multi-module project.
If I don't use the -pl and build all the modules, everything work fine, the project builds as intended.
Any ideas why this might be happening, I can't get Maven to build the entire project if I single it out individually?
The -pl option builds exactly the projects that you specify. Maven treats a parent POM as a regular project that happens to be the first in the hierarchy.
What you can do is browse into your "sub-multimodule" project and execute Maven from there. The option -rf (resume from) might also work.
There is no good reason for Maven behaving that way, other than this is how Maven always behaved for -pm. The -am option could include the modules listed of any module listed in -pl, because the parent "depends on them" in a way, but because they are not dependencies in the strict sense, Maven does not.
If Maven were an active project, a new -amm also-make-modules option might help.
I think the better way to achieve that is to use -pl to refer to one of the grandchild submodules. As it probably depends on its siblings, will can possibly achieve the same result.
This project is a great example on how to do that:
https://github.com/andyglick/maven-multi-level-modules-project
In short, you can do:
mvn test -pl com.example:my-grandchild-module -am

Resources