Thursday, September 22, 2016

If you are using Maven inside Eclipse IDE via M2Eclipse plugin then following tips can help you a lot.

1) Setup Dependency as Java Project in Eclipse
If your POM dependencies is another project in Eclipse then your project will automatically get updated whenever you build the dependent project. For example, let's say you have a project, ABC, which is dependent on two core modules e.g. framework.jar and persistence.jar, if you have checked out these project and has set up them as Maven Eclipse project, M2Eclipse can directly add them as dependency, instead of loading their JAR files from repository, be it local or remote.
You can verify this by expanding Maven dependency in your project, as shown below. You should see project folder instead of JAR in place of dependency. By the way to make this work, you must have the same version in pom.xml of ABC and framework.jar project. For example, if ABC needs 1.0.0.SNAPSHOT version of framework and framework pom.xml also has 1.0.0.SNAPSHOT.

The moment they goes out of sync, Maven fallback to loading JAR from repository option. What is the benefit of it? Well, you can build your dependency right there and also update your main project, you can also navigate and debug to the dependent source. I have noticed one issue, while using this feature, sometimes even after building dependent project, the main project is not updated automatically in Eclipse, if this happens just select your main project, right click, choose Maven and select update project. You can further read Maven for Eclipse to learn more tips about using Maven in Eclipse.

2) Customizing Goals
You are not limited to Maven build commands provided by default to you e.g. When you right click on any Maven Java project and select "Run as", you can see all these options as "Maven Build", Maven Clean etc. You can create your own Maven build configuration, select the goals you want. For example, I usually use a Maven run configuration with goals like "clean compile package test" or sometimes just "clean compile package". You can also choose options like Update Snapshots, Debug Output, Skip Tests etc.

3) Updating Maven Settings.xml
Whenever you change your proxy details on settings.xml file of Maven config directory, don't forget to also load updated file in Eclipse, otherwise, M2Eclipse plugin will not be able to connect to remote Maven central repository.

That's all about some quick Maven Eclipse tips for Java developers. Both Maven and Eclipse are great tools and every Java developer should be familiar with them. They also goes along nicely because of M2Eclipse plugin. Yes, there are some initial hiccups but once you cross that barrier its smooth sailing. I have saved a lot of time using Maven directly from Eclipse, no need to search JAR, download them from internet and then finally add them into build path of Eclipse project, all can be done in couple of clicks using Maven dependency management features. You can also read Maven for Eclipse of Packt to learn more tips about using Maven in Eclipse.