首页 文章

将现有WAR添加到嵌入式tomcat

提问于
浏览
1

我已经用Google搜索了这个问题,但我找不到任何针对此案例的内容 . 我发现了很多“如何将Spring Boot WAR部署到Tomcat”,但没有提到使用Spring Boot包装现有的Tomcat WAR .

我需要重新配置.1407756_t . This solution不起作用,因为它取决于绝对位置可用的WAR,而我们正试图在Spring Boot WAR中打包"application" WAR . 然后我们可以像这样添加WAR的上下文:

Context context = tomcat.addWebapp("myapp", Thread.currentThread().getContextClassLoader().getResource("myapp.war").getPath());

这几乎正常 . 我遇到一个特定问题的问题 . 当现有WAR文件被放入Spring Boot项目时,它将被放入 /WEB-INF/lib-provided 而不是 /WEB-INF/classes . 我可以't find a way to get the embedded Tomcat to add a WAR file from this location. The ClassLoader won' t加载它因为它不在 WEB-INF/classes 之下 .

/WEB-INF/lib-provided 获取此WAR是否有一种灵巧的方式(或任何方式)?

1 回答

  • 1

    对于其他需要执行此操作的人,答案是使用 maven-dependency-plugin 并将 spring-boot-maven-plugin 设置为从资源中排除WAR文件(否则您将获得Spring Boot WAR中包含的两个WAR副本):

    <!-- include your WAR as a resource instead of a dependency -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <excludeTransitive>true</excludeTransitive>
                            <includeArtifactIds>my-war-name-here</includeArtifactIds>
                            <stripVersion>true</stripVersion>
                            <outputDirectory>${project.basedir}/src/main/resources</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    
    
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.5.3.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <!-- Don't copy the war as a dependency, it's included as a resource -->
                            <excludeArtifactIds>my-war-name-here</excludeArtifactIds>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    

相关问题