Keywords: Spring Boot | Maven | repackage error
Abstract: This paper provides an in-depth analysis of the "Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.0.2.RELEASE:repackage failed: Source must refer to an existing file" error that occurs when executing mvn package in Spring Boot projects. By examining the error stack trace and POM configuration, it identifies that setting the packaging type to pom is the root cause. The article explains the working mechanism of the Spring Boot Maven plugin's repackage goal, compares the differences between pom and jar packaging types, and offers comprehensive solutions including changing packaging to jar and simplifying plugin configurations. It also discusses the relationship between Maven build lifecycle and plugin execution, providing practical guidance for developers to avoid similar errors.
Error Phenomenon and Background Analysis
During the development of Spring Boot projects using Maven for building, developers may encounter the following error message: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.0.2.RELEASE:repackage failed: Source must refer to an existing file. This error typically appears when executing the mvn package command, indicating that the repackage goal of the Spring Boot Maven plugin has failed. From the error stack trace, it can be seen that the exception originates from the constructor of the org.springframework.boot.loader.tools.Repackager class, indicating that the source must refer to an existing file.
Root Cause Investigation
By analyzing the provided POM.xml configuration file, a critical issue can be identified: <packaging>pom</packaging>. In Maven, the packaging type defines the output format of the project, with common types being jar, war, pom, etc. When packaging is set to pom, Maven treats it as a parent or aggregation project and does not generate an executable JAR file. The repackage goal of the Spring Boot Maven plugin is specifically designed to repackage the compiled JAR file to make it an executable Spring Boot application.
Specifically, the repackage goal executes after the package phase in Maven's lifecycle. It requires an existing JAR file (the one generated by standard Maven packaging) as the input source. When packaging is pom, Maven does not generate this JAR file, causing the repackage goal to fail to find the required source file, thus throwing the "Source must refer to an existing file" exception.
Detailed Solution
According to the best answer guidance, the core step to solve this problem is to change the packaging type from pom to jar:
<packaging>jar</packaging>This modification ensures that Maven generates a standard JAR file during the package phase, providing the necessary input for subsequent repackage operations. Additionally, the answer suggests simplifying the POM configuration:
- Remove unnecessary configurations in
repositoriesandpluginRepositories, unless the project specifically requires particular repositories. - Delete explicitly specified versions of
maven-compiler-plugin, as the Spring Boot parent POM provides appropriate default configurations. - Simplify the
spring-boot-maven-pluginconfiguration by removing theversionandexecutionssections, allowing the plugin to inherit default settings from the parent POM.
An example of the simplified plugin configuration is as follows:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>Maven Build Lifecycle and Plugin Execution Mechanism
Understanding this error requires grasping the basic concepts of the Maven build lifecycle. Maven's lifecycle consists of a series of phases, such as compile, test, package, install, etc. When executing mvn package, Maven runs all goals up to and including the package phase in sequence. The repackage goal of the Spring Boot Maven plugin is by default bound to the package phase, meaning it automatically executes after standard packaging is completed.
The alternative solution mentioned in the supplementary answer, mvn clean install spring-boot:repackage, provides another approach: ensuring the JAR file is installed to the local repository via the install phase, then explicitly invoking the repackage goal. However, this method is only a temporary fix; modifying the packaging type is the fundamental solution.
Practical Recommendations and Conclusion
To avoid similar errors, developers should note the following when creating Spring Boot projects:
- Ensure the packaging type is correctly set to jar (or war for web applications).
- Fully utilize Spring Boot's convention-over-configuration principle, avoiding unnecessary plugin version overrides.
- When encountering build errors, carefully read the error stack trace to understand the specific context of plugin execution.
Through the analysis in this paper, we have not only resolved the specific "Source must refer to an existing file" error but also gained a deeper understanding of the integration mechanism between Spring Boot and Maven. Correct packaging configuration is fundamental to the successful building of Spring Boot applications, while reasonable POM simplification enhances project maintainability.