有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Maven未使用服务器的servletapi构建

场景

在阅读了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的答案之后,我从pom.xml中删除了javax.servlet依赖项,并将*目标运行时添加到我的项目中

*Target Runtime

问题

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

但是同样,如果我在pom.xml中添加了javax.servlet的依赖项,那么它可以正常工作

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:我正在使用Java8、ApacheTomcat8和maven 3


共 (2) 个答案

  1. # 1 楼答案

    它说它找不到类是非常正确的,因为您的pom文件中没有该类

    将依赖项设置为“提供”是正确的。在编译时,它将使用从存储库下载的存根,在运行时,它将使用安装到servlet容器中并由servlet容器提供的实际类

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

  2. # 2 楼答案

    @Junaid,我在工作场所也遇到了同样的问题。后来我意识到ServletAPI。jar文件不是workplace提供的存储库。我想,我们是在代理之后,它从本地存储库下载文件,而不是maven。我不得不使用j2ee。用于此目的的jar文件