Maven没有使用服务器的servlet-api构建

时间:2017-03-20 09:54:06

标签: java eclipse maven tomcat servlets

方案

在尝试阅读BalusC的回答on this post后,我尝试将服务器的servlet-api添加到现有的动态Web应用程序中。

所以从早上开始使用maven依赖项目

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

在阅读了BalusC的回答之后,我从javax.servlet删除了pom.xml依赖关系,并将* Target Runtime添加到我的项目中。

* Target Runtime

问题

在此之后所有错误(在eclipse的窗口中)都消失了。但是当我尝试maven clean install时,它会[ERROR] COMPILATION ERROR : package javax.servlet does not exist

但是,如果我在javax.servlet中添加了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>IncidentManagement</groupId>
    <artifactId>IncidentManagement</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <sourceDirectory>src.main.java</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <java.version>1.8</java.version>
        <junit.version>4.12</junit.version>
        <servlet.version>3.1.0</servlet.version>
        <mojarra.version>2.2.12</mojarra.version>
        <primefaces.version>5.3</primefaces.version>
        <maven.compiler.plugin.version>3.3</maven.compiler.plugin.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <repositories>
        <repository>
            <id>JBoss repository</id>
            <url>http://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
    </repositories>

    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>

        <!-- ORACLE database driver -->

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
         <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency> 

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.3.Final</version>
        </dependency>

        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>

        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>

        <!-- <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${servlet.version}</version>
            <scope>provided</scope>
        </dependency> -->
        <!-- Mojarra JSF -->
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>${mojarra.version}</version>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>${mojarra.version}</version>
        </dependency>
        <!-- PrimeFaces -->
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>${primefaces.version}</version>
        </dependency>

        <!-- Excel and CSV -->

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.10-FINAL</version>
            <type>jar</type>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.10-FINAL</version>
            <type>jar</type>
        </dependency>

        <!-- PDF -->

        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.1.7</version>
        </dependency>

        <!-- JSON -->
        <dependency>
           <groupId>com.googlecode.json-simple</groupId>
           <artifactId>json-simple</artifactId>
           <version>1.1.1</version>
        </dependency>

        <!-- Primefaces Theme -->
        <!-- https://mvnrepository.com/artifact/org.primefaces.extensions/all-themes -->
        <dependency>
            <groupId>org.primefaces.extensions</groupId>
            <artifactId>all-themes</artifactId>
            <version>1.0.8</version>
        </dependency>


    </dependencies>

</project>

P.S:我使用的是Java 8,Apache tomcat 8和maven 3.

2 个答案:

答案 0 :(得分:0)

说明它找不到课程是完全正确的,因为你没有在你的pom文件中找到它......

将依赖关系作为&#34;提供&#34;是正确的。在编译时,它将使用从存储库下载的存根,在运行时它将使用servlet容器中安装和提供的实际类。

Balus的答案与maven项目无关,只有本机Eclipse项目不是在Eclipse外部构建的。

答案 1 :(得分:-1)

@Junaid,我在工作场所遇到同样的问题。后来我意识到servlet-api.jar文件不是工作场所提供的存储库。我猜想,我们在代理后面,并且从本地存储库下载文件而不是从Maven下载文件。为此,我不得不使用j2ee.jar文件。