hjalpmig
hjalpmig

Reputation: 702

Maven - Configuring POM to execute JAR

I have a simple Hello World application 'App' which has been made in a Maven project. I'm aware that after building the project, the Maven assembly plugin is required in order to execute the JAR which is created when the project is built. I have followed the instructions found at:

http://maven.apache.org/plugins/maven-assembly-plugin/usage.html

But after editing my pom.xml and re-building the project, I still cannot run my JAR file.

Here is my 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>mini-project-6-ex6</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
</properties>
<build>
<plugins>
  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.5.4</version>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
      <archive>
        <manifest>
          <mainClass>com.mycompany.mini.project.ex6.App</mainClass>
        </manifest>
      </archive>
    </configuration>
  </plugin>
</plugins>
</build>

Is there a mistake in my POM or additional lines which I have left out which is causing me to not be able to run the resulting JAR file?

EDIT: It seems as though the JAR has no manifest attribute (see comments). I suppose that I could open the jar using 7zip or similar program and manually add a manifest file, but the real question is how to create the JAR using Maven to include a manifest file in the first place.

Upvotes: 1

Views: 1184

Answers (1)

Aaron Digulla
Aaron Digulla

Reputation: 328830

You're looking at the wrong JAR. The POM above will create at least two JARs:

mini-project-6-ex6-1.0-SNAPSHOT.jar
mini-project-6-ex6-1.0-SNAPSHOT-jar-with-dependencies.jar

The former is only the code of the module, the latter is the code plus all the dependencies.

If you run mvn install, you will only get the first one since the Assembly Plugin isn't configured to run automatically. You can run it manually with mvn assembly:assembly.

If you want it to run automatically when you mvn install, you need to change the POM:

<plugins>
  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.5.4</version>
    <configuration>
      ...
    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id> <!-- this is used for inheritance merges -->
        <phase>package</phase> <!-- bind to the packaging phase -->
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

See http://maven.apache.org/plugins/maven-assembly-plugin/usage.html#Execution:_Building_an_Assembly

Upvotes: 1

Related Questions