有 Java 编程相关的问题?

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

java本地JAR不包括在类路径中(`<scope>system</scope>`)

我正在尝试使用maven和eclipse构建我的应用程序。 我依赖于本地机器上的第三方JAR。 这是我的pom。xml

<dependency>
    <groupId>sourceforge.net</groupId>
   <artifactId>zipdiff</artifactId>
   <version>0.4</version>
   <scope>system</scope>
   <systemPath>C:/gelcap/lib/zipdiff-0.4.jar</systemPath>
</dependency>

<dependency>
    <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>1.2.11</version>
</dependency>

当我运行mvn:install时,它会为我的项目创建war文件。 但问题是它不包括zipdiff。jar文件到web inf/lib文件夹,它只包括下载的文件。我还需要包括从本地系统复制文件,但maven会忽略它们。 我没有想到为什么maven没有在我的war文件中包含具有系统作用域的文件。 请告诉我如何解决这个问题。 提前谢谢


共 (5) 个答案

  1. # 1 楼答案

    只是一件触动我眼睛的事: 在路径中,您使用的是普通unix样式的斜杠/。Windows对路径使用反斜杠:。 我不知道maven是否能够将它们相互转换,所以可以尝试如下输入路径:

    C:\gelcap\lib\zipdiff-0.4.jar

  2. # 3 楼答案

    I have not got any thought why it is happening why maven is not including files with system scope to my war file. Please give me any idea how this problem can be resolved.

    这是在设计上,system范围内的依赖项应该作为documented提供

    事实上,我已经写了很多次(herehereherehere)应该避免system范围的依赖关系。大多数情况下,它们是一种不好的做法,人们正在虐待它们,它们产生的麻烦几乎总是多于好处

    如果你想要一个“官方”的观点,让我引用Dependency Scopes迷你指南:

    • system: This dependency is required in some phase of your project's lifecycle, but is system-specific. Use of this scope is discouraged: This is considered an "advanced" kind of feature and should only be used when you truly understand all the ramifications of its use, which can be extremely hard if not actually impossible to quantify. This scope by definition renders your build non-portable. It may be necessary in certain edge cases. The system scope includes the <systemPath> element which points to the physical location of this dependency on the local machine. It is thus used to refer to some artifact expected to be present on the given local machine an not in a repository; and whose path may vary machine-to-machine. The systemPath element can refer to environment variables in its path: ${JAVA_HOME} for instance.

    因此,与其使用system范围,不如:

    • 通过install:install-file将库添加到本地存储库。这是一种快速而肮脏的工作方式,如果你独自一人,这可能是一种选择,但它会使你的构建不可移植
    • 安装并运行Nexus、Archiva或Artifactory等“企业存储库”,并通过deploy:deploy-file添加库。这是理想的情况
    • 按照this previous answer中所述设置一个基于文件的存储库,并将库放在其中。如果您没有公司存储库,但需要团队合作,又不想牺牲可移植性,那么这是最好的折衷方案

    请停止使用system范围

  3. # 4 楼答案

    在“系统”范围内,容器有望提供人工制品。从maven docs开始:

    provided

    This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.

    [...]

    system

    This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.

    通过使用系统作用域,您向war插件指示容器将提供这种依赖关系。由于这不是您打算做的,最简单的解决方案是将人工制品放在存储库中,可以是您的本地maven存储库,也可以是您自己的内部网maven repo(如果您有)

    可以使用install:install-file目标将单个文件(不带POM)安装到本地存储库中。完成此操作后,将依赖项类型更改为“compile”,并删除“systemPath”元素

    在我的日常工作中,我们使用Nexus来管理公司内部网上的一个公司范围的存储库。您可以为自己的人工制品和第三方人工制品建立单独的存储库。Nexus还充当代理,缓存外部存储库中的人工制品,大大加快了构建速度。这意味着,只有使用其他REPO中不可用的新依赖项的开发人员必须上传——之后,所有其他开发人员都可以使用它——他们可以从SCM中签出并构建,而不必担心依赖项位于何处

  4. # 5 楼答案

    因为之前没有尝试过这个方法,所以它可能无法工作,您可以更改编译的范围吗

    <dependency>
       <groupId>sourceforge.net</groupId>
       <artifactId>zipdiff</artifactId>
       <version>0.4</version>
       <scope>compile</scope>
       <systemPath>C:/gelcap/lib/zipdiff-0.4.jar</systemPath>
    </dependency>