使用spring-boot-dependencies代替spring-boot-starter-parent,jar启动报错 没有主清单属性解决

2022-07-04 08:16:10 浏览数 (1)

maven项目中不使用spring-boot-starter-parent作为上级项目,改为导入spring-boot-dependencies管理依赖

代码语言:javascript复制
<dependencyManagement>
	<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${spring-boot.version}</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
</dependencyManagement>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

项目构建后,直接执行jar包会报错:

jar中没有主清单属性

此外还会遇到如无法在配置文件中使用maven占位符等问题

代码语言:javascript复制
version: @project.version@

报错:

ERROR org.springframework.boot.SpringApplication - Application run failed org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token found character ‘@’ that cannot start any token. (Do not use @ for indentation) in ‘reader’, line 1, column 10: version: @project.version@

解决

  • 配置spring构建插件
代码语言:javascript复制
<build>
	<plugin>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-maven-plugin</artifactId>
	    <executions>
	        <execution>
	            <id>repackage</id>
	            <goals>
	                <goal>repackage</goal>
	            </goals>
	        </execution>
	    </executions>
	</plugin>
</build>
  • 配置资源过滤,支持配置文件使用maven占位符
代码语言:javascript复制
<build>
	<resources>
        <resource>
            <directory>${basedir}/src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/application*.yml</include>
                <include>**/application*.yaml</include>
                <include>**/application*.properties</include>
            </includes>
        </resource>
        <resource>
            <directory>${basedir}/src/main/resources</directory>
            <excludes>
                <exclude>**/application*.yml</exclude>
                <exclude>**/application*.yaml</exclude>
                <exclude>**/application*.properties</exclude>
            </excludes>
        </resource>
    </resources>
</build>

0 人点赞