gpt4 book ai didi

java - Spring Boot Maven - 无法将 Web 应用程序打包为可执行 jar/war

转载 作者:行者123 更新时间:2023-12-02 12:27:29 24 4
gpt4 key购买 nike

我正在尝试学习如何使用 Spring Boot 和 MVC 构建 Web 应用程序。

我能够创建一个 Spring boot 应用程序,并通过执行 SpringBootWebApplication 类中的 main 方法从 Eclipse 运行它。它工作正常,我可以通过点击 http://localhost:8080 来启动我的应用程序。

现在,我想将我的应用程序打包为可执行的 WAR 或 JAR(我不知道哪一个是正确的!)。为此,我在 pom.xml

中添加了一个名为 spring-boot-maven-plugin 的构建插件
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sriram</groupId>
<artifactId>springboot</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springboot Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<!-- Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Tomcat Embed -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- To compile JSP files -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>


<build>
<plugins>
<!-- Package as an executable jar/war -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

郑重声明,我根据教程 here 创建了这个 pom

当我从命令提示符运行 mvn clean package -e 时,我收到如下错误:

[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building springboot Maven Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[WARNING] The POM for org.springframework.boot:spring-boot-maven-plugin:jar:1.5.1.RELEASE is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.890 s
[INFO] Finished at: 2017-08-01T13:03:42+05:30
[INFO] Final Memory: 8M/184M
[INFO] ------------------------------------------------------------------------
[ERROR] Plugin org.springframework.boot:spring-boot-maven-plugin:1.5.1.RELEASE or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.springframework.boot:spring-boot-maven-plugin:jar:1.5.1.RELEASE: 1 problem was encountered while building the effective model for org.springframework.boot:spring-boot-maven-plugin:[unknown-version]
[ERROR] [FATAL] Non-parseable POM C:\Users\Sriram\.m2\repository\org\springframework\boot\spring-boot-parent\1.5.1.RELEASE\spring-boot-parent-1.5.1.RELEASE.pom: in epilog non whitespace content is not allowed but got r (position: END_TAG seen ...</profiles>\n</project>\nr... @946:2) @ C:\Users\Sriram\.m2\repository\org\springframework\boot\spring-boot-parent\1.5.1.RELEASE\spring-boot-parent-1.5.1.RELEASE.pom, line 946, column 2
[ERROR] -> [Help 1]
org.apache.maven.plugin.PluginResolutionException: Plugin org.springframework.boot:spring-boot-maven-plugin:1.5.1.RELEASE or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.springframework.boot:spring-boot-maven-plugin:jar:1.5.1.RELEASE
at org.apache.maven.plugin.internal.DefaultPluginDependenciesResolver.resolve(DefaultPluginDependenciesResolver.java:117)
at org.apache.maven.plugin.internal.DefaultMavenPluginManager.getPluginDescriptor(DefaultMavenPluginManager.java:181)
at org.apache.maven.plugin.internal.DefaultMavenPluginManager.getMojoDescriptor(DefaultMavenPluginManager.java:286)
at org.apache.maven.plugin.DefaultBuildPluginManager.getMojoDescriptor(DefaultBuildPluginManager.java:241)
at org.apache.maven.lifecycle.internal.DefaultLifecycleMappingDelegate.calculateLifecycleMappings(DefaultLifecycleMappingDelegate.java:110)
at org.apache.maven.lifecycle.internal.DefaultLifecycleExecutionPlanCalculator.calculateLifecycleMappings(DefaultLifecycleExecutionPlanCalculator.java:266)
at org.apache.maven.lifecycle.internal.DefaultLifecycleExecutionPlanCalculator.calculateMojoExecutions(DefaultLifecycleExecutionPlanCalculator.java:217)
at org.apache.maven.lifecycle.internal.DefaultLifecycleExecutionPlanCalculator.calculateExecutionPlan(DefaultLifecycleExecutionPlanCalculator.java:127)
at org.apache.maven.lifecycle.internal.DefaultLifecycleExecutionPlanCalculator.calculateExecutionPlan(DefaultLifecycleExecutionPlanCalculator.java:145)
at org.apache.maven.lifecycle.internal.builder.BuilderCommon.resolveBuildPlan(BuilderCommon.java:96)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:110)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:309)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:194)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:107)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:993)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:345)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:191)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.eclipse.aether.resolution.ArtifactDescriptorException: Failed to read artifact descriptor for org.springframework.boot:spring-boot-maven-plugin:jar:1.5.1.RELEASE
at org.apache.maven.repository.internal.DefaultArtifactDescriptorReader.loadPom(DefaultArtifactDescriptorReader.java:339)
at org.apache.maven.repository.internal.DefaultArtifactDescriptorReader.readArtifactDescriptor(DefaultArtifactDescriptorReader.java:199)
at org.eclipse.aether.internal.impl.DefaultRepositorySystem.readArtifactDescriptor(DefaultRepositorySystem.java:296)
at org.apache.maven.plugin.internal.DefaultPluginDependenciesResolver.resolve(DefaultPluginDependenciesResolver.java:103)
... 27 more
Caused by: org.apache.maven.model.building.ModelBuildingException: 1 problem was encountered while building the effective model for org.springframework.boot:spring-boot-maven-plugin:[unknown-version]
[FATAL] Non-parseable POM C:\Users\Sriram\.m2\repository\org\springframework\boot\spring-boot-parent\1.5.1.RELEASE\spring-boot-parent-1.5.1.RELEASE.pom: in epilog non whitespace content is not allowed but got r (position: END_TAG seen ...</profiles>\n</project>\nr... @946:2) @ C:\Users\Sriram\.m2\repository\org\springframework\boot\spring-boot-parent\1.5.1.RELEASE\spring-boot-parent-1.5.1.RELEASE.pom, line 946, column 2

at org.apache.maven.model.building.DefaultModelProblemCollector.newModelBuildingException(DefaultModelProblemCollector.java:197)
at org.apache.maven.model.building.DefaultModelBuilder.readModel(DefaultModelBuilder.java:581)
at org.apache.maven.model.building.DefaultModelBuilder.readParentExternally(DefaultModelBuilder.java:1097)
at org.apache.maven.model.building.DefaultModelBuilder.readParent(DefaultModelBuilder.java:829)
at org.apache.maven.model.building.DefaultModelBuilder.build(DefaultModelBuilder.java:331)
at org.apache.maven.repository.internal.DefaultArtifactDescriptorReader.loadPom(DefaultArtifactDescriptorReader.java:321)
... 30 more
[ERROR]
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException

我的源代码中没有收到任何编译错误。鉴于当我从 Eclipse 中运行 main 方法时,我的应用程序能够启动,为什么我的 Maven 打包不起作用?我可以弄清楚以下内容:

根据堆栈跟踪,我打开了文件C:\Users\Sriram.m2\repository\org\springframework\boot\spring-boot-parent\1.5.1.RELEASE\spring-boot-parent -1.5.1.RELEASE.pom 使用 Notepad++,它确实有一些错误 - 我的意思是 XML 似乎格式错误,Notepad++ 也引发了解析错误。

虽然我可以找出问题所在,但我不确定 XML 在由我的 Maven pom 自动设置时是如何最终损坏的。我错过了什么?

谢谢,斯里拉姆

最佳答案

您(至少)有两件事需要纠正。

第一个:你的 Maven 构建失败:

[FATAL] Non-parseable POM C:\Users\Sriram.m2\repository\org\springframework\boot\spring-boot-parent\1.5.1.RELEASE\spring-boot-parent-1.5.1.RELEASE.pom: in epilog non whitespace content is not allowed but got r (position: END_TAG seen ...\n\nr... @946:2) @ C:\Users\Sriram.m2\repository\org\springframework\boot\spring-boot-parent\1.5.1.RELEASE\spring-boot-parent-1.5.1.RELEASE.pom, line 946, column 2

原因不是你实际的 pom 而是 spring-boot-parent-1.5.1.RELEASE.pom依赖关系似乎已损坏。
所以,首先删除C:\Users\Sriram.m2\repository\org\springframework\boot\spring-boot-parent\1.5.1.RELEASE\文件夹并重新启动构建以下载它。

第二个问题:要创建可执行的 jar 或 war,您必须使用 spring-boot:repackage目标。

来自plugin documentation :

repackage: create a jar or war file that is auto-executable. It can replace the regular artifact or can be attached to the build lifecycle with a separate classifier.

在您的 pom 中,您没有指定 repackage目标。
您仅指定 spring-boot-maven-plugin :

<build>
<plugins>
<!-- Package as an executable jar/war -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

因此将其替换为:

<build>
<plugins>
<!-- Package as an executable jar/war -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

关于java - Spring Boot Maven - 无法将 Web 应用程序打包为可执行 jar/war,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45431751/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com