Does Maven automatically build other modules? - java

I couldn't really find an answer anywhere to this particular question.
I have a (Maven) project consisting of multiple modules, let's say a core module (a jar) and a webapp module (a war).
When I run mvn clean package on my webapp, does it automatically always build the core first and will it pick up any changes in it? Do I have to run mvn clean install instead? Or do I have to run mvn clean package/install on my parent pom?
Does it matter if the parent/module is a release or a snapshot?

If you are working with a so called Multi Module Build you should do everything what you like to do with your whole project from the parent level.
There you can do:
mvn clean package
than it will build all modules in the correct order (assuming you have defined the dependencies between them correct).
If you have such a multi module build all your modules incl. your parent should have the same version number. If you like to make a release of the whole you can simply start from the parent.

You have to build anything that's changed, upwards. So, if you change core, then rebuild core, then rebuild your war. If you've just changed your war, then you only need to rebuild your war. Cleaning is generally good practice. The reason not to do it would be if you're generating a bunch of entity classes, which takes a long time to redo.
I've seen a lot of mistakes because people forget to clean, and then some old piece of code is still active, even though they thought they'd deleted it.
Installing will put your latest build into your M2 repo, which is generally a good idea too. You really can't go wrong with "mvn clean install"

Related

Maven - how to rebuild .xml files only on a Java EE project

As the title states.
I've made changes to .xml file only. Do I need to mvn clean install the whole project or a simple mvn install? Or maybe mvn generate-sources will do the stuff?
Can anyone elaborate?
To build the artifact, you need to call mvn package. If you do not do a clean, beware that remainders of the previous build might still exist. Changing the code of a class is generally fine, while changing the dependencies in the POM is not (you might end up with both versions of an artifact if you change the version in the POM).
Your hibernate case is somewhere in between, so you probably need to make experiments. Maven does not make any guarantees.

situations where clean install is a must not just install?

I have a question which is pretty straight forward, but hard to find an exact answer.
When we are using mvn build, what are the exact situation where we must go for clean install in order to see latest code changes in our built artifact. i.e. just install would not do that for us.
Thanks in advance.
Basically
mvn clean install
is same as
mvn clean && mvn install
so as to answer your question, its required when cleaning the files and directories generated by Maven is the primary requirement prior to doing another install.
Related to the changes in code, I would assume this is more of cleaning the target folder generated by Maven, hence mostly the files compiled would be overwritten when there is not much of a change.
In cases where one has removed/refactored a class from the previous build. There might be references still left if you don't clean and that would get packaged into the jar created thereafter.
From the official documentation(formatting mine) shared by ernest,
But if we try to build the project with mvn install without any clean lifecycle, then we see 2 bugs
the maven build still succeeds to compile the project maven even
generates a jar which contains broken classes
moduleB does not get recompiled and is thus broken as well.
Also, a preferably suggested way is to instead use
mvn verify

IntelliJ: Build > Rebuild Project menu item versus Maven clean install

For a Maven-driven project in IntelliJ 2018, when should I choose to use…
The IntelliJ 2018.1 menu item Build > Rebuild Project
The Maven panel’s clean and install Lifecycle items
Under what circumstances is either appropriate?
My situation is a Vaadin 8 application using the vaadin-archetype-application-multimodule Vaadin archetype.
The Question, Does IntelliJ Build > Rebuild Project invoke maven? has a single Answer that says the Rebuild Project does not invoke Maven at all. But no guidance is given on when to use route versus the other.
Well, if you make substancial changes within a pom.xml file (new plugins, new modules within a multi-module project), you'd better run "mvn clean install". I personally never use the "Rebuild Project" within a Maven project. usually I just "make" the project so the class files are updated, or I start a real Maven build to get new / updated dependencies.
The third important option (for me) is the "Reimport all Maven projects" option, which is quite useful if a pure Maven build runs fine, but IntelliJ still thinks that there is something wrong within your project.
When you have a multi-module Maven project and:
Start your tests and application programmatically by IntelliJ (Main.java):
You do not need to use Maven at all, no clean, no install, only Make. And Rebuild when the project gets screwed up.
Click Reimport when you change dependencies or someone deploys a new dependency snapshot to your repo.
If IntelliJ refuses to download the snapshot, then try mvn clean install.
And why would you even do all that? It is faster and more user friendly.
Start your tests and application by Maven in IntelliJ (mvn jetty:run):
You have to install your modules which are as dependencies for the module on which you start your application (mvn jetty:run), because Maven does not use your compiled output for dependencies, but takes an artifact from your remote/local repository. For this case Make/Rebuild is useless, you have to use mvn (clean) install.
If you have made changes only in a module on which you run mvn jetty:run then you do not need to use package/install, but sometimes you have to use clean or Rebuild as Maven does not remove deleted resources.
Make is good for hot-swap - for that reason, it is a good idea to import all Maven projects you develop and on which you depend into a single IntelliJ project. Initial Rebuild comes handy, because without it, Make would compile the whole project instead of only changed classes, then possibly failing hot-swap or it would just take too long.
Or you could just click Make before you run your application, but it could leave some old resources created by Maven in compiled output, Rebuild would take care of that.
You can also configure your app-server to restart/reload when it detects updated files from Make or Rebuild - certainly faster than restarting the JVM or compiling by Maven, but you might like to disable that and use only hot-swap, which is still way faster if you do small changes.
Either way you might want to take a look at Maven Helper plugin for IntelliJ, it will make things way easier in big projects.

Maven m2eclipse detect changes in dependecy in workspace and automatically install

I found an annoying issue working with m2eclipse in Eclipse.
My workspace contains 2 projects, an application A and a library B. The application A POM has B as a dependency and everything works correctly. (The project dependecy is found and used when I build the application)
But if I change some code in project B and I forgot to mvn:install it, when I build the application it uses the last built version of the library and it loses my last changes.
Is there a way to force Maven / M2Eclipse to check if the source code of the dependecy is newer than the last version built, and to install it when installing/ deploying the main application?
Or maybe my approach is wrong or is something obvious that I'm missing?
You can tell m2eclipse to use resolve dependencies from the workspace rather than through the normal mechanisms. In your project properties (NOT workspace properties), select Maven->Resolve dependencies from Workspace projects.
This will mean that when you change B and subsequently build A, the changes should be picked up automatically.
If, however, you build outside Eclipse, you'll have to do the normal mvn install to get the correct dependencies.
I don't know of a way to tell maven to build the library first, then build your project. You could put two maven commands into a script and run the script.
Alternatively, you could put both projects inside a maven parent project, and then build the parent. This causes all child project to be built too (so in your case the library, and the application).
I think you have couple of options here
If you can change the maven project set up, I would suggest you to use maven multimodule
Option two might solve your problem but still involves a manual stop when you change your dependent project B, Do this on for your dependent project in eclipse
Select Library B ==> Properties ==> Maven ==> in the input box under Goals to invoke after project clean: ==> enter : install
To deploy any change that you made to Project B in eclipse to maven local/remote repo, You have to run Clean build in eclipse, This will deploy the latest Library B to the maven repository
Also make sure your Library B version is a SnapShot during the development
If you are looking to run the latest snapshots on your local machine you should try to set things up to launch directly from Eclipse.
Apart from avoiding the problem you originally posted about, it also has other advantages.
It will save you a lot of time by not having to perform intermediate build steps.
You can start and stop servers without having to leave your IDE.
The Eclipse console has extra features that you don't get from the system console.
You don't have to modify your start scripts to attach a debugger.

Migrating from ant to maven in Netbeans

Our software is written in Java and comprise many (7) projects.
These projects are Netbeans ant projects.
I'm considering to converting them to maven2.
Where can I find some hints for doing such thing?
Don't read that book. It will only make you confused. Read this book instead: "Maven - The definitive guide" http://www.sonatype.com/books/maven-book/reference/ .
Also, the maven site has a lot of information, but the structure is terrible so you'll need to use google to navigate in it.
Here is my suggestion:
Do this by hand, not with "automagic" "help" from the IDE. Maven integration doesn't work that good yet, not in any IDE.
Make sure you program project is divided into modules under a common umbrella module, so that each module produces a single binary artifact (jar, war,...) possibly accompanied by the javadoc of the source code behind that artifact, a zip with the source code etc. The basic principle is that each module produces a single artifact, containing all the non-test-code under that module. You can do this while the project is still built by ant.
Each module should conform to the standard maven directory layout. The build destination is under [module]/target/[output-type, e.g. "classes"]. The source code is under [module]/src/main/[src-type e.g. "java"] and [module]/test/[src-type]. The artifact consists of all the code under src/main, and none of the code under src/test, as it built to the target directories. You can do this while the is still built by ant.
Start by transforming the sub-module that has no dependencies on other modules in the project.
Now you can create the parent maven module pom.xml with artifact type "pom", consisting of one of the modules below. Make a child module for the first submodule (the one with only external dependencies), using the umbrella module as "parent". Remember that you need to specify version for the parent. Remember to add the child module as a "module" in the parent too. Always use ${project.version} as version in the child modules when you create multi-module projects like this. All modules under a parent must be released simultaneously in a single operation, and if you use this setting maven will make sure the version fields stay the same across all modules and gets updated everywhere during the release. This may make it difficult to re-use the existing numbering scheme, but that doesn't matter. You are never going to run out of version numbers anyway.
Add the necessary dependencies, and make sure you can build the parent and the child module together using the command "mvn clean install" from the parent module.
Proceed with the rest of the modules the same way. Dependencies to other modules under the same parent project should also use ${project.version} as the "version" they are depending on, meaning "the same version as this". NOTE THAT in order to build, the module you are depending on must be built using "mvn install", so that it gets deployed to you local (computer) repository. Otherwise the depending module will not be able to find the classes. There are NO source-code dependencies between modules in maven, only dependencies to built and packed versions installed in local and remote repositories. This can be very confusing if you come from ant-projects. Build from the root module until you get comfortable with this. It takes two days.
Don't use maven integration in IDEs. It is a bad idea. Use "mvn idea:idea" or "mvn eclipse:eclipse" to set up your workspace as a non-maven ordinary IDE project. The inter-module dependencies mechanisms in maven and the IDE aren't identical and will never be. Also, if you have several mavenized projects with dependencies in between, you want to have several of these in your workspace with dependencies set up between. You can do this with mvn idea:idea / eclipse:eclipse if you create a separate maven project file called "workspace.xml" (or whatever) in the same directory as parent module, set up as a multi-module project containing modules "." and "../otherproject" (only one-way reference here, no parent ref back). If you run "mvn idea:idea / eclipse:eclipse -f workspace.xml" you get a workspace with all these modules linked together. No IDE integration lets you do that. This sound like a lot of extra work, but the workspace.xml-file is really small. It doesn't have to contain all that dependency stuff and all that, only the reference to the modules you want to bind together in your IDE.
I did a succeful migration of NetBeans Ant project to Maven project using the instruccions by Joseph Mocker here: http://forums.netbeans.org/ptopic55953.html
I cite the important part:
close the project
rename the build.xml, nbproject files/folders to something so NB won't recognize them.
close and restart NB (so any memory cache knowledge of the project is gone)
copy in an empty pom from some other project.
open the project back up in NB (NB should now identify it as a maven project)
rearrange the files to follow the maven way (™)
This won't be an easy task since Maven2 expects the files to be organized in a specific way. Anyway Better Builds with Maven is a free book that should get you started. It will help you understand Maven and it also has a chapter on migration.
I discovered that the migration is not necessary. The real requirements that I need was automatic download of dependencies (libraries).
This is also achieved by Ivy which nonetheless uses maven repositories.
I solved converting project from ant to ant+ivy with IvyBeans.
I have built a script to migrate Ant builds to Maven. You can find more information here:
https://github.com/ewhauser/ant2maven
It won't help you with fixing your directory structure and or any additional Ant tasks, but it removes a lot of the tedious steps to get started.

Categories

Resources