maven force rebuild of dependent projects (build dependencies first) - maven

I have a multimodule maven project. the service module depends on domain module using dependency tag. Every time i build the service module, i want it to automatically build the domain module and pick up the most recent domain module from local repository. How can I do it. Right now it pick from the local repository but that may not be the latest copy.
I build the project from my service module dir and not from the parent directory. Because my parent module has lot of other submodules that I am not interested in building.

One thing you can possibly do is to build the service module from the parent directory with the following parameters.
mvn compile -pl service-module -am
What this does is builds only the service module, along with its dependant modules (which would include the domain module).
The options are described by mvn --help (Maven 2.2.0):
-am,--also-make
If project list is specified, also build projects required by the list
-pl,--projects <arg>
Build specified reactor projects instead of all projects

Related

Maven multi modules SNAPSHOT dependency [duplicate]

I created a maven multi modules project with 2 modules: core and service.
Service module is using core module (core is declared as a dependency on service module).
The parent pom version is 1.0-SNAPSHOT (so the modules too).
During the maven clean package phase, maven is not fetching core module neither from my local repo nor from remote repo (nexus). It is always building with core module within the project and not fetching any more recent SNAPSHOT version from repo (local/remote).
Is that the expected behaviour?
If you build the project, it will build everything. The command clean package will resolve the inter-module dependencies inside the project.
Everything else would probably lead to surprising results.
If you really just want to build just one module, use the -pl parameter.

Maven multi module, dependency between modules

I created a maven multi modules project with 2 modules: core and service.
Service module is using core module (core is declared as a dependency on service module).
The parent pom version is 1.0-SNAPSHOT (so the modules too).
During the maven clean package phase, maven is not fetching core module neither from my local repo nor from remote repo (nexus). It is always building with core module within the project and not fetching any more recent SNAPSHOT version from repo (local/remote).
Is that the expected behaviour?
If you build the project, it will build everything. The command clean package will resolve the inter-module dependencies inside the project.
Everything else would probably lead to surprising results.
If you really just want to build just one module, use the -pl parameter.

Maven dependency resolving in multi module project

I have a question about how the Maven dependency resolving mechanism is working in a multi module project.
Normally I only use 'mvn clean install' when I build my multi module projects and my assumption was that if any module in the project needs a previous module, dependency will be resolved by going local repository and loading the corresponding 'jar'.
For project internal reason, I have to use 'mvn clean compile,' this command naturally does not create any 'jar' while 'install' is not there. So here I started wondering, how the dependency resolution for a multi module project works, while jar' is not created but project still able to see the changes from the previous builds. Does the target directories used for dependency management?
Or for 'mvn clean compile' target directory used but for 'mvn clean install' the local repository.
Can anybody explain me how the dependency resolution works in a 'multi module' project.
Thx for answers.....
I think you will understand better if you look at https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
There is a life cycle in the process of building the jar. The compile target will compile the code and create a complete classes folder in your target directory. This target will resolve all your dependencies in your poms and download any dependencies to your local repo, not already there.
The install target will create the jar from the classes directory and install it in your local repository.
I really think you will need to run the install target to get anything useful.
Maven is made of separate components.
There a component that deal with a given module and among other things try to get its dependencies. It ALWAYS get the dependencies from the local repository, eventually after having downloaded such dependencies. If the dependencies are not there and can't be dowloaded, it will fail. Eventually the module will create its own artifact that it will publish to the local repo.
Then there a compoment that when you ask it to build several maven modules, for example calling mvn at the root of a project does order the various module use the dependencies to find the best ordering for the build so that if a given module depend on another, it will be build after the module it depend on. It then call the previous compoent I described, building each module in order.
In all cases, a given module dependencies are always taken from the local repo. The expectation is that the previous modules that were built before actually pushed their artifact to the local repo typically with the mvn install but you could force it do it at any step thanks to proper configuration (may not be a good idea).
In all case if the previous component jar was not built and put into the repo, there no way such jar can be added in the classpath for the next module to be compiled.
Doing compile only on multiple projets isn't going to be any useful.

Maven: how can I skip building artifacts which exist in central repo?

My situation: I have project which contains several Maven modules. I make changes to one of them. Suddenly I find out, that my project is no longer possible to be built because of the errors in other modules. To fix this I need to run SVN UPDATE and rebuilt the project.
My assumption: probably, during the build process of my module some of the artifacts are taken from central repository and have the most newest version, while others are still outdated and taken from my local repo.
A question: I don't want to rebuild my project each time someone updates ANOTHER Maven module. I want to download the already built artefacts from the central repository without rebuilding them by myself. Is it possible?
You can tell Reactor which modules to build. In your case when you only change a single module and want to speed up the build you can pass -pl (Project Location) parameter to maven. For example:
mvn -pl module-with-changes
That will build single module, while taking other dependencies from your local Maven Repository or download from Central (whatever is the latest). That said, if you already ran mvn install for whole project and other artifacts have not been updated in Central repository, then Maven will see your local artifacts as latest and will not re-download them.
Another issue you might get with -pl parameter is when other modules in your project depend on the module that you are building. If there is a problem in dependent module you will not see it by building only the dependency model. To avoid that you can pass -amd (Also Make Dependents). Like this:
mvn -pl module-with-changes -amd
That will trigger the build for module-with-changes + modules that depend on module-with-changes + their dependents.
You can get more info about Reactor parameters from here:
http://www.sonatype.com/people/2009/10/maven-tips-and-tricks-advanced-reactor-options/

How to rebuild dependencies before running jetty from maven

I have a multi module maven project. One of the modules is a reusable part which is packaged into a jar, and the other is a war web-app which depends on the first module. When I use jetty:run-exploded on the second module, the packaged jar is taken from local maven repository whereas I want the first module to be rebuild and packaged into the resulting war. Is there any way to force such behavior instead of the default one?
From everything I can tell from reading documents about Maven's design and using Maven myself this cannot be done in the projects own directory.
Maven will not follow module paths UP a hierarchy. Using -amd (also make dependencies) will only work at the top level module that ties all the other multi-module pom's together. So what you can do is this:
At the TOP level directory
mvn -amd -pl jetty_module jetty:run-exploded
I think you can use maven Advanced Reactor Options to archive this.
http://www.sonatype.com/people/2009/10/maven-tips-and-tricks-advanced-reactor-options/
The -pl or –projects option allows you to select a list of projects from a multimodule project. This option can be useful if you are working on a specific set of projects, and you’d rather not wait through a full build of a multi-module project during a development cycle.
Maven -amd(also-make-dependents ) also help to build multi module project once. Using that you can build a project and any project that depends on that project.

Resources