knly
knly

Reputation: 81

The right way to use maven

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.

Upvotes: 2

Views: 602

Answers (3)

Aaron Digulla
Aaron Digulla

Reputation: 328754

  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.

Upvotes: 0

wemu
wemu

Reputation: 8160

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>

Upvotes: 2

Tim
Tim

Reputation: 20777

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>

Upvotes: 1

Related Questions