Kotlin + Maven assembling: no main manifest attrib

2019-07-09 09:09发布

问题:

I have problem assembling Kotlin project with some dependencies to JAR file using Maven.

How am i assembling project to JAR:

RightPanel -> MavenProjects -> Lifecycle -> package -> run

When i running JAR file:

java -jar path.jar

I'm getting following error:

no main manifest attribute, in path.jar

I've added maven-assembly-plugin like here:

So my plugins directory looks like this:

<build>
    <sourceDirectory>src/main/kotlin</sourceDirectory>
    <testSourceDirectory>src/test/kotlin</testSourceDirectory>

    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals> <goal>single</goal> </goals>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>${main.class}</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <version>${kotlin.version}</version>
            <executions>
                <execution>
                    <id>compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>test-compile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

main.class property defined here:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <kotlin.version>1.1.51</kotlin.version>
    <junit.version>4.12</junit.version>
    <main.class>ru.icarumbas.main.HelloKt</main.class>
</properties>

Some facts:

  • Hello.kt is starter class and it has fun main(...){}
  • When i unarchive JAR it has META-INF folder.

Versions:

  • Platform: Mac OS
  • Kotlin version: 1.1.51
  • Maven version: 4.0.0

Maybe i'm missing something. I've already looked on a lot of questions alike this, and they didn't help me. So please don't mark this as duplicate and write a comment if you want to see more code. I didn't show my full pom.xml file so write me if you want to see it full.

回答1:

I've got such problems with kotlin and maven. I've solved it in following way (Application.kt):

package org.somepackage

open class Application {
    companion object {
        @JvmStatic fun main(args: Array<String>) {
            // something..
        }
    }
}

-- So first i've written the kotlin class with main inside it.

Secondly as in your case defined the main class in props in a following way <main.class>org.somepackage.Application</main.class>

After mvn clean install command finished i've got the necessary someapp-1.0-SNAPSHOT-jar-with-dependencies.jar which was able to work.

And at the end please pay attention that you run exactly jar-with-dependencies.jar (not a stripped version like a app-1.0-SNAPSHOT.jar).



回答2:

it seems like you have one of the plugins missing. add the bellow to pom.xml file

`<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>
                            yourclassnameKt
                        </mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>`

The plugin will add MANIFEST.mf file, which will tell the Java runtime which class to execute.

There is another issue that has the same problem -> Maven Jar Builder: Could not find or load main class

Reference: https://michaelrice.com/2016/08/hello-world-with-kotlin-and-maven/



回答3:

Your POM file is correct, no need to add anything. Just run the generated JAR postfixed with *-jar-with-dependencies.jar in target directory.



标签: maven jar kotlin