2013-05-17 25 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

相关问题