maven的正确使用方法

发布于 2025-01-05 07:05:41 字数 421 浏览 5 评论 0原文

我是 Maven 新手,这里有两个问题:

1、如何解决工件缺失问题?

我需要 jcharts-0.7.5,但 Maven 中央存储库中没有它。我有 jar 文件,但将其放入 Maven 项目似乎并不容易。

2、如何修复工件错误的依赖范围?

我有一个 WAR 项目依赖于工件 axis2-kernel,它依赖于 servlet-api-2.3,其范围为“compile”(错误为“provided”),因此 mvn install 将 servlet- 打包api-2.3.jar 到 war 文件中,并导致“validateJarFile(...) - jar not returned.” Tomcat 7 中出现错误。

I'm new to maven, I got 2 problems here:

1,How to solve missing artifact problem?

I need jcharts-0.7.5, but it's not available in Maven Central Repository. I have the jar file, but seems not easy to put it into a maven project.

2,How to fix wrong dependency scope of artifact?

I have a WAR project depends on artifact axis2-kernel, which is depending on servlet-api-2.3 with the scope of 'compile' (mistake of 'provided'), so mvn install packet the servlet-api-2.3.jar into the war file, and causes a "validateJarFile(...) - jar not loaded." error in Tomcat 7.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

∞琼窗梦回ˉ 2025-01-12 07:05:41

第一:使用 Maven 存储库。这可以是一个具有静态内容的简单 Apache HTTP 站点。但我建议使用 Sonatype Nexus 或 JFrog Artifactory 来存储在其他地方找不到的工件。
您可能会在 JBoss 存储库或 IBiblio(两者都很大)中找到它们

。第二:您可以将该依赖项添加到您的 pom 中,然后设置您想要的范围。如果您排除了该工件,则无论如何您都必须再次添加它。 Maven 总是更喜欢直接在你的 pom 中的内容:

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <scope>provided</scope>
</dependency>

Number one: use a maven repository. This can be a simple Apache HTTP site with static content. But I would recommend using Sonatype Nexus or JFrog Artifactory for storing artifacts not found somewhere else.
You may find them in the JBoss repository or IBiblio (both quite big)

Number two: you can add that dependency to your pom and just set the scope you want. If you exclude the artifact you will have to add it again anyway. Maven will always prefer what is directly in you pom:

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <scope>provided</scope>
</dependency>
栀梦 2025-01-12 07:05:41

要回答您的第二个问题:您可以使用 < 排除传递依赖项排除>标签: http://maven.apache.org /guides/introduction/introduction-to-Optional-and-excludes-dependency.html

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>org.apache.axis2</groupId>
      <artifactId>axis2-kernel</artifactId>
      <version>...</version>
      <exclusions>
        <exclusion><!-- declare the exclusion here -->
          <groupId>javax.servlet</groupId>
          <artifactId>servlet-api</artifactId>
        </exclusion>
      </exclusions> 
    </dependency>
  </dependencies>
</project>

To answer your second question: you can exclude transitive dependencies using the < exclusion > tag: http://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>org.apache.axis2</groupId>
      <artifactId>axis2-kernel</artifactId>
      <version>...</version>
      <exclusions>
        <exclusion><!-- declare the exclusion here -->
          <groupId>javax.servlet</groupId>
          <artifactId>servlet-api</artifactId>
        </exclusion>
      </exclusions> 
    </dependency>
  </dependencies>
</project>
や莫失莫忘 2025-01-12 07:05:41
  1. 有许多不同的解决方案如何添加缺少的依赖项。例如,您可以使用范围 system 并提供路径(您可以将 JAR 放入项目中并使用相对于 ${basedir} 的路径)。

    但更好的解决方案是安装 NexusArtifactory 因为它们允许您创建自己的存储库,并且在互联网出现故障时它们仍然可以工作(例如,当某个白痴破坏了您的路由器或您的 ISP 有问题或有人将 18 英寸双 T 钢梁穿过一堆纤维)。

    构建速度也会快得多,因为下载将通过本地 LAN 进行,而不是绕行半个地球。

  2. 您可以简单地在不同范围的 POM 中重复依赖项(您的 POM 总是获胜),或者您可以 使用 dependencyManagement 元素

    我更喜欢 dependencyManagement 方法,因为它允许您在一个位置为所有项目设置范围和版本。

  1. There are many different solutions how to add missing dependencies. For example, you could use the scope system and provide the path (you can put the JAR into the project and use a path relative to ${basedir}).

    But a much better solution is to install a server like Nexus or Artifactory because they allow you to create your own repositories and they still work should the Internet fail (for example, when some idiot breaks your router or your ISP has some problems or someone drives a 18 inch double-T steel beam through a bunch of fibres).

    Builds will also be much faster since the downloads will be via your local LAN instead of going around half the globe.

  2. You can simply repeat the dependency in your POM with a different scope (your POM always wins) or you can use a dependencyManagement element.

    I prefer the dependencyManagement approach because it allows you to set scopes and versions in a single place for all your projects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文