Removing unused dependencies form pom.xml - java

I am working on a huge spring project and I want to optimize the build time for the project.
So, I ran the dependency:analyze to find out there are more than 400 unused dependencies in the project.
Is there any tool or maven command to update the original pom.xml by removing unused dependencies.
Removing 400+ dependencies manually is obviously not a good idea.

If dependencies are your bottleneck, then set up an internal Maven repository mirroring the world and tell your build process to use that.

Related

Maven dependency tree / hierarchy not showing the truth

I was going to migrate an old Ant project existing of multiple single java projects to a multi-module maven project. All the libraries for have been stored in one local folder.
For building up the dependency management I wanted to go the way to add all dependencies to the parent pom.xml (dependency management section) and also do my best by adding the correct ones to the children (Java Maven projects) until compilation is working.
I then want to streamline the pom.xmls by using "mvn dependency:tree -X" to see if I have added some transitive dependencies to the single Maven projects which aren't not needed to be explicitly added to the pom.xmls
Now when comparing the dependency hierarchy for a Maven project shown in Eclipse with that shown by using "mvn dependency:tree -X", there are some differences:
Maven will not show that "jetty-http" is actually a transitive dependency of jetty-server - mostly because I already added it as direct dependency in the pom.xml before.
While Eclipse does show that relationship and this is the correct result (also checked it manually using Maven central dependency list).
So in the end when using Maven I would have left "jetty-http" as direct dependency in my pom.xml, although I don't have to. This is kind of useless.
Does anyone know why the Maven dependency tree is so limited? I want to understand what is going on here. Is there any alternative using Maven commands?
Or is there even a better way to check for/identify transitive dependencies added to the pom.xml by mistake?
Best practise is to add all dependencies to the POM that are used directly in your source code (and at runtime).
So if X is used in your source code, but is already present as transitive dependency of Y, you should nevertheless add X as direct dependency.
You can check this with mvn dependency:analyze
https://maven.apache.org/plugins/maven-dependency-plugin/analyze-mojo.html

Maven war/jar size is so big

I'm building a project where the final war/jar size is very big. I am not sure how to reduce the size of the jar.
How to build only the jars required for the project?
Currently we just given maven build plugin in the dependencies and artifact id on the top which builds the war or jar
Please advise.
Thanks.
use dependency:analyze on your project to analyze used and unused dependencies, so you can exclude unwilling dependency files, i search a lot for a plugin to do it automatically, but I get disappointed in it. if you find please inform me.
https://maven.apache.org/plugins/maven-dependency-plugin/analyze-mojo.html
JAR files typically don't include dependencies, so unless you're building a so-called fat JAR, they should never get very big.
For WAR files:
Remove unneeded dependencies from your POM.
Optionally, move static assets (images etc.) to a web server instead of including them in your application.
If you have dependencies that are being used by multiple applications, include them on the application server level instead of inside your WAR file.
You can use the combination of two mvn commands:
1] mvn dependency:tree -> to understand the entire structure of jars in your project
2] mvn dependency:analyze -> as mentioned above to understand used and unused dependencies
after that you can start removing unused dependencies and always make sure to do mvn clean install and run your project locally to see if there are dependency issues.

Custom maven goal as part of project? (MOJO)

I've been working on this all day and can't seem to find a solution to fit my requirements.
Addition to compile life cycle that generates code. (I thought a MOJO would work here)
MOJO depends on compiled code to generate 'new' parts.
MOJO is only ever going to be required for this maven project.
I cannot alter the definitions of the existing maven project which
is a fully compliant maven infrastructure and packaging of jar. (So
I cannot create the MOJO as a module of this)
Is there any way I can create this step as part of the maven lifecycle?
I've tried a dozen different routes and they either cause issues as I try to declare the dependency as it self and goes into an infinite loop, or if I make the MOJO as a child of the project (part of the src), I have no way to automatically compile the MOJO's project during the compilation of the main maven project.

Can't build plugin because of circular dependencies

My plugin consists of a collection of other plugins written by myself and these plugins have circular dependencies in their class paths (and I don't see a way to remove those because the plugins rely on each other).
The plugin runs just fine when testing after I have set eclipse to mark those dependencies as warnings and not as errors. However when I try to build my plugin via the site.xml or the export wizard it always tells me
A cycle was detected when generating the classpath
raven.sqdev.preferences_0.2.0, raven.sqdev.util_0.3.0,
raven.sqdev.preferences_0.2.0.
and quits the building process.
I already found the option to allow circular dependencies in the export wizard but the result stays the same.
Has anyone an idea how I can build my plugin with these circular dependencies?

Make maven generate dependencies even with errors

This feels like a really stupid question but I haven't been able to find an answer.
I'm working on a maven project but I do most of my development in eclipse. Is there any way for me to force maven to generate all of my dependencies under target even if there are errors in the code? I set my eclipse project's build path to use the jars under target/dependencies/jars, but calling mvn clean kills them and if there are any errors in my code causing it to not compile mvn package won't create the dependencies but will instead just crash saying BUILD FAILURE. This makes the problem even worse since instead of seeing the actual errors my eclipse will just bombard me with errors everywhere since all of its dependencies just died.
Or maybe the way I'm working with it is just stupid and there's a better way.
Are you using the m2e plugins for Eclipse to process maven projects, or simply importing the projects as general ones?
If the latter, you should use the m2 plugins (simply go to the Eclipse Marketplace and search for Maven), as they interrogate your POM and set up your dependences properly. You can then concentrate on any compile errors in your code.
You should not point to the jars in the target folder for dependent JAR's since this is where the products of building your project are stored. Performing a mvn clean removes this folder.
To use Maven with Eclipse install the m2e plugin in Eclipse. This makes Eclipse understand the structure of Maven projects.
Once installed you can import your Maven project into Eclipse. I use Import... | Existing Maven Projects for this. But you can also directly import form a versioning system.
During the import Eclipse will set up the Eclipse project to use the Maven dependencies to locate the required JAR's. These are taken from the repository as configured with the used Maven installation.

Categories

Resources