Skip maven build for unmodified modules? - java

I am using maven multi module build like below
<modules>
<module>module1</module>
<module>module2</module>// dependant on module 1
<module>module3</module>// dependant on module 2
<module>warApplication</module>// its a war file dependant on above modules
</modules>
Present build behaviour :-
If i make a change only in module 1, maven will build even module2, module3 source files and then put module1,module2,module3 jar files under warApplication/target/lib.
Is there a configuration/way when i trigger the build, maven verifies if any file was modified under the module, if no skip that module. For war file it will put specific
lib file that got modified. This will save a lot of time.
To check if any file got modified under module or not, maven can store last build time . Before starting build it will check if any of the file got modified after last build timestamp, if yes build it otherwise skip it.
Is there any maven plugin available for this ?
Update :-
My project structure is
TopFolder
.mvn directory -> extensions.xml
build directory->build-Projects-> pom.xml
build directory-> parent -> pom.xml
modules - > module1 -> pom.xml
modules - > module2 -> pom.xml
modules - > module3 -> pom.xml
Here is i am including modules in parent pom
<modules>
<module>../../modules/module1</module>
<module>../../modules/module2</module>
</modules>
. I have included scm tag <developerConnection>scm:svn:https://comp.com/svn/trunk</developerConnection> in both below projects
build directory->build-Projects-> pom.xml
build directory-> parent -> pom.xml
Running build from build directory->build-Projects-> pom.xml.
But the issue is when I change any file any module and run build from build directory-> pom.xml, its not detecting module changes ?

Porbably the best option is using incremental module builder. The first time after making changes in your extension file you need to mvn install and so on you can use mvn -b incremental package which just build only the modules which are changed from the previous build. The github address is https://github.com/khmarbaise/incremental-module-builder

You can use profiles to build what you know has change, as timestamp isn't supported within the pom
See here for more details : Maven Modules optional in pom.xml

Related

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

Submodule dependencies with Maven

I have a project structure like:
However, whenever I run mvn clean test -DskipTests from the Project 1 directory, it ends up NOT including the Project 2 module in the Maven reactor even though I've listed it as a dependency in Project 1 as follows:
<dependency>
<groupId>com.main.sub</groupId>
<artifactId>project2-artId</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
What could be happening and why isnt the dependency being recognized?
From https://maven.apache.org/guides/mini/guide-multiple-modules.html; the reactor "Collects all the available modules to build". (modules of the project that is currently being built)
You need to: create a parent project, with packaging pom; add two modules to this project (one for Project1, one for Project2), and submodules for project1.
Then, when you build the parent project, the build order for all modules are decided by the reactor-

how to add dependency of other maven project to my current maven project in java eclipse?

I want to use maven project 2 classes in my maven project 1 so that I can use it as a parent class. Can you please tell me how to do this using eclipse and JAVA.
Please write in step by step navigation as I am new in maven.
Thanks in advance
I know its too late But this could help others who are searching for same problem.
suppose you have 2 projects p1 and p2 and you want to use p2 project's classes in p1 project then,
A) Right Click on your p2 maven project(If not maven project then convert it to maven) then
-->Choose Run As-->Click on Maven Install.
(It will make p2 to avaliable for other projects in your local maven repository)
B) Now you can simply add p2 project as dependency in p1 project's pom.xml file like this
<dependency>
<groupId>com.project.p2</groupId>
<artifactId>p2</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
For more details see this
maven install is a hack.
the correct thing to do is create an aggregator pom that includes both project directories something like this:
<packaging>pom</packaging>
<modules>
<module>pathTo/P1</module>
<module>pathTo/P2</module>
</modules>
and then from this new pom directory run
mvn -am compile
the "-am" here is what is important. It will ensure both projects are added to the reactor and are discoverable.
You can add your local project 1 what is in yor eclipse workspace to your local maven repository.
With this command (command line) yor are able to build this project to a pom.
mvn install:install-file -Dfile=c:\whereYoureFileIs-{version}.jar -DgroupId=com.companyName.projectName
-DartifactId=projectName -Dversion={Your version} -Dpackaging=jar
and after you build your jar of Project 1 to your local maven repository you are able to use this jar in your second project as a dependency. After including this jar you can extend the class you need.
If you need more explanation, please let me know.
add a maven dependency like this :
<dependency>
<groupId>groupId of project2</groupId>
<artifactId>artifactId of project2</artifactId>
<version>version of project2</version>
</dependency>
First Create parent java project
Simple java project
Click on project (right-click) -> Configure -> Convert to maven project
Convert to Maven
Convert Java to Maven
Right click on parent project -> new -> Porject...
select maven module
Create another Maven Module child project depend on base project
ex : called (webapp)
Open webapp/pom.xml -> dependencies tap
Click add -> search about your base group id and select -> save

maven - using local source instead of external dependency

If someone could help me out here it would save me a lot of time.
I maintain an open source library that gets pushed out to a sonatype repository. I make changes to that library a few times a day and push it out to the 1.0_snapshot build using mvn deploy.
Let's call it project1
I work constantly in another project that uses that library let's call it project2.
Right now, whenever i make changes to project 1 or 2, i need to first build and deploy project 1 to the repo, then build project 2 so it downloads a fresh copy of project1.jar
Project2 has Project1 as a dependency in a pom.xml:
<dependency>
<groupId>com.group</groupId>
<artifactId>project1</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
in order to build in a way where all of my changes can be tested, I have to do something like this:
mvn -f ./project1/pom.xml clean deploy
mvn -U -f ./project2/pom.xml clean package
this uploads my project1.jar to sonatype, then project2 downloads the new snapshot and builds it.
This is a simplified picture of what i'm doing on a larger scale, where my compiles take 5 minutes of up and downloads.
Question: What is the proper way to use maven so it knows to use the source of project1 in a dependency in project 2?
IDE:
install m2e in eclipse
import your both projects into workspace
from consumer project (right click > maven > enable workspace resolution)
this will put project2's classes in classpath from its target/classes instead of the actual jar
native straight maven:
You can create a maven project tree, if this two are open source project going through same build cycle it must have one already, if they are not related but related for your usecase then you can temporarily create a maven tree on top of these 2 projects and build the top parent it will build from bottom up in one command
it will find the leaf project, build it install it in maven's cache and now while building projectA it will refer it from maven's cache so no need to deploy to sonatype
You can also point project2 to build using offline mode :
mvn -o package
Then you drop the upload part of the project1 build to the remote repo.
Check the following link: Intro to repositories and How do I configure Maven for offline development?

Using Maven to build and install seperate jar dependencies

Main Module
-pom.xml
Module1
jar dependency
Module2
jar dependency
I am new to Maven and am wondering what the best practice is for configuring the above setup.
We are upgrading our version of Alfresco (currently all of the above are compile via ANT) and for testing purposes and to make headway on the project, I initially just compiled Module1 and Module2 using their existing ant build scripts and then did a manual install into the Maven repository. I manually added the dependency in my Main Model pom.
Module1 and Module2 need to be compiled and rebuild every time we update Main Module. They are shared with other projects and can change frequently.
What I am working on at this point is the following:
In Main Module pom.xml, I am using maven-antrun-plugin to run the associated ant build scripts for Module1 and Module2. This is working fine and produces the two jar.
Now I would like to have my main module pom.xml file install the two jars into the maven repository.
My dependencies in my pom.xml have an issue when I update the project version since the pom.xml has not been run yet to install the two jars.
<dependency>
<groupId>ca.gc.hc.nhpd</groupId>
<artifactId>EformExtractor</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>ca.gc.hc.nhpd</groupId>
<artifactId>SubmissionConsumer</artifactId>
<version>${project.version}</version>
</dependency>
Am I going about this incorrectly? It will work if I run the ant script and install manually and then add the correct version in the pom.xml dependency. We wish this to happen automatically though. We don’t want to have to do this manually for every build we do.
Perhaps you should structure this project with parent and child pom.xml files:
MainModule
|-- pom.xml
|-- ModuleA
| |--pom.xml
|-- ModuleB
|--pom.xml
The parent pom.xml would look something like the one in this example.
The pom.xml for ModuleA and ModuleB could run their Ant scripts, if necessary. Then you should be able to run mvn install at the parent level to install ModuleA and ModuleB to your local Maven repository.

Categories