#Maven compared with npm
AddedIn Node.js, package.json lists your dependencies and scripts, npm install downloads packages from the npm registry into node_modules, and npm test runs a script. Maven does the same jobs for Java with different names, and one big difference in philosophy:
| npm | Maven | Note |
|---|---|---|
package.json | pom.xml | the project file ("Project Object Model"), in XML |
"name", "version" | groupId, artifactId, version | the coordinates: com.example:shop:0.0.1-SNAPSHOT |
| npm registry | Maven Central | where libraries are published |
node_modules/ per project | ~/.m2/repository/ shared | each JAR is downloaded once per machine and shared by every project |
"scripts" you write | the lifecycle (compile, test, package) | the steps are standard; plugins do the work |
npx, Corepack | ./mvnw (Maven Wrapper) | pins the Maven version per project |
package-lock.json | none by default | versions in a pom are exact; Spring Boot's parent picks matching versions for you |
The philosophy difference: npm runs scripts you write; Maven runs a fixed build lifecycle that every project shares. You don't write "how to compile" or "how to test" in a pom. You say what the project is (coordinates, dependencies, Java version) and Maven already knows the steps: compile src/main/java, compile and run the tests in src/test/java, package a JAR. That's convention over configuration: every Maven project in the world has the same folder layout and the same commands, so you can open any of them and know where things are.
Here's a complete Maven project with no framework: one class, one test, and one dependency (JUnit, for tests only).
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>pricing</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.release>25</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>6.1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>package com.example.pricing;
public class Discount {
public static long apply(long cents, int percent) {
if (percent < 0 || percent > 100) {
throw new IllegalArgumentException("percent must be 0-100, was " + percent);
}
return cents * (100 - percent) / 100;
}
}package com.example.pricing;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
class DiscountTest {
@Test
void takesPercentOff() {
assertEquals(7500, Discount.apply(10000, 25));
}
@Test
void rejectsPercentOver100() {
assertThrows(IllegalArgumentException.class, () -> Discount.apply(10000, 120));
}
}$ ./mvnw test
[INFO] Results:
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS- The pom's coordinates name the project
com.example:pricing:1.0.0. If you published it, another project would depend on it with exactly those three values. maven.compiler.releaseis 25, the same asjavac --release 25(chapter 01). The encoding line stops a platform-dependent default from creeping in.- The one dependency is JUnit with
<scope>test</scope>: it's on the classpath when tests compile and run, and left out of the main code and the final JAR. npm's equivalent isdevDependencies. - Nothing in the pom says where the code is or how to test it. Maven looks in
src/main/javaandsrc/test/javaby convention, and its default test plugin (Surefire) findsDiscountTestbecause its name ends inTest. ./mvnw testruns every lifecycle phase up totest: it downloads JUnit into~/.m2the first time, compiles both folders, then runs the tests. The summary saysTests run: 2, Failures: 0, Errors: 0, Skipped: 0andBUILD SUCCESS.
npm's "^1.2.0" means "1.2.0 or any newer 1.x" and a lock file freezes what you actually got. Maven dependencies are normally written as one exact version, so the pom itself is the lock. The price is that upgrading is manual, which is why tools like Dependabot or Renovate (or ./mvnw versions:display-dependency-updates) matter in Java teams. And when two libraries want different versions of the same third library, Maven doesn't install both as npm would: only one version of a class can be on the classpath, and Maven picks the one nearest to your project in the dependency tree. The next topic shows that tree.