2016-01-21 53 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