3

In an attempt to create an executable jar from a JavaFX Maven-based project running Java 1.8.0_121, I was using the command: mvn clean assembly:single. During the process, the following warning is printed:

[WARNING] Cannot include project artifact: groupid:artifactid:jar:1.0-SNAPSHOT; it doesn't have an associated file or directory.

The resulting jar file was built but it could not be executed. When running java -jar product.jar, the following error was being printed:

Error: Could not find or load main class groupid.artifactid.MainClass

The pom file:

<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>groupid</groupId>
    <artifactId>artifactid</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <organization>
        <name>org</name>
    </organization>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <build>
        <finalName>product</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>groupid.artifactid.MainClass</mainClass>
                        </manifest>
                    </archive>
                    <finalName>product</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

I double checked that there is a class named MainClass.java with a psvm entry point in the package structure groupid/artifactid/. Why is this happening and how can it be resolved?

RDM
  • 4,986
  • 4
  • 34
  • 43

1 Answers1

5

I've resolved the issue by executing the command

mvn clean compile assembly:single

The difference with before is the added goal compile. The resulting product.jar file in target/ is now executable and the warning is no longer printed. However, a different warning is now shown:

[WARNING] Replacing pre-existing project main-artifact file: /path/to/target/classes with assembly file: /path/to/target/product.jar

Can someone explain why this works?

RDM
  • 4,986
  • 4
  • 34
  • 43
  • I think I figured it out myself: by not specifying a phase of the default build cycle, the sources of the project itself were not being packaged into a jar, thus triggering maven in warning me that no jar exists. This explains why adding the **compile** phase ensures that the sources of the project itself are first compiled and then can be added. I'm guessing the last warning only happens because the final product jar is forced to have the same name as the original project, thereby overwriting the previous, non-executable build product. – RDM Mar 02 '17 at 10:01