Remove unnecessary dependencies in POM - java

I am new to Maven and I faced a problem when I tried to convert my current project from using Ant -> using Maven.
That project requires many Jars, and I look up those jars on mvnrepository and add all to POM.xml.
I don't know if some dependency is redundant.
Do you guy know any way to check if which dependency which I really need?

The Maven dependency analyzer plugin is just what you're looking for.
Just run
mvn install dependency:analyze
(on some platforms, for some reason, the full notation is required:)
mvn install org.apache.maven.plugins:maven-dependency-plugin:2.9:analyze
And review the report it produces.

Related

Gradle Plugin dependency

What is the exact dependency I need to develop a Gradle Plugin in Java? Ideally I would like to get it from a well-known repository such as Maven Central or similar.
I have a Maven project with a core functionality and I just added two extra plugins, one for Ant, one for Maven. They are already tested and working; easy! Now, I wanted to add a third module for a Gradle plugin to make this functionality also available from any Gradle project.
However, I can't find the exact dependencies I need to develop a Gradle plugin.
The Gradle docs (such as https://docs.gradle.org/current/userguide/java_gradle_plugin.html) are not very well written to say the least. They mention:
the gradleAPI() dependency
or the java-gradle-plugin dependency
But they are quite unclear... no group, no version (really?).
If anyone can enlighten me to where I can get these dependencies from, I would be very thankful.
Gradle's public and internal APIs, aka gradleApi(), are bundled with the Gradle distribution and not independently published and therefore not easily consumable by Maven builds. There's the pending epic #1156 (Ensure plugin cross-version compatibility by allowing a user to depend on gradlePublicApi()) that might help here.
Since Gradle plugins are best to be built with Gradle, a pragmatic solution is to invoke the Gradle build from Maven and attach the produced artifact to the Maven build. Andres Almiray (aalmiray) once described this in the blog post Running Gradle Inside Maven (Web Archive Link). He describes the following high level steps:
Create a new Maven module (e.g. gradle-plugin) and add attach it to the parent POM
In the POM of gradle-plugin add a dependency to your core module. Use the maven-dependency-plugin to store dependencies to the Maven build folder, e.g. target/dependencies.
Create the build.gradle, add a Maven repository that points to target/dependencies (step 2) and let it depend on the core module as well as gradleApi(). Implement the Gradle plugin.
Use the exec-maven-plugin to invoke the Gradle build.
Use the maven-resources-plugin to copy the Gradle built plugin jars to the standard Maven build folder.
Use the build-helper-maven-plugin to attach the copied jars to the Maven build.
Sample project to be found here (gradle-in-maven).
https://docs.gradle.org/current/userguide/custom_plugins.html#sec:custom_plugins_standalone_project
In here it is mentioned that it is gradleApi() and I know that this works (from experience). The localGroovy() on that page is only needed if your plugin code uses groovy (does not apply if you only use groovy in the build.gradle of your plugin).
java-gradle-plugin is a library that makes it a bit simpler to make plugins, it is not required though. I personally prefer using gradleApi only.
EDIT:
It appears I've misunderstood the question. Here are the steps to get gradleApi jar:
Create a Gradle project with your desired Gradle version.
Add implementation gradleApi() dependency.
Import/run the project once.
Go to your .gradle folder (located in home folder in Linux-based operating systems).
Open caches folder
Open the version folder you want, e.g. 6.0.1
Open generated-gradle-jars folder.
Copy the jar to wherever you want and use it.
For me the 6.0.1 jar is at ~/.gradle/caches/6.0.1/generated-gradle-jars/gradle-api-6.0.1.jar
Please note that I have not tested this, I know the jar is there but I haven't tried using it.

Installing a framework without maven or gradle

I try to install Javalin framework for creating an API on my Java project. (old java 8 project without maven, gradle, etc). I would like to install the framework with adding the jars to my build path.
But If I add the main jar file then it needs another dependencies jar , then another one another one another one.. etc.
Is there any simple way to add this to my project and all it's dependencies without any build tool like Maven,etc?
I have tried adding it manually , but each jar has many dependencies that it is almost impossible(?)
Well you could create a Maven project and use it to download the dependencies for you.
Maven dependency plugin might be useful. With it you could just call:
mvn dependency:copy-dependencies
and it will download all your dependencies into target/dependency.
I don't think there's a way, I'm afraid.  Dependency management is the exact problem that build tools like Maven and Gradle were created to solve!
The framework supplier could provide a ‘fat’ jar including all the dependencies; but I'm not aware of any that do, as everyone uses Maven or Gradle (or SBT or Ivy or Grape or Leiningen or Buildr).
I think the only real alternative is to do it manually — which, as you've discovered, can be a horrible and lengthy task if the dependency tree is big.  (And would need redoing with every update.)
So I'd suggest biting the bullet and using Maven if you can.

How to tell maven that I need the javafx classes?

That looks easy enough: search the net in which package they are, copy the dependency into your pom.xml and here you go!
But I didn't didn't anything that I could use as a dependency.
Not much of a surprise, other people had the same problem, and solutions can found here https://stackoverflow.com/a/29270114/4142984 in combination with https://stackoverflow.com/a/15692230/4142984 .
In other words, those solutions suggest to get the jar manually and hard-link it in you build-path. It worked, though.
But isn't maven supposed to do that?
Question is: what did I miss, to tell maven to do this.
And I'm using maven with eclipse, just in case this would make a difference.
The links you provided suggest adding a JAR to your project CLASSPATH. That's not using Maven.
If you want to have Maven manage that dependency, and it's not in a Maven Central repo, you need to do an mvn install to your enterprise or local Maven repository.
I'm not if this Maven plugin is what you need. Maybe you can consider that as well.

Maven war contains extra dependencies if not built standalone

I've got a war which I'm trying to reduce the dependencies contained in it's WEB-INF/lib as they're provided by the ear and if they're only in the ear it resolves some classloader issues I've had.
If I run mvn clean install in the module for the war it builds in only the dependencies I want. If I go up a level and run mvn install on the project (the war's defined as a module in the pom.xml at this level) then the war is repackaged with a lot more dependencies.
Why would the behaviour be different and how can I stop all these extra dependencies getting in?
I've tried following the advice here but saw no difference in the behaviour: http://maven.apache.org/plugins/maven-war-plugin/examples/skinny-wars.html
Maven 2.0.6, maven-ear-plugin 2.4.1, maven-war-plugin 2.1-beta-1 (I can't change the versions, I'm on a closed network). Thanks in advance.
ETA: A mvn clean install at the project level gives me the results I want. An mvn clean and then an mvn install adds in the extra jars that I don't want.
Run mvn dependency:tree to see where the (probably transitive) dependencies are coming in from.
You'll want to read "Maven the Definitive Guide" (free PDF by Sonatype) in order to get an in depth understanding of how Maven deals with Dependency management.
And please do find a way to upgrade to maven 2.1.0 (preferably 2.2.1), and the latest versions of the plugins, earlier versions are notoriously buggy.

Ivy loading unnecessary dependencies?

I'm migrating a maven project to ant + ivy (it's an arbitrary decision, and it has to be done ).
The thing is that comparing both WAR files, the ant and the maven one, the former has a lot more 3rd party jars than the first one...
At first I thought that they were transitive dependencies but when I run mvn dependency:tree they don't appear there.
Any ideas?
EDIT:
Just for the record, I'm aware of the scope attribute of maven. I'm excluding all the provided and test dependencies in the ivy.xml file
You can use the ivy:report Ant task to generate a report of the dependencies resolved by Ivy. In this report, you can see where the extra dependencies are comming from.
Maarten

Categories