1

note: I tried out the suggested answers in the "possible-duplicated"-questions, they did sadly not help me. I edited my question a bit.

I set up a Spring-Boot application (that can be executed from the Spring Tool Suite IDE). Now I wanted to build the jar-file and execute it. What I tried:

cd %path_to_my_project_root%
mvn clean package
cd target
java -jar myApp.jar

The error is (translated, maybe incorrect wording):

no main manifest attribute in myApp.jar

I have tried different solutions suggested on different platforms (e.g. here and here). Yet, as far as I understood, it seems to be a problem with my POM.xml.

My Pom:

<?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.demo</groupId>
    <artifactId>myApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>myApp</name>
    <description>desc</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-ldap</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.restdocs</groupId>
            <artifactId>spring-restdocs-mockmvc</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <type>jar</type>
        </dependency>
     </dependencies>

     <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>validate</phase>
                            <goals>
                                <goal>exec</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <executable>ng</executable>
                        <workingDirectory>src/main/ui</workingDirectory>
                        <argument>
                            <argument>build</argument>
                        </argument>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

I looked up my Manifest.MF and this was the content:

Manifest-Version: 1.0
Implementation-Title: myApp
Implementation-Version: 0.0.1-SNAPSHOT
Built-By: me
Implementation-Vendor-Id: myPackage
Created-By: Apache Maven 3.6.0
Build-Jdk: 1.8.0_191
Implementation-URL: my_Url

What I seem to be missing:

I looked up the jar-file and there were a few things I noticed:

  • I am missing a .jar.original-file
  • There is not Spring-Boot-Version, Main-Class, Start-Class and no Boot-Inf argument in my Manifest.MF
  • My jar contains only three things: META-INF, com (the classes, compiled) and the application.properties-file. There is usually a lot more stuff in it.

My Application.properties

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://external.url/dbname?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=user
spring.datasource.password=pass

spring.jpa.database = MYSQL
spring.jpa.show-sql = true
Rüdiger
  • 893
  • 5
  • 27
  • 56
  • Possible duplicate of [How can I create an executable JAR with dependencies using Maven?](https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven) – Mark Dec 06 '18 at 10:02
  • Or simply provide an entrypoint when calling java -jar : `java -jar myApp.jar your.own.ClassWithMainMethod` – Aaron Dec 06 '18 at 10:04
  • Possible duplicate of [How do I run a spring boot executable jar in a Production environment?](https://stackoverflow.com/questions/22886083/how-do-i-run-a-spring-boot-executable-jar-in-a-production-environment) ; there's an `executable` tag you can set on the `spring-boot-maven-plugin` plugin configuration to make the jar executable, seems like the most appropriate for your context – Aaron Dec 06 '18 at 10:06
  • @Mark I'm confused about this - i am going to try this but I had an earlier pom.xml (going to add it in my question) where these things were not necessary – Rüdiger Dec 06 '18 at 10:30
  • @Aaron Adding the executable-flag did not change the outcome – Rüdiger Dec 06 '18 at 10:38
  • @Aaron You can't specify the entrypoint when using `-jar`. Did you mean `-cp` instead? – Mark Rotteveel Dec 06 '18 at 14:39
  • @MarkRotteveel Wow, I was sure you could, weird. I guess I haven't executed a jar from the command line for too long. `java -cp myApp.jar your.own.ClassWithMainMethod` it is then, yes. – Aaron Dec 06 '18 at 14:44

2 Answers2

1

you are missing this maven property:

<start-class>com.demo.MainClass</start-class>

(This must contain your Main-class that calls SpringApplication.run(..))

markusw
  • 1,975
  • 16
  • 28
1

I removed the <pluginManagement>-tag in the plugins and had no problem building it afterwards. I have no idea what that Tag does (that it should not do) but it seems like this was the trigger for the problem. Thanks to everyone who tried to help me there.

Rüdiger
  • 893
  • 5
  • 27
  • 56
  • 1
    I can't believe this Answer helped me after two days of fiddling around with the assembly plugin not creating my -jar-with-dependency.jar. Even this Answer is almost three years old. Thank you very much sharing this information. This tag is created by default when i create a new project with maven on the terminal - and i have no idea why it is in my pom. – FreakErn Oct 15 '21 at 08:17