Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Maven similar to npm?

As I have worked with npm which looks for dependencies in package.json file and download it for you. Similarly, I see a pom.xml file in Java project. Does maven looks in this file and download dependencies for me. Can I pass around this pom.xml file like package.json, rather than giving the dependency jars ? Are these tools similar and just build for different platforms ?

like image 447
Shubham Jain Avatar asked Jul 15 '16 05:07

Shubham Jain


People also ask

Can Maven be used for Nodejs?

The frontend-maven-plugin defines goals for specific tools like grunt, gulp, karma, whereas the nodejs-maven-plugin has just one generic run goals that lets you execute any Node. js-based tool.

Is npm same as Maven?

npm has a broader approval, being mentioned in 2605 company stacks & 2587 developers stacks; compared to Apache Maven, which is listed in 301 company stacks and 138 developer stacks.

What is the equivalent of Maven?

The rough equivalent to Maven in the . NET ecosystem is NuGet. NuGet files can be created using the IDE or command-line tools such as dotnet pack or nuget pack .

Is Maven same as Pip?

Pip installs system and project-level Python packages. Maven manages dependencies and the build process for Java projects.


1 Answers

Same tool, different language?

Maven is the most popular build and dependency resolution tool for Java, just like NPM is for JS. But it's not just the same tool for a different language. There are obviously huge differences between Java and JS builds, and these differences are directly visible in the way Maven operates. For example, while many JS tools rely on Git to do some heavy-lifting, Maven works with custom filesystem-based Maven repositories, as Maven predates Git and needs to handle binary artifacts, which Git historically didn't handle well. In Maven there's a clear separation between sources and binaries, while they are often the same thing in JS world.

Maven basics

Maven in its purest form follows a declarative model, where pom.xml (similar to package.json) defines different properties of the build, but contains no scripts. The disadvantage is it can be a challenge to fine-tune some aspects of the build without using scripts as you have to rely on plugins. The advantage is it can be easier to understand other builds just by looking at pom.xml, as they usually follow the same approach without too much customization. Gradle is a popular Groovy-based tool built on top of Maven standards and conventions, and is specifically designed to simplify pom.xml and break this "no script" barrier.

Referencing your dependencies

Similarly to package.json, you don't work with pom.xml of your dependency directly, but rather define dependency coordinates and let your build tool handle the rest. In Maven the basic form of these coordinates is GAV (groupId, artifactId, version).

Flat dependency tree?

Based on comments in the other answer, Maven provides "flat dependency tree", not "nested dependency tree" that NPM provides by default. Maven does not allow multiple versions of the same dependency. If it happens that different versions are requested, Maven uses dependency resolution to pick a single version. This means that sometimes your transitive dependencies will get a different version than they require, but there are ways to manage this. However, this limitation comes from Java, not Maven, as (normally) in Java a class loader will only provide access to a single class definition even if multiple definitions are found on the classpath. Since Java is not particularly good at handling this, Maven tries to avoid this scenario in the first place.

Note: since npm v3 the dependencies are flatten. The alternative package manager yarn also does the same.

Maturity

Furthermore, Maven is considerably older than NPM, has a larger user base, huge number of custom plugins, and so far could probably be considered more mature overall. Sometimes Maven is used for non-Java or even polyglot projects, as there are plugins for handling other languages or specific environments, such as Android. There are plugins that bridge Maven and other build tools, such as frontend-maven-plugin that actually handles multiple JS build tools.

like image 67
Anton Koscejev Avatar answered Sep 25 '22 14:09

Anton Koscejev