Search code examples
javamavenpom.xml

ways to generate jar file from pom.xml


Wanted to check , what produces a jar file from pom.xml with no plugins included? Let say I a class file

my-app/
|-- src/
    |-- main/
        |-- java/
            |-- com/
                |-- example/
                    |-- MyClass.java
|-- pom.xml

Myclass.java
package com.example;

public class MyClass {
    public static void main(String[] args) {
        System.out.println("Hello, Maven!");
    }
}
pom.xml

<?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>my-app</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <build>
    <finalName>model</finalName>
    </build>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

should the above pom.xml without plugins produce a jar file ?


Solution

  • If you take a look at the Packaging section of Introduction to the lifecycle in the documentation, you can see that it will execute jar:jar (the jar goal of the maven-jar-plugin) in the package phase by default.

    You can also see this in the Usage section of the maven-jar-plugin documentation.

    Usually there is no need to mentioned the 'maven-jar-plugin' explicit cause it's bound to the Maven Build Life Cycle.

    This is assuming you are using the jar packaging which is the default. In your example, you even set this explicitely with <packaging>jar</packaging> in your pom.xml.

    The jar goal of the maven-jar-plugin will create a JAR from your compiled project sources and resources. However, it won't contain any dependencies.