note: I tried out the suggested answers in the "possible-duplicated"-questions, they did sadly not help me. I edited my question a bit.
我设置了一个 Spring-Boot 应用程序(可以从 Spring Tool Suite
IDE 执行)。现在我想构建 jar 文件并执行它。我尝试过的:
cd %path_to_my_project_root%
mvn clean package
cd target
java -jar myApp.jar
错误是(翻译后,可能是错误的措辞):
no main manifest attribute in myApp.jar
我尝试了不同平台上建议的不同解决方案(例如 here 和 here )。然而,据我了解,这似乎是我的 POM.xml
的问题。
我的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>
我查找了我的 Manifest.MF
,内容如下:
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
我似乎缺少什么:
我查找了 jar
文件,我注意到了一些事情:
- 我缺少
.jar.original
文件 - 我的
Manifest.MF
中没有Spring-Boot-Version
、Main-Class
、Start-Class
和Boot-Inf
参数 - 我的
jar
仅包含三样东西:META-INF
、com
(已编译的类)和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
最佳答案
您缺少此 Maven 属性:
<start-class>com.demo.MainClass</start-class>
(这必须包含调用 SpringApplication.run(..)
的主类)
关于java - Mvn clean package 未正确创建 JAR 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53648762/