Thursday, September 22, 2016

3 Maven Eclipse Tips for Java Developers

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 Maven Eclipse Tips for Java Developers

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.

Using Maven in Eclipse tips Java Developers

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.

Further Learning
Maven Fundamentals by Bryan Hansen
Java Web Fundamentals
The Eclipse Guided Tour - Part 1 and 2

Other Maven Eclipse articles you may like to explore
  • Difference between Maven, ANT and Jenkins? (answer)
  • Top 10 Maven Plugins Every Java developer should know (see here)
  • How to install Maven on Windows 10? (tutorial)
  • Maven dependency search not working in Eclipse? (solution)
  • How to increase heap size of Maven? (steps)
  • How to remote debug Java application in Eclipse? (tutorial)
  • 10 Eclipse debugging tips Java developer should know? (see here)
  • Free Maven books for Java developers (see here)

No comments :

Post a Comment