Minor Annoyance: edu.emory.mathcs.backport.java.util - maven

I know this is trivial, but I bet I am not the only developer being annoyed by this issue:
I have a maven project building a Spring Hibernate frameowrk for a webapp.
I am building using Eclipse.
I use Eclipse shortcuts to import dependencies and often do so almost subconsciously.
Somewhere in my project dependencies I have a jar that includes the following package.
edu.emory.mathcs.backport.java.util
I often accidentally import this package instead of java.util and get compile or runtime errors.
QUESTION: How can I find out which dependency includes this package so I can (hopefully) exclude it using the Maven enforcer plugin.

This does not directly answer your question but may help you prevent the accidental import in the first place:
In Eclipse you can add a type filter in Preferences → Java → Appearance → Type Filters.
From the description there:
All types whose fully qualified name matches the selected filter strings will not be shown in the 'Open Type' dialog. They will also be ignored in content assist or quick fix proposals and when organizing imports. For example 'java.awt.*' will hide all types from the awt packages.

You can run mvn dependency:tree from the command line. It will show a tree of your dependencies and their transitive dependencies.

Within Eclipse you can open your pom.xml using the Maven POM Editor (default editor for POM files) and click on Dependency Hierarchy.
Within this view you can use a filter or select the dependency on the right side and the dependency tree for this dependency will be shown on the left side.

I recently encountered this issue with a Maven project in IntelliJ IDEA.
You can view transitive dependencies using the Maven command mvn dependency:tree to figure out where this package is creeping in.
However, it's worthwhile to mention that this problem can occur for Gradle or Maven projects. If you are using IntelliJ, then another workaround that will work for all of your projects - regardless of build tool - is to exclude the package altogether from the IDE's editor settings.
The steps to exclude the edu.emory.mathcs.backport.java.util package when using auto-import for all of your projects opened in IntelliJ are as follows (this is valid for IntelliJ IDEA 2022.2):
Ctrl + Alt + S (or click on File, then Settings)
Editor > General > Auto Import
Under the exclusion section, add the following exclusion with an IDE scope: edu.emory.mathcs.backport.java.util.*
Reference Screenshot

Related

How to find a library reference in Intellij Idea

I have a complex project with a lot of Maven libraries. From time to time I am facing the problem of a library version conflict. I can open the class which is conflicting and see all libraries a class is packed in. But unfortunately I don't find an easy way to check where these libraries are referenced in the Maven dependencies. Do you have an idea how to find a library in the Maven tree?
You need Maven Helper plugin
https://plugins.jetbrains.com/plugin/7179-maven-helper
It has a dependencies viewer and lets you resolve conflicts right there. Open your pom file after installing the plugin and you will see another tab at the bottom of the editor window.
Right click on any dependency to bring up the context menu and you will see an option to exclude it. It will add exclude to your pom file. That obviously won't work for uberjars.
Going more low-tech, you can let maven tell you the answer with the command
mvn dependency:tree -Dincludes=groupId:artifactId
You can also invoke this command simply from an Intellij terminal window.
mvn dependency:tree will print the entire tree, the includes addition will let you filter on a specific artifact making it easier to search (although I've been known to just copy the entire tree into a scratch file and just do searches on it there).
If you don't know the groupId or the artifactId is already unique enough, you can filter like this:
mvn dependency:tree -Dincludes=*:artifactId

Eclipse RCP: Dependency check of Manifest file

Background
I am developing an Eclipse RCP application with about 100 plug-ins. It's my responsibility to control the dependencies of these plug-ins. If the source code of one plug-in is changing and another plug-in is needed it's easy in Eclipse to add a new dependency by a quick-fix or the Manifest Editor.
My questions
Is there also a way to get informed, that a referenced plug-in is not anymore needed and can be removed from the Manifest file? My goal is to keep dependencies as minimal as possible. So is there a static code analyze tool or an Eclipse plug-in that covers this?
There is an unused dependencies analysis tool in the MANIFEST.MF editor.
Open the MANIFEST.MF editor and switch to the Dependencies tab. At the bottom right there is a 'Dependency Analysis' section which is usually collapsed. Expand this section and choose the 'Find unused dependencies' tool. This will run an analysis and suggest unused dependencies to be removed from from the MANIFEST.MF
This needs to be used with caution because it can be a bit too keen to remove dependencies when there are indirect dependencies.
If its a maven project you could always run mvn 'dependency:analyze' on the project and check for 'used undeclared' and unused declared dependencies

How to link individual Java classes to jars displayed by dependency:tree?

Is there a way to easily link a specific Java class to a jar that is listed by mvn dependency:tree? I tried -X to and -Dverbose to add verbosity but it didn't do it.
I have an import statement that I suspect is pulled from one jar in one version of my pom and from another when I change some dependency declarations in the pom because the API becomes different. But I know of no easy way to trace the imported class to a jar in the dependency tree.
To locate the maven dependency for a specific import, I usually do a Go to -> Declaration (⌘B on OSX) followed by clicking the Scroll from Source button in the Project tab like so:
I noticed that the first time I did this, the source for junit-3.8.1 was not present in my local maven cache due to which, Intellij IDEA decompiled the Testcase class. The Scroll from Source did not work for this de-compiled source. However after clicking the Download Sources and retrying the Scroll from Source worked fine as above.
This approach has stood me well for checking the maven dependency which is the source of that one off import.

Adding maven support to existing IntelliJ module is not available

I just started an IntelliJ plugin project, and after going a few steps, I realized that I am not managing dependencies with maven. Naturally I head over to the module in the project explorer, and right click -> Add Framework Support. But Maven is not listed! In fact, the only thing listed is Groovy. What could cause this, and how do I get maven back?
The maven plugin is enabled.
Here is a brief answer how to get going with Gradle, but most questions remain unanswered.
How to manage development life cycle of IntelliJ plugins with Maven
For a comprehensive read this post : http://labs.bsb.com/2013/11/how-to-manage-development-life-cycle-of-intellij-plugins-with-maven-2/
The post also includes a link to the source code on github.
Making your plugin project use maven in IntelliJ
This is probably not what you are struggling with, but I include it just in case. Supposing you have already started a new plugin project you have two options:
Right-click on the project name in the project explorer and choose New > Module
Create a pom.xml in the project root, then right-click on it and choose Add as Maven Project
hth
First, make sure you have enabled maven plugin in
File -> Settings -> Plugins and add search the maven plugin and activate
Restart the IntelliJ.
Go and check the tool window to make it visible
go to View -> Tool Windows > Maven Projects to open it.
The above options might take some time to execute but there is a quick fix to this if you already have a pom.xml file present in the project.
Right click on the pom.xml file and you will see an option
Add as Maven Project
As far as I understood, you have to mark your folder with pom.xml as Maven module.
In order for the project to become Maven:
Go to - File -> Project Structure (or Ctrl + Shift + Alt + S)-> Modules.
In the middle, you must remove the existing module.
Then in the same place of window click on the plus -> Import module -> Select the
required folder with pom.xml -> Import module from external module -> Maven -> Next ->
Finish
You may need to restart your IntelijIdea. Better just in case to restart it.
After restarting it, it should be appear a pop-up window in the lower right corner - Import maven module -> Click on it.
After that, the folder for module and pom.xml should be displayed as Maven.
P. S. Also check before these steps, that you have Maven support for your IntelijIdea.
Solved it being root as intelliJ launcher... not the best solution but seems a workaround.
So the best solution I found yet instead of digging into filesystem to see where there is a permissions problem was to change the owner to my user
sudo chown -R myUsername:myUsergroup /opt/becauseIInstalledItHere/idea-IC-version
To get the option in the right hand of intellij you have to follow two step given below -
Right click on Pom.xml
Click on "add as Maven Project"

Using maven's --also-make option in IntelliJ

You can set several properties and configuration options in a Maven build within IntelliJ, but I haven't quite figured if you are able to specify options that are available on the command line such as --also-make or --also-make-dependents.
Is there a way to have those options used by Maven run configurations in Intellij?
You can create a run/debug configuration by right-clicking any goal in the Maven Projects view, select Create your-project[your-goal] and from there you can add any command-line parameters (such as --also-make). Your configuration will be saved and accessible from a single click on the green arrow :).
You can put --also-make into the per-project .mvn/maven.config file which sets command-line options to always apply.
In combination with IntelliJ's option to "Delegate IDE build/run actions to Maven" (which seems to be default when importing Maven projects these days), this gets building and running with the IDE's main buttons and menu items working. No more failures to resolve inter-module dependencies or failing Enforcer.
There may be undesirable side effects to always including --also-make but so far I haven't encountered them—in a multi-module project using the Reactor this way is just about always what I want, so it saves typing on the CLI too.
Side note: I still find #Bastien Jansen's answer useful for invoking other run-like Maven goals, like spring-boot:run with IDEA Community Edition which does not have the more first-class support for Spring Boot applications that Ultimate does.

Resources