Reading:
Maven Package as single Jar

Maven Package as single Jar

Metamug
Maven Package as single Jar

To package a Maven project into a single JAR file, you can use the "maven-jar-plugin" in your project's pom.xml file. The plugin's "jar" goal creates a JAR file with the compiled classes and resources of the project.

Here is an example of how to configure the maven-jar-plugin in your pom.xml file:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.2.0</version>
      <executions>
        <execution>
          <goals>
            <goal>jar</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

This will package the project into a JAR file with the default settings, which includes all the compiled classes and resources in the project's "target/classes" directory.

You can also specify the finalName of the JAR and its location by adding the following configuration:

<configuration>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <outputDirectory>${project.build.directory}</outputDirectory>
</configuration>

You can also include dependencies of the project by adding the following configuration:

<configuration>
    <archive>
        <manifest>
            <addClasspath>true</addClasspath>
            <mainClass>com.example.Main</mainClass>
        </manifest>
    </archive>
</configuration>

This will create a JAR file in the "target" directory of your project with the name specified in finalName.

It is important to note that the Main class should be specified in the fully-qualified format, that is package name and class name should be specified. Also, you should make sure that Java is installed and the JAVA_HOME environment variable is set properly on your system for this to work.

Run as single jar

To run a Maven project with its dependencies, you can use the "maven-shade-plugin" to package the dependencies into a single JAR file. The shade plugin allows you to create a "fat" JAR file, which includes all the dependencies of the project.

Here is an example of how to configure the maven-shade-plugin in your pom.xml file:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>3.2.4</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

This will package the project and its dependencies into a single JAR file named projectName-version-shaded.jar in the "target" directory of your project.

You can also specify the finalName of the JAR and its location by adding the following configuration:

<configuration>
    <finalName>${project.artifactId}-${project.version}-shaded</finalName>
    <outputDirectory>${project.build.directory}</outputDirectory>
</configuration>

Once you've added the plugin configuration to your pom.xml file, you can create the shaded JAR by running the following command:

mvn package

To run this JAR you can use the following command:

java -jar target/<finalName>.jar


Icon For Arrow-up
Comments

Post a comment