Gradle Multi-project build - project dependencies and build order - maven

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.

Related

Is there any way i can see the effective build configuration of a sub project in gradle (in intelij)?

I have a root project and few sub projects.
The root project, in it's build.gradle file, adds some configurations, tasks, plugins and dependencies to subprojects.
In the subprojects's build.gradle file i have added additional deps, plugins, task and conf.
Now i want to know the effective build configuration of the subproject. Is it possible to get this info using any gradle command. If yes, is the same possible in intellij idea
(In maven I can get the effective configuration of any project using mvn help:effective-pom no matter if it's a submodule or has a parent pom. And in intellij idea i can right-click on the pom and choose to view the effective pom. It's quite simple. )
Edit:
Like Rob mentioned in comments - The below commands can be used to find any projects (parent or child/sub-project) dependencies in various scopes/configuration.
/gradlew :<sub-project-name>:dependencies
and
./gradlew :<sub-project-name<:dependencies --configuration <name-of-configuration>
And the Maven equivallent of the above would be mvn dependency:tree.
But I am interested in knowing other info also besides dependencies. Other info like: tasks, plugins, repositories etc that is applicable for the sub-project (i.e defined in the sub-project + inherited or applied from/by parent project.
maven mvn help:effective-pom == gradle ????

How to reference local Gradle project from Maven build as dependency?

I have several Gradle library projects and main Ant spring web-app project (historically). I'd like to replace Ant with Maven for main project while keeping existing Gradle projects nature.
Is it possible to refer local Gradle projects from pom.xml as local dependencies of Maven project?
Search readily gives me the opposite - "how to refer maven projects from gradle builds", but not my case.
Gradle always builds locally build/libs (or distributions); the only easy way to share the build dependencies between completely different projects is 'as maven repository dependencies'
in your case the options are
Work Local Builds only
add the maven plugin to the gradle builds - do local install
and refer them in the maven build locally.
Build Anywhere
Your Gradle builds publish artefacts to your local nexus
and you refer them properly in your dependencies
--
Gradle by default does not have 'maven install'; it can be added using the maven plugin see - https://docs.gradle.org/current/userguide/maven_plugin.html#header.

Maven - do a mvn install on local artifact when packaging

i have a maven project which has some local artifacts as dependencies.
When I have to package my main application, i have to do a mvn install command on my local repositories before, which is quite annoying and easy to forget.
Is there a way to tell maven to install local repositories when packaging the main one?
One solution can be to wrap up all your components into a pom project.
When building a parent pom maven automatically triggers a build of all the sub-modules.

How do I create the junit5 maven POM files from gradle?

I'm attempting to build (a fork of) jUnit5.
./gradlew build
produces the JARs, but I also need the pom.xml files that go with the JARs, so I can use the artifacts using Maven. Changes are there is a gradle task configured that does this already in the existing jUnit repo, but which?
Figured it out. Gradle is non-obvious to me ... it is the "maven" plugin.
Invoke:
./gradlew install
which will:
Generate POMs into directory .../build/poms with name pom-default.xml
Copy those (while renaming) and the JARs into the local Maven repository at ~/.m2.

Setting up maven to compile (instead of downloading) dependencies

I cloned the git repository of Apache ActiveMQ Artemis project (https://github.com/apache/activemq-artemis) and then typed
mvn -Ptests test -pl :integration-tests
I was surprised to see log messages like the following
...
Downloading: http://repository.apache.org/snapshots/org/apache/activemq/artemis-selector/1.4.0-SNAPSHOT/artemis-selector-1.4.0-20160625.030221-11.jar
Downloading: http://repository.apache.org/snapshots/org/apache/activemq/artemis-core-client/1.4.0-SNAPSHOT/artemis-core-client-1.4.0-20160625.030211-11.jar
...
Since e.g. artemis-core-client is contained in the git repository I cloned in the beginning, I'd have expected maven just builds it from there.
That way, when I make changes in the core client source, they get picked up by the integration tests.
Instead, maven is downloading the jar from the repository.
Question: How do I configure maven to always build all modules that are in the git repository and download only "true" dependencies, which I mean things not in the git repository?
You are not executing the Maven build on the main project, on the main pom.xml which indeed defines the artemis-selector and artemis-core-client modules, among others.
You are executing the Maven build on the tests and its pom.xml, where only tests modules are defined. This is a side/test project, which has as parent the previous pom file, but it doesn't play any role in its parent modules definition. Hence, dependencies are not resolved as modules but as Maven dependencies.
You should firstly install (via mvn clean install) the former project, so that libraries will be available in your local Maven cache (hence no downloading would be triggered), then execute the tests project.
Check the Maven docs for a inheritance vs aggregation difference to further clarify it.
From the Stack Overflow, the follow threads could also be interesting:
What is the difference between using maven -pl option and running maven from module level?
Maven multi module project cannot find sibling module

Resources