2016-01-21 54 views
2

我试图让我的Tomcat服务器提供文件。我做了一个非常简单的例子,告诉你什么是错的,即使它很简单,它也不起作用。Tomcat未从maven-war-plugin提供文件

我的项目是由这样的:

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错误)...

回答

3

运行tomcat6:run的时候,因为你不能访问testB.txt,Tomcat的Maven插件会看webapp文件夹在默认情况下,而不是在生成的WAR文件(通过package阶段),也不在target文件夹中生成的解压战争。

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

您可以通过验证:

  • 添加额外testC.txt战争文件,它会通过运行实例
  • 忽视添加额外testC.txt到内置解压的战争,它会被忽略
  • 将另外的testC.txt添加到webapp文件夹,它将可用!

从它official documentation

默认位置为$ {BASEDIR}/src目录/主/ web应用

您可以通过warSourceDirectory元素进行配置。在你的情况下,你要指出它在target文件夹的内置解压战争。所以,你可以改变你的配置如下:

<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的指向。它会起作用。

+0

感谢您的解释。我没有意识到这一点。现在它工作得很好。 – Utundu

2

tomcat6:run不运行打包的战争,请尝试用mvn tomcat6:run-war来代替。

+0

这也适用。谢谢 ! – Utundu