gpt4 book ai didi

maven - Tomcat 不提供来自 maven-war-plugin 的文件

转载 作者:行者123 更新时间:2023-11-28 21:55:28 26 4
gpt4 key购买 nike

我正在尝试让我的 Tomcat 服务器提供一个文件。我做了一个非常简单的示例来向您展示问题所在,即使很简单,它也不起作用。

我的项目是这样制作的:

test
|->assets
| |->testB.txt
|->src
| |->main
| | |->webapp
| | | |->WEB-INF
| | | | |->web.xml
| | | |->testA.txt
|-> pom.xml

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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>test</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<webResources>
<resource>
<directory>assets/</directory>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
</web-app>

如果我执行mvn package tomcat6:run,我可以访问testA.txt但我不能访问testB.txt,甚至如果,当我查看生成的“.war”时,我看到:

|->testA.txt
|->testB.txt
|->META-INF
|->WEB-INF
| |->web.xml
| |->classes

我不明白为什么我可以访问一个但我看不到另一个(404 错误)...

最佳答案

您无法访问testB.txt,因为在运行tomcat6:run 时,Tomcat Maven 插件默认会查看webapp 文件夹,不是在生成的 war 文件(通过 package 阶段),也不是在 target 文件夹中生成的解压 war。

这是有意为之的,以便您可以即时创建新资源或更改其内容,并且更改将在正在运行的实例上可用(热部署)。

您可以通过以下方式验证:

  • 在war文件中添加一个额外的testC.txt,它会被运行的实例忽略
  • 在构建的解包war中添加一个额外的testC.txt,它将被忽略
  • 在 webapp 文件夹中添加一个额外的 testC.txt,它将可用!

来自其official documentation :

The default location is ${basedir}/src/main/webapp

您可以通过 warSourceDirectory 元素配置它。在您的情况下,您想将其指向 target 文件夹的内置解压 war 。所以你可以改变你的配置如下:

<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
<warSourceDirectory>${project.build.directory}/${project.build.finalName}</warSourceDirectory>
</configuration>
</plugin>

注意:它现在指向通过 package 阶段构建的 Maven。它会起作用。

关于maven - Tomcat 不提供来自 maven-war-plugin 的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34934062/

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