【问题标题】:Maven/Eclipse external jar troubleshootingMaven/Eclipse 外部 jar 故障排除
【发布时间】:2024-01-19 07:18:02
【问题描述】:

我的问题是:如何将外部 jar 库添加到 jsp/maven/eclipse 项目中? 第二个问题;如果我怀疑我的问题是我的 pom.xml 文件中的语法错误;什么是正确的?

我是一名经验丰富的 Java 开发人员,我正在寻求将我的专业知识扩展到 Servlet 和 jsp 编程。我正在对此进行试验。

我已经获得了设置 maven3/eclipse (kepler) 工作站的帮助,并且我已经成功完成了一些基本教程。但是,我在让外部 .jar 与我的代码一起工作时遇到了严重的困难。我尝试了几种解决方案。

  1. 在线提供的一个防弹解决方案只是将我的 jar 放在 WEB-INF/lib 中,然后通过

  2. 在怀疑我没有指定我正在使用 maven 时,在搜索解决方案时,它确实是一个重要的细节,我发现了很多关于 pom.xml 文件的不同建议。依赖项和存储库是流行语。我尝试了几种不同的复制粘贴解决方案,但现在我的项目甚至不再构建。这是我尝试启动码头服务器并构建项目时的错误消息:

    H:\projects\xxx-releaseplan>mvn jetty:run
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building xxx-releaseplan Maven Webapp 1.0-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] >>> jetty-maven-plugin:8.1.13.v20130916:run (default-cli) @ xxx-releaseplan >>>
    [WARNING] The POM for com.xxx.mrpb.model.jar:com.xxx.mrpb.model.jar:jar:1.0 is missing, no dependency information availabl
    e
    [WARNING] The POM for com.xxx.mrpb.viewmodel.jar:com.xxx.mrpb.viewmodel.jar:jar:1.0 is missing, no dependency information
    available
    [WARNING] The POM for servlet-api-2.5.jar:servlet-api-2.5.jar:jar:2.5 is missing, no dependency information available
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 0.299s
    [INFO] Finished at: Tue Feb 25 13:47:26 CET 2014
    [INFO] Final Memory: 2M/61M
    [INFO] ------------------------------------------------------------------------
    [ERROR] Failed to execute goal on project xxx-releaseplan: Could not resolve dependencies for project com.xxx.releaseplan
    :xxx-releaseplan:war:1.0-SNAPSHOT: The following artifacts could not be resolved: com.xxx.mrpb.model.jar:com.xxx.mrpb.mode
    l.jar:jar:1.0, com.xxx.mrpb.viewmodel.jar:com.xxx.mrpb.viewmodel.jar:jar:1.0, servlet-api-2.5.jar:servlet-api-2.5.jar:jar:
    2.5: Failure to find com.xxx.mrpb.model.jar:com.xxx.mrpb.model.jar:jar:1.0 in http://repo.maven.apache.org/maven2 was cach
    ed in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updat
    es are forced -> [Help 1]
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
    H:\projects\xxx-releaseplan>
    

我的 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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>dk.sdc.releaseplan</groupId>
    <artifactId>sdc-releaseplan</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>sdc-releaseplan Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>dk.sdc.mrpb.model.jar</groupId>
            <artifactId>dk.sdc.mrpb.model.jar</artifactId>
            <version>1.0</version>
            <scope>compile</scope>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>dk.sdc.mrpb.viewmodel.jar</groupId>
            <artifactId>dk.sdc.mrpb.viewmodel.jar</artifactId>
            <version>1.0</version>
            <scope>compile</scope>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>servlet-api-2.5.jar</groupId>
            <artifactId>servlet-api-2.5.jar</artifactId>
            <version>2.5</version>
            <scope>compile</scope>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>sdc-releaseplan</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <!--<plugin> <groupId>ro.isdc.wro4j</groupId> <artifactId>wro4j-maven-plugin</artifactId> 
                <version>${wro4j.version}</version> <executions> <execution> <goals> <goal>jshint</goal> 
                </goals> </execution> </executions> <configuration> <options>devel,evil,noarg</options> 
                </configuration> </plugin> -->
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>initialize</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo>********************************************</echo>
                                <echo>***** This project REQUIRES Maven 3.0+ *****</echo>
                                <echo>********************************************</echo>
                                <echo>mvn jetty:run - Running as un-assembled webapp</echo>
                                <echo>mvn jetty:run-war - Running as assembled webapp</echo>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <!-- Jetty Version: 8.x Home: Eclipse, Codehaus Java Version: 1.6 Protocols: 
                    HTTP/1.1 RFC2616, WebSocket, SPDY Servlet Version: 3.0 JSP Version: 2.1 http://wiki.eclipse.org/Jetty -->
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>8.1.13.v20130916</version>
                <configuration>
                    <webAppConfig>
                        <contextPath>/</contextPath>
                    </webAppConfig>
                    <scanIntervalSeconds>1</scanIntervalSeconds>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

【问题讨论】:

    标签: java eclipse jsp maven jar


    【解决方案1】:

    错误

    The following artifacts could not be resolved
    

    说 jar 文件在存储库位置不可用:http://repo.maven.apache.org/maven2

    这是在 M2_HOME 的 settings.xml 中配置的。检查 jar 文件的版本是否在 repo 中可用。

    如果您可以重新检查依赖项是否可用,请检查您的互联网连接以下载它。

    如果下载了jar文件,则检查users/.m2/repository/&lt;your artifact path&gt;以检查是否正确下载。

    【讨论】:

    • 换句话说,我下载的 jar 文件应该放在存储库文件夹中吗?让我试试。提前致谢。
    • 我似乎在存储库中生成了一大堆无用的文件夹...
    • 如果您可以冒险再次下载所有jar文件,那么您可以删除存储库并运行mvn clean install。它会下载你所有的jar文件
    • 我认为这行不通。我尝试使用的 .jar 文件是我自己生成的。它们包含一个独立应用程序的模型类,我正在尝试将其制作成一个 Web 应用程序。
    • 所以我说它的风险。然后您需要调整您正在使用的配置文件并确保 .m2/repository 中存在的所有 jars