Maven:父Spring Webapp [英] Maven: Parent Spring Webapp

查看:75
本文介绍了Maven:父Spring Webapp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为使用Maven和Spring的所有Web应用程序提供框架基础代码. Tomcat7在我的子项目上正确启动,但是尝试打开index.html时得到404.

I would like to have a framework base code for all my webapps using Maven and Spring. Tomcat7 is starting correctly on my child project, but I get a 404 when trying to open index.html.

父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.

The web.xml is provided in the parent project (base). The JSPs are provided in the child project.

我猜想404是由于pom打包造成的,因为子项目不知道从何处获取父项目的文件.我错了吗?怎么解决呢?

I guess the 404 is due to the pom packaging as the child project doesn't know where to get the files of the parent project. Am I wrong? What's the reasona dn how to solve it?

编辑 是否可以创建一个定义所有配置(web.xml,applicationContext.xml,servletContext.xml,Controllers等)和包含JSP内容的子项目的父spring webapp?

EDIT Is it possible to create a parent spring webapp which defines all the configuration (web.xml, applicationContext.xml, servletContext.xml, Controllers, ...) and child projects which contains the JSP stuff?

推荐答案

看看 maven war插件.该插件允许使用叠加.

Have a look at the maven war plugin. This plugin allows what it calls overlays.

叠加层用于在多个Web应用程序之间共享公共资源. WAR项目的依赖项收集在WEB-INF/lib中,但WAR工件除外,这些工件叠加在WAR项目本身上.

Overlays are used to share common resources across multiple web applications. The dependencies of a WAR project are collected in WEB-INF/lib, except for WAR artifacts which are overlayed on the WAR project itself.

它可能对您和您的要求有用.

It might prove useful to you and your requirements.

这篇关于Maven:父Spring Webapp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆