2013-05-17 68 views
2

我想爲使用Maven和Spring的所有webapps提供框架基本代碼。 Tomcat7在我的子項目上正確啓動,但在嘗試打開index.html時收到404。Maven:Parent Spring Webapp

父POM:

<?xml version="1.0" encoding="UTF-8"?> 
<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>com.example.base</groupId> 
    <artifactId>base</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 

    <packaging>pom</packaging> 

    <properties> 

     <!-- Generic properties --> 
     <java.version>1.6</java.version> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 

     <!-- Spring --> 
     <spring.version>3.2.2.RELEASE</spring.version> 

     <!-- Servlet --> 
     <jstl.version>1.2</jstl.version> 
     <servlet.version>2.5</servlet.version> 

     <!-- Logging --> 
     <slf4j.version>1.7.5</slf4j.version> 
     <log4j.version>1.2.17</log4j.version> 

     <!-- Test --> 
     <junit.version>4.11</junit.version> 

    </properties> 

    <dependencies> 

     <!-- Spring --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <version>${spring.version}</version> 
      <exclusions> 
       <!-- Exclude Commons Logging in favor of SLF4j --> 
       <exclusion> 
        <groupId>commons-logging</groupId> 
        <artifactId>commons-logging</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 

     <!-- Servlet --> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>servlet-api</artifactId> 
      <version>${servlet.version}</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
      <version>${jstl.version}</version> 
     </dependency> 

     <!-- Logging --> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-api</artifactId> 
      <version>${slf4j.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>jcl-over-slf4j</artifactId> 
      <version>${slf4j.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-log4j12</artifactId> 
      <version>${slf4j.version}</version> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>log4j</groupId> 
      <artifactId>log4j</artifactId> 
      <version>${log4j.version}</version> 
      <scope>runtime</scope> 
     </dependency> 

     <!-- Apache Tiles --> 
     <dependency> 
      <groupId>org.apache.tiles</groupId> 
      <artifactId>tiles-jsp</artifactId> 
      <version>2.2.2</version> 
      <exclusions> 
       <!-- Exclude Commons Logging in favor of SLF4j --> 
       <exclusion> 
        <groupId>commons-logging</groupId> 
        <artifactId>commons-logging-api</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 

    </dependencies> 

    <build> 

     <defaultGoal>install</defaultGoal> 

     <plugins> 

      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.0</version> 
       <configuration> 
        <compilerArguments> 
         <Xlint /> 
        </compilerArguments> 
        <verbose>true</verbose> 
        <source>${java.version}</source> 
        <target>${java.version}</target> 
        <showWarnings>true</showWarnings> 
       </configuration> 
      </plugin> 

      <plugin> 
       <groupId>org.apache.tomcat.maven</groupId> 
       <artifactId>tomcat7-maven-plugin</artifactId> 
       <version>2.0</version> 
       <configuration> 
        <webXml>src/main/webapp/WEB-INF/web.xml</webXml> 
       </configuration> 
       <executions> 
        <execution> 
         <id>tomcat-run</id> 
         <goals> 
          <goal>run</goal> 
         </goals> 
         <phase>pre-integration-test</phase> 
        </execution> 
        <execution> 
         <id>tomcat-shutdown</id> 
         <goals> 
          <goal>shutdown</goal> 
         </goals> 
         <phase>post-integration-test</phase> 
        </execution> 
       </executions> 
      </plugin> 

     </plugins> 

    </build> 

</project> 

兒童POM:

<?xml version="1.0" encoding="UTF-8"?> 
<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>com.example.child</groupId> 
    <artifactId>child</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 

    <packaging>war</packaging> 

    <parent> 
     <groupId>com.example.base</groupId> 
     <artifactId>base</artifactId> 
     <version>0.0.1-SNAPSHOT</version> 
    </parent> 

    <build> 

     <plugins> 

      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-war-plugin</artifactId> 
       <version>2.3</version> 
      </plugin> 

     </plugins> 

    </build> 

</project> 

符web.xml在父項目(基極)提供。 JSP在子項目中提供。

我猜404是由於POM包裝,因爲子項目不知道從哪裏獲得父項目的文件。我錯了嗎?如何解決它的原因是什麼?

編輯 是否有可能創建一個父春天web應用程序定義所有的配置文件(web.xml,applicationContext.xml中,servletContext.xml,控制器,...)和兒童項目,其中包含JSP的東西?

回答

3

看看maven war plugin。這個插件允許它叫做overlays

覆蓋用於跨多個Web應用程序共享公共資源。 WAR項目的依賴關係在WEB-INF/lib中收集,WAR項目自身上覆蓋的WAR工件除外。

它可能證明對您和您的要求有用。

+0

這正是我所期待的。十分感謝! – dtrunk

+0

@dtrunk樂於提供幫助。 –

+0

很好的回答(+1)!!! – Michael

1

您應該在每個模塊中添加依賴關係。 最好的做法是在父pom中添加dependencyManagement,並在其中列出與版本,作用域等有關的依賴關係。 請注意,父模塊根本沒有依賴關係元素(因爲它只聲明依賴關係)
在孩子pom包含不依賴版本,範圍等

dependencyManagement父POM:

<dependencyManagement> 

    <!-- Spring --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
     <version>${spring.version}</version> 
     <exclusions> 
      <!-- Exclude Commons Logging in favor of SLF4j --> 
      <exclusion> 
       <groupId>commons-logging</groupId> 
       <artifactId>commons-logging</artifactId> 
      </exclusion> 
     </exclusions> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 

    <!-- Servlet --> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>servlet-api</artifactId> 
     <version>${servlet.version}</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>jstl</artifactId> 
     <version>${jstl.version}</version> 
    </dependency> 

    <!-- Logging --> 
    <dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>slf4j-api</artifactId> 
     <version>${slf4j.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>jcl-over-slf4j</artifactId> 
     <version>${slf4j.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>slf4j-log4j12</artifactId> 
     <version>${slf4j.version}</version> 
     <scope>runtime</scope> 
    </dependency> 
    <dependency> 
     <groupId>log4j</groupId> 
     <artifactId>log4j</artifactId> 
     <version>${log4j.version}</version> 
     <scope>runtime</scope> 
    </dependency> 

    <!-- Apache Tiles --> 
    <dependency> 
     <groupId>org.apache.tiles</groupId> 
     <artifactId>tiles-jsp</artifactId> 
     <version>2.2.2</version> 
     <exclusions> 
      <!-- Exclude Commons Logging in favor of SLF4j --> 
      <exclusion> 
       <groupId>commons-logging</groupId> 
       <artifactId>commons-logging-api</artifactId> 
      </exclusion> 
     </exclusions> 
    </dependency> 

</dependencyManagement> 

依賴於子POM:

<dependencies> 

    <!-- Spring --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 

    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 

    </dependency> 

    <!-- Servlet --> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>servlet-api</artifactId> 

    </dependency> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>jstl</artifactId> 

    </dependency> 

    <!-- Logging --> 
    <dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>slf4j-api</artifactId> 

    </dependency> 
    <dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>jcl-over-slf4j</artifactId> 

    </dependency> 
    <dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>slf4j-log4j12</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>log4j</groupId> 
     <artifactId>log4j</artifactId> 
    </dependency> 

    <!-- Apache Tiles --> 
    <dependency> 
     <groupId>org.apache.tiles</groupId> 
     <artifactId>tiles-jsp</artifactId> 
     </dependency> 

</dependencies> 
+0

我可以編譯和運行子項目沒有任何問題。父項目包含子項目依賴的文件(web.xml,applicationContext.xml,servletContext.xml,tiles.xml)。我不想在每個子項目中添加依賴項。 – dtrunk

相關問題