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

时间:2017-08-01 07:40:20

标签: java eclipse maven spring-mvc spring-boot

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

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

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

中添加了一个名为pom.xml的构建插件
<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 ++也引发了解析错误。

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

谢谢, 斯利拉姆

1 个答案:

答案 0 :(得分:1)

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

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

  

[致命]不可解析的POM   C:\ Users \用户Sriram.m2 \库\组织\ springframework的\启动\弹簧引导家长\ 1.5.1.RELEASE \弹簧引导家长1.5.1.RELEASE.pom:   在epilog非空白内容是不允许但得到r(位置:   END_TAG见过...... \ n \ nr ... @ 946:2)@   C:\ Users \用户Sriram.m2 \库\组织\ springframework的\启动\弹簧引导家长\ 1.5.1.RELEASE \弹簧引导家长1.5.1.RELEASE.pom,   第946行,第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或战争,你必须使用 spring-boot:repackage目标。

来自plugin documentation

  

repackage:创建一个可自动执行的jar或war文件。它可以   替换常规工件或可以附加到构建生命周期   使用单独的分类器。

在你的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>